1. 程式人生 > >在 Node.js 中利用 js-xlsx 處理 Excel 檔案

在 Node.js 中利用 js-xlsx 處理 Excel 檔案

參考連結:http://scarletsky.github.io/2016/01/30/nodejs-process-excel/

var _headers = [''],
    _headers_len = _headers.length;
headers = _headers.map((v, i) => Object.assign({}, {v: v, position: String.fromCharCode(65 + i) + 1}))
    .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v}}), {});

// 為 _headers 新增對應的單元格位置

    // [ { v: 'id', position: 'A1' },

    // { v: 'name', position: 'B1' },

    // { v: 'age', position: 'C1' },

    // { v: 'country', position: 'D1' },

    // { v: 'remark', position: 'E1' } ]


var excelData = {};

var output = Object.assign({}, headers, excelData);
//console.log(output);
// 獲取所有單元格的位置
var outputPos = Object.keys(output);
// 計算出範圍
var ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
var wb = {
    SheetNames: ['mySheet'],
    Sheets: {
        'mySheet': Object.assign({}, output, {'!ref': ref})
    }
};
XLSX.writeFile(wb, 'output.xlsx');