NodeJS 节点xlsx模块获取excel文件的标题

rjjhvcjd  于 2023-02-03  发布在  Node.js
关注(0)|答案(3)|浏览(184)

如何在节点xlsx(https://www.npmjs.com/package/xlsx)模块中获取给定Excel文件的表头?

jslywgbw

jslywgbw1#

在xlsx v0.16.9中执行以下操作

const workbookHeaders = xlsx.readFile(filePath, { sheetRows: 1 });
const columnsArray = xlsx.utils.sheet_to_json(workbookHeaders.Sheets[sheetName], { header: 1 })[0];
0pizxfdo

0pizxfdo2#

正如我所发现的那样,没有公开的方法可以从模块中获取Excel文件的标题,所以我从源代码中复制了一些函数(完全尊重author. https://github.com/SheetJS/js-xlsx),并做了一些修改。

function getHeaders(sheet){
    var header=0, offset = 1;
    var hdr=[];
    var o = {};
    if (sheet == null || sheet["!ref"] == null) return [];
    var range = o.range !== undefined ? o.range : sheet["!ref"];
    var r;
    if (o.header === 1) header = 1;
    else if (o.header === "A") header = 2;
    else if (Array.isArray(o.header)) header = 3;
    switch (typeof range) {
        case 'string':
            r = safe_decode_range(range);
            break;
        case 'number':
            r = safe_decode_range(sheet["!ref"]);
            r.s.r = range;
            break;
        default:
            r = range;
    }
    if (header > 0) offset = 0;
    var rr = XLSX.utils.encode_row(r.s.r);
    var cols = new Array(r.e.c - r.s.c + 1);
    for (var C = r.s.c; C <= r.e.c; ++C) {
        cols[C] = XLSX.utils.encode_col(C);
        var val = sheet[cols[C] + rr];
        switch (header) {
            case 1:
                hdr.push(C);
                break;
            case 2:
                hdr.push(cols[C]);
                break;
            case 3:
                hdr.push(o.header[C - r.s.c]);
                break;
            default:
                if (val === undefined) continue;
                hdr.push(XLSX.utils.format_cell(val));
        }
    }
    return hdr;
}


function safe_decode_range(range) {
    var o = {s:{c:0,r:0},e:{c:0,r:0}};
    var idx = 0, i = 0, cc = 0;
    var len = range.length;
    for(idx = 0; i < len; ++i) {
        if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
        idx = 26*idx + cc;
    }
    o.s.c = --idx;

    for(idx = 0; i < len; ++i) {
        if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
        idx = 10*idx + cc;
    }
    o.s.r = --idx;

    if(i === len || range.charCodeAt(++i) === 58) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }

    for(idx = 0; i != len; ++i) {
        if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
        idx = 26*idx + cc;
    }
    o.e.c = --idx;

    for(idx = 0; i != len; ++i) {
        if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
        idx = 10*idx + cc;
    }
    o.e.r = --idx;
    return o;
}

通过传递Work Sheet调用getHeaders函数将返回excel工作表的标题数组。

dfty9e19

dfty9e193#

看看这里:https://www.npmjs.com/package/xlsx,如果我们浏览文档的话。它说我们需要传递选项By default, sheet_to_json scans the first row and uses the values as headers. With the header: 1 option, the function exports an array of arrays of values.
整个代码如下所示:

const data = e.target.result;
  const workbook = XLSX.read(data, { type: "array" });
  console.log(workbook);
  const firstSheetName = workbook.SheetNames[0];
  const worksheet = workbook.Sheets[firstSheetName];
  const options = { header: 1 };
  const sheetData2 = XLSX.utils.sheet_to_json(worksheet, options);
  const header = sheetData2.shift();
  console.log(header); //you should get your header right here

其中e.target来自您的输入。

相关问题