简体   繁体   中英

Why an external row is inserting while writing the excel file in Nodejs?

I am learning Nodejs and I am reading and writing the excel file using Nodejs by the module "XLSX" npm. Reading and writing the data done successfully. But there is an external row which is inserting in place of first row and the data of row is [A, B, C, D..... the column name].

How to solve that problem? the code is below.

 {
 var filepath = './src/assets/uploads/csv template/fileName.csv';
var workbook = XLSX.readFile(filepath);
var sheet_name_list = workbook.SheetNames;
var xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]], { header: 'A' });
let worksheet = workbook.Sheets[sheet_name_list[0]];
if(xlData){
    xlData[0]["L"] = "hash";
    for(let i = 1; i< xlData.length; i++){
        xlData[i]["L"] = generatehasnumber(xlData[i]["A"], xlData[i]["D"]);
    }

}

if(xlData[1]["A"] == "A" )
{
    xlData.shift();
}
const ws = XLSX.utils.json_to_sheet(xlData)
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, "hash")
XLSX.writeFile(wb, './src/assets/uploads/csv template/fileName.csv')  
}

try adding the flag for not including header row in output:

const ws = XLSX.utils.json_to_sheet(xlData, { skipHeader: true })

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