21

因此,根据官方文档,我应该能够使用以下命令阅读 excel 文档:

    // 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());

我有以下文件:

在此处输入图像描述

我需要做的基本上是将每一行加载到一个对象中:

var excelObject = {competence1: '', competence2: ''}

然后将其保存到数组中。

然而,文档并没有给我更多关于如何从这个文件中读取的信息。它使用了一个变量,stream但是这个变量没有在任何地方解释。

有谁知道这个插件并且知道我如何实现我的目标?

4

6 回答 6

27
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));
        });
    });
于 2015-11-06T10:18:07.990 回答
7

如果您使用文件作为 ArrayBuffer(例如文件是在客户端上使用 FileReader.readAsArrayBuffer() 读取的),那么要使用此库加载它,您必须执行以下操作:

    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
    });
于 2016-04-09T00:37:50.703 回答
5

下面的代码片段将帮助您阅读文件 sample.xlsx,其中包含两列 1. 用户名 2. 密码。我正在尝试使用 Mocha(chai) 进行断言,以检查数据类型匹配。

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));
      });
})

希望这可以帮助某人

于 2018-04-16T12:23:06.227 回答
4
//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");

});
于 2018-07-03T02:46:32.780 回答
1

我收到 .xls 文件的错误。在尝试了所有的排列和组合之后,我将 .xls 文件转换为 .xlsx 并且它起作用了。

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);

   })

}
于 2019-12-27T14:18:36.093 回答
0

Step1:使用npm下载exceljs模块

Step2:使用以下代码从excel中读取数据

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;
}
于 2019-11-06T08:15:58.280 回答