React中使用xlsx读取Excel时如何跳过空行
Great job getting Excel file reading up and running in React! Dealing with empty rows cluttering your parsed data is a common pain point with the xlsx library, and you’re right to look for a way to handle this at the source instead of filtering after the fact.
The good news is that the sheet_to_json method has a built-in option to skip blank rows entirely. You just need to add the blankrows: false parameter to your parsing configuration. This tells the library to ignore any rows that don’t contain meaningful data, eliminating empty entries from your result array directly.
Here’s your modified code with this fix included:
var reader = new FileReader(); reader.onload = function (e) { var data = e.target.result; let readedData = XLSX.read(data, {type: 'binary'}); const wsname = readedData.SheetNames[0]; const ws = readedData.Sheets[wsname]; /* Converts a worksheet object to an array of JSON objects, skipping blank rows */ const parsedData = XLSX.utils.sheet_to_json(ws, {header:1, blankrows: false}); console.log(parsedData); } reader.readAsBinaryString(fileName);
Notice the addition of blankrows: false in the options passed to sheet_to_json—that’s the key change here. This will handle most cases where rows are truly empty. If you run into rows that have whitespace or hidden formatting but appear empty, you might still need a quick filter for those edge cases, but this should cover the majority of scenarios and keep your data clean from the start.
内容的提问来源于stack exchange,提问作者DevLoverUmar

