英文:
How can I read data from an Excel sheet using ExcelJS library?
问题
它一直报告文件未找到异常。以下是我正在调用以从Excel表格中读取数据的函数:
import excel from 'exceljs'
getDataFromExcel(sheetName, rowNum, colNum) {
//const excel = require('exceljs')
let wb = new excel.Workbook()
wb.xlsx.readFile('../testData/Full-TestData.xlsx').then(() => {
let sheet = wb.getWorksheet(sheetName)
let data = sheet.getRow(rowNum).getCell(colNum).value().toString()
return data
})
}
我正在尝试从Excel表格中读取数据,以在我的脚本中使用它,而不是硬编码,但是我收到了文件未找到的异常。
英文:
It keeps on giving file not found exception. Below is the function which i am calling to read data from the excel sheet:
import excel from 'exceljs'
getDataFromExcel(sheetName,rowNum,colNum){
//const excel = require('exceljs')
let wb = new excel.Workbook()
wb.xlsx.readFile('../testData/Full-TestData.xlsx').then(()=>{
let sheet = wb.getWorksheet(sheetName)
let data = sheet.getRow(rowNum).getCell(cellNum).value().toString()
return data
})
}
I am trying to read data from an excel sheet to use it in my script instead of hard coding however i am getting no file found exception
答案1
得分: 0
这个演示将会运行
保存为 read.js
文件名放在 src
目录下。
function columnName (index) {
var cname = String.fromCharCode(65 + ((index - 1) % 26))
if (index > 26)
cname = String.fromCharCode(64 + (index - 1) / 26) + cname
return cname
}
function getDataFromExcel (sheet, rowNum, colNum) {
cell = columnName(colNum) + String(rowNum)
text = sheet.getCell(cell).text
return text
}
module.exports = { getDataFromExcel, columnName }
保存为 client.js
文件名放在 src
目录下。
const ExcelJS = require('exceljs')
const excel = require("./read");
const fileName = '../testData/Full-TestData.xlsx';
const wb = new ExcelJS.Workbook()
const sheetName = 'contact angle'
wb.xlsx.readFile(fileName).then(() => {
sheet = wb.getWorksheet(sheetName)
// A1
rowNum = 1
colNum = 1
cell = excel.columnName(colNum) + String(rowNum)
text = excel.getDataFromExcel(sheet, rowNum, colNum)
console.log(cell + ": " + text)
// B3
rowNum = 3
colNum = 2
cell = excel.columnName(colNum) + String(rowNum)
text = excel.getDataFromExcel(sheet, rowNum, colNum)
console.log(cell + ": " + text)
}).catch(err => {
console.log(err.message)
})
结果
$ node client.js
A1: SDM
B3: 75.8
安装依赖
在 src
目录下。
npm install exceljs
使用这个 Excel 文件
保存为 Full-TestData.xlsx
文件名放在 testData
目录下。
https://figshare.com/articles/dataset/Test_data_xlsx/22040333
文件结构
├── src
│ └── read.js
│ └── client.js
└── testData
└── Full-TestData.xlsx
运行
在 src
目录下。
node client.js
结果
英文:
This demo will work
Save as read.js
name under src
directory.
function columnName (index) {
var cname = String.fromCharCode(65 + ((index - 1) % 26))
if (index > 26)
cname = String.fromCharCode(64 + (index - 1) / 26) + cname
return cname
}
function getDataFromExcel (sheet, rowNum, colNum) {
cell = columnName(colNum) + String(rowNum)
text = sheet.getCell(cell).text
return text
}
module.exports = { getDataFromExcel, columnName }
Save as client.js
name under src
directory.
const ExcelJS = require('exceljs')
const excel = require("./read");
const fileName = '../testData/Full-TestData.xlsx';
const wb = new ExcelJS.Workbook()
const sheetName = 'contact angle'
wb.xlsx.readFile(fileName).then(() => {
sheet = wb.getWorksheet(sheetName)
// A1
rowNum = 1
colNum = 1
cell = excel.columnName(colNum) + String(rowNum)
text = excel.getDataFromExcel(sheet, rowNum, colNum)
console.log(cell + ": " + text)
// B3
rowNum = 3
colNum = 2
cell = excel.columnName(colNum) + String(rowNum)
text = excel.getDataFromExcel(sheet, rowNum, colNum)
console.log(cell + ": " + text)
}).catch(err => {
console.log(err.message)
})
Result
$ node client.js
A1: SDM
B3: 75.8
Install dependency
At src
directory.
npm install exceljs
Using this excel file
Save as Full-TestData.xlsx
name under testData
directory.
https://figshare.com/articles/dataset/Test_data_xlsx/22040333
File structure
├── src
│   └── read.js
│   └── client.js
└── testData
└── Full-TestData.xlsx
Run it
at src
directory.
node client.js
Result
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论