简体   繁体   English

为什么在 Nodejs 中写入 excel 文件时插入外部行?

[英]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.我正在学习 Nodejs,我正在通过模块“XLSX”npm 使用 Nodejs 读写 excel 文件。 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].但是有一个外部行正在插入代替第一行,并且行的数据是[A,B,C,D.....列名]。

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:尝试在 output 中添加不包括 header 行的标志:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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