跳过JavaScript sheet-j中的XLSX在Excel中启动空白行
我有以下代码在标题行是第1行时效果很好,
readerData(rawFile) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
const data = e.target.result;
const workbook = XLSX.read(data, { type: "array" });
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
const header = this.getHeaderRow(worksheet);
const results = XLSX.utils.sheet_to_json(worksheet,{ header: 0, range: 0, defval: ""});
this.generateData({ header, results });
this.loading = false;
resolve();
};
reader.readAsArrayBuffer(rawFile);
});
},
generateData({ header, results }) {
this.excelData.header = header;
this.excelData.results = results;
this.excelData.original_results = [...results];
this.onSuccess && this.onSuccess(this.excelData);
var grid = this.$refs.membersGrid.ej2Instances;
grid.dataSource = this.excelData.results;
grid.refresh();
},
getHeaderRow(sheet) {
const headers = [];
const range = XLSX.utils.decode_range(sheet["!ref"]);
let C;
const R = range.s.r;
/* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) {
/* walk every column in the range */
const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })];
/* find the cell in the first row */
let hdr = "UNKNOWN " + C; // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell);
headers.push(hdr);
}
return headers;
},
它运行良好,并将所有标头值放入exceldata.header中,并且将所有命名的数组数据放入Exceldata.Results。我的问题是,当第一行或前两个行是空白的,或者我需要跳过它们时,一切都会弄乱。我已经尝试过
https://github.com/SheetJS/sheetjs/issues/463
,但是我正在使用“ xlsx”:“^0.17.1”。当我使用时,
range.s.r = 1;
我能够将范围更改为A2,但是我无法获得命名的数据阵列。任何帮助都将受到赞赏。
I have the following code that works great when the header row is row 1
readerData(rawFile) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
const data = e.target.result;
const workbook = XLSX.read(data, { type: "array" });
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
const header = this.getHeaderRow(worksheet);
const results = XLSX.utils.sheet_to_json(worksheet,{ header: 0, range: 0, defval: ""});
this.generateData({ header, results });
this.loading = false;
resolve();
};
reader.readAsArrayBuffer(rawFile);
});
},
generateData({ header, results }) {
this.excelData.header = header;
this.excelData.results = results;
this.excelData.original_results = [...results];
this.onSuccess && this.onSuccess(this.excelData);
var grid = this.$refs.membersGrid.ej2Instances;
grid.dataSource = this.excelData.results;
grid.refresh();
},
getHeaderRow(sheet) {
const headers = [];
const range = XLSX.utils.decode_range(sheet["!ref"]);
let C;
const R = range.s.r;
/* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) {
/* walk every column in the range */
const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })];
/* find the cell in the first row */
let hdr = "UNKNOWN " + C; // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell);
headers.push(hdr);
}
return headers;
},
It works great and put all of the Header values into the excelData.header and it put all of the named array data into the excelData.results. My problem is it all goes to a mess when the first row or first two rows are blank or I need to skip them. I've tried
https://github.com/SheetJS/sheetjs/issues/463
but I'm using "xlsx": "^0.17.1" . When I used
range.s.r = 1;
I was able to change my range to A2 but I could not get my named array of data. Any help is appreciated .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几天的挖掘和大量的反复试验,我得到了我的解决方案,如果他们遇到相同的问题,我将发布我的解决方案。到目前为止,我使用了相同的代码。
原因是它为我提供了文件名,第一张名称和第一行中的值。从那里,我可以确定我的标题行在哪里以及数据行启动的位置。数据行不必是标头后的行,因此我的下一句代码可能是:
结果基于使用以下的值调整的值标题和范围:
我现在可以阅读所有Excel表并制作一个用于数据处理的干净命名数组。希望这对某人有帮助。
而不是我原来的0
After a few days of digging and a lot of trial and error code I got my solution that I will post for others to use if they have the same problem. I used the same code up to this point.
The reason for this is that it gives me the file name, the first sheets name and the value in the first row. From there I can determine where my header row is and where the data row starts. The data row does not have to be the row after the header so my next line of code could be:
The results are based upon the values header and range that have been adjusted using the:
I can now read all of the Excel sheets and make a clean named array for data processing. Hope this helps someone.
rather than my original 0's