Node exceljs reading file
因此,根据官方文档,我应该能够使用以下方法读取Excel文档:
1 2 3 4 5 6 7 8 9 10 | // read from a file var workbook = new Excel.Workbook(); workbook.xlsx.readFile(filename) .then(function() { // use workbook }); // pipe from stream var workbook = new Excel.Workbook(); stream.pipe(workbook.xlsx.createInputStream()); |
我有以下文件:
我需要做的基本上是将每一行加载到一个对象中:
1 | var excelObject = {competence1: '', competence2: ''} |
然后将其保存到数组中。
但是,文档并没有给我更多有关如何从此文件中读取内容的信息。 它使用一个名为
有谁知道插件,知道我如何实现我的目标吗?
1 2 3 4 5 6 7 8 | var workbook = new Excel.Workbook(); workbook.xlsx.readFile(filename) .then(function() { var worksheet = workbook.getWorksheet(sheet); worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) { console.log("Row" + rowNumber +" =" + JSON.stringify(row.values)); }); }); |
并且如果您将文件用作ArrayBuffer(例如,在文件上使用FileReader.readAsArrayBuffer()在客户端读取了文件),则要使其与此lib一起加载,您必须执行下一个操作:
1 2 3 4 5 6 7 | let workbook = new Excel.Workbook(); let stream = new Stream.Readable(); stream.push(file); // file is ArrayBuffer variable stream.push(null); workbook.xlsx.read(stream).then((workbook)=> { // get worksheet, read rows, etc }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //Read a file var workbook = new Excel.Workbook(); workbook.xlsx.readFile("data/Sample.xlsx").then(function () { //Get sheet by Name var worksheet=workbook.getWorksheet('Sheet1'); //Get Lastrow var row = worksheet.lastRow //Update a cell row.getCell(1).value = 5; row.commit(); //Save the workbook return workbook.xlsx.writeFile("data/Sample.xlsx"); }); |
以下代码段将帮助您阅读文件sample.xlsx,其中包含两列:1.用户名2.密码。 我正在尝试与Mocha(chai)断言,以检查数据类型是否匹配。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | var Excel = require('exceljs'); const assert = require("chai").assert; var workbook = new Excel.Workbook(); workbook.creator ="Naveen"; workbook.modified ="Kumar"; workbook.xlsx.readFile("sample.xlsx").then(function(){ var workSheet = workbook.getWorksheet("one"); workSheet.eachRow({ includeEmpty: true }, function(row, rowNumber) { currRow = workSheet.getRow(rowNumber); console.log("User Name :" + currRow.getCell(1).value +", Password :" +currRow.getCell(2).value); console.log("User Name :" + row.values[1] +", Password :" + row.values[2] ); assert.equal(currRow.getCell(2).type, Excel.ValueType.Number); // console.log("Row" + rowNumber +" =" + JSON.stringify(row.values)); }); }) |
希望这对某人有帮助
.xls文件出现错误。 在尝试了所有排列和组合之后,我将.xls文件转换为.xlsx并成功运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | const Excel = require('exceljs/dist/es5'); const readExcel = async () => { //You can use the absolute path also let filepath = './genovaFile.xlsx'; let workbook = new Excel.Workbook(); await workbook.xlsx.readFile(filepath); //You can use the index number also eg. 0 for selecting the sheet let worksheet = workbook.getWorksheet("sheet1"); worksheet.eachRow({ includeEmpty: true }, async (row, rowNumber) => { console.log("row" + row.values); }) } |
步骤1:使用npm下载exceljs模块
步骤2:使用以下代码从Excel读取数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import { Workbook, Worksheet } from 'exceljs'; public static async getExcelD(filename: string, column1Item: string) { let wb:Workbook = new Workbook(); let datafile = path.join(__dirname,"../testData", filename); await wb.xlsx.readFile(datafile).then(async ()=>{ let sh:Worksheet = wb.getWorksheet("Sheet1"); for(let i=1;i<=sh.actualRowCount;i++){ if(sh.getRow(i).getCell(1).value==column1Item){ data1 = sh.getRow(i).getCell(2).value; } } }) return await data1; } |