获取标题行 REACT XLSX
我目前正在处理 Excel 文档,但我只需要获取表头名称。 到目前为止,我的代码看起来像这样
if(file){
let fileReader = new FileReader();
fileReader.readAsBinaryString(file);
fileReader.onload = (event) =>{
let data = event.target?.result;
let workbook = XLSX.read(data,{type:"binary"});
console.log('WORKBOOK',workbook)
workbook.SheetNames.forEach(sheet => {
let rowObject = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
console.log('RowObject',rowObject)
setFile(rowObject)
})
}
}
I'm currently working with an excel document, but I need to get only the table headers name.
So far my code looks like this
if(file){
let fileReader = new FileReader();
fileReader.readAsBinaryString(file);
fileReader.onload = (event) =>{
let data = event.target?.result;
let workbook = XLSX.read(data,{type:"binary"});
console.log('WORKBOOK',workbook)
workbook.SheetNames.forEach(sheet => {
let rowObject = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
console.log('RowObject',rowObject)
setFile(rowObject)
})
}
}
Inside the Sheets object, I can see the cells, including the header name, how can I get only the header row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式获取标头。
You can get headers in the following way.
您可以将工作表转换为sheet_to_json helper,并且可以跳过第一行(标题)添加 header:1,此外,如果您设置 defval:"" 那么您将用空白文本替换默认值,您可以将其替换为 undefined 或任何其他您想要的值。
当您使用此参数应用此方法时,您将获得一个数组数组,此数组数组中的第一个数组是标题。
You can convert ths sheet to sheet_to_json helper and you can skip the firt row (the headers) adding header:1, additionally if you set defval:"" then you will raplacing default values with blanck text, you can replace it with undefined or anyother value that you want.
When you apply this method with this parameters you will obtain an array of arrays, the first array in this array of arrays is the header.