如何使用 js-xlsx 遍历 XLSX 行
此代码片段允许你使用 js-xlsx 库轻松遍历任何 XLSX 文件中的行(在此示例中我们不遍历所有列,而是仅获取 B 列作为示例):
iterate_xlsx_rows.js
const table = XLSX.readFile('mytable.xlsx');
const sheet = table.Sheets[table.SheetNames[0]];
var range = XLSX.utils.decode_range(sheet['!ref']);
for (let rowNum = range.s.r; rowNum <= range.e.r; rowNum++) {
// 示例:获取每行中的第二个单元格,即列 "B"
const secondCell = sheet[XLSX.utils.encode_cell({r: rowNum, c: 1})];
// 注意:如果 secondCell 不存在(即如果它为空),则为 undefined
console.log(secondCell); // secondCell.v 包含值,即字符串或数字
}Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow