简体   繁体   中英

How to properly convert an excel worksheet to json using XLSX?

I am trying to convert an Excel worksheet into a JSON object using the built in node module XLSX' function XLSX.utils.sheet_to_json() However, the Excel sheet contains a column without a name, an empty cell. In order to maintain that information, I have set "sheetStubs" to "true" when reading the file in the first place:

var data = await new Promise((resolve) => {
let reader = new FileReader();

reader.onload = () => resolve(reader.result);

reader.readAsArrayBuffer(file[0]);


});
  var temp = new Uint8Array(data);
  var workbook = XLSX.read(temp, { type: "array", sheetStubs: true });

"file" is the excel file given by the user through an input field. This functions as intended. Now, when I convert one of the sheets into a JSON object using said function:

var json = XLSX.utils.sheet_to_json(worksheet);

There is unwanted behaviour, such as the empty cell still being considered a column and the conversion stopping at that column, ignoring every column beyond that. I was unable to find any more documentation about XLSX.utils.sheet_to_json() beyond this:

XLSX.utils.sheet_to_json generates an array of objects

What arguments could I pass the function in order to change the behaviour? Is there anywhere I can find more documentation on this?

The JSON section of their docs has more info on this. sheet_to_json accepts a 2nd opts argument, an object with these optional properties:

Option Name Default Description
raw true Use raw values (true) or formatted strings (false)
range from WS Override Range (see table below)
header Control output format (see table below)
dateNF FMT 14 Use specified date format in string output
defval Use specified value in place of null or undefined
blankrows ** Include blank lines in the output **

I've run into similar issues as you, and specifying a defval of an empty string works well to ensure empty cells aren't skipped:

xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { defval: '' })

Now, if you have empty column headers you will likely have to eliminate your header row completely and define your own header array. Using this method, the returned data will be an array of arrays instead of an array of objects:

xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { 
  defval: '',
  header: ['A', 'B', 'C', '', 'D'] // blank column
})

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM