I'm converting a file from xlsx format to json, I can do it correctly, but I could not get it to show the cells that are empty, just ignore them. I am using the XLSX library. This is the code with which I do the parsing.
const workbook = XLSX.readFile(filename);
const sheet_name_list = workbook.SheetNames;
let jsonPagesArray = [];
sheet_name_list.forEach(function(sheet) {
const jsonPage = {
name: sheet,
content: XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
};
jsonPagesArray.push(jsonPage);
});
res.json(
{
data:jsonPagesArray
}
);
});
actually if a give this:
xxx1 | xxx2 | xxx3
------------------
yyyy | yyyy |
zzzz | zzzz | zzzz
it return me:
[
{
xxx1:yyyy,
xxx2:yyyy
}
],
[
{
xxx1:zzzz,
xxx2:zzzz,
xxx3:zzzz
}
]
i want it return me something like this:
[
{
xxx1:yyyy,
xxx2:yyyy,
xxx3:""
}
],
[
{
xxx1:zzzz,
xxx2:zzzz,
xxx3:zzzz
}
]
你能在你的代码中用content: XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {defval:""})
替换content: XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
content: XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {defval:""})
试吗?
This should work.Working example
let XLSX = require('xlsx');
const workbook = XLSX.readFile(filename);
const sheet_name_list = workbook.SheetNames;
let jsonPagesArray = [];
sheet_name_list.forEach(function(sheet) {
const jsonPage = {
name: sheet,
content: JSON.parse(JSON.stringify(XLSX.utils.sheet_to_json(workbook.Sheets[sheet],{defval:""})))
};
jsonPagesArray.push(jsonPage);
});
console.log(jsonPagesArray[0].content);
您可以使用
XLSX.utils.sheet_to_json(worksheet , {defval:""})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.