简体   繁体   English

如何使用量角器读取 javascript 中的 excel 文件?

[英]How to read excel file in javascript with protractor?

I need to read excel file in Javascript dynamically.我需要动态读取 Javascript 中的 excel 文件。 want to iterate cell values using row counts and column count.想要使用行数和列数来迭代单元格值。 Is it possible to do so?有可能这样做吗? without converting it into JSON object.无需将其转换为 JSON 对象。 However, I revisited many posts in order to resolve the issue @ stackoverflow.但是,我重新访问了许多帖子以解决@stackoverflow 的问题。 but not succeeded.Please suggest some satisfactory solution.但没有成功。请提出一些令人满意的解决方案。

thanks you感谢您

There's a npm package which would perfectly match with your requirements : xlsx .有一个 npm 包可以完全符合您的要求: xlsx If you need exemple of code, feel free to ask, I often use it如果您需要代码示例,请随时询问,我经常使用它

Here is a basic exemple of parsing a xlsx file : I read the value of the cell 'B1' of the first sheet of the file test.xlsx这是解析 xlsx 文件的基本示例:我读取了文件 test.xlsx 第一页的单元格“B1”的值

'use strict';
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheetName = workbook.SheetNames[0];
var worksheet = workbook.Sheets[sheetName];
var redValue = worksheet['B1'];
console.log(redValue); 

我找到了一个使用“kexcel”npm 包读取 excel 的解决方案。我可以用它来完成我的工作。但我卡在了这一点上,我们如何使用它从 excel 中获取最后一行编号。

To read from an excel file you can use 2 different methods,要从 excel 文件中读取,您可以使用 2 种不同的方法,

The first one is using the package xlsx, Inside the project folder run the command 'npm i xlsx' on the terminal which will install the necessary packages第一个是使用包 xlsx,在项目文件夹中在终端上运行命令“npm i xlsx”,它将安装必要的包

var XLSX = require('xlsx');
var UserCred = XLSX.readFile('ExcelFile/UserLogin.xlsx');
var UserLoginWorksheet= UserCred.Sheets['Sheet1'];
var BaseUrl=UserLoginWorksheet['B1'].v;
console.log(BaseUrl);

Here 'BaseUrl' holds the value at column B1 in the excel file这里'BaseUrl'保存excel文件中B1列的值

The second one is using the package exceljs, Inside the project folder run the command 'npm i excel' on the terminal which will install the necessary packages.第二个是使用 exceljs 包,在项目文件夹中在终端上运行命令“npm i excel”,它将安装必要的包。

var Excel = require("exceljs");
 var filePath = 'ExcelFiles/StaffDetails.xlsx';
 var workbook = new Excel.Workbook();
 await workbook.xlsx.readFile(filePath).then(function () {
 //Use sheetName in getWorksheet function
 worksheet = workbook.getWorksheet("Sheet1");
 let cellValue1 = worksheet.getRow(1).getCell(1).value;
 console.log(emptyCell1+"4");
 })
let  cellValue2 = worksheet.getRow(2).getCell(2).value;

Here cellvalue1 holds A1 columns value and cellvalue2 holds B2 column value这里 cellvalue1 保存 A1 列值, cellvalue2 保存 B2 列值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM