你可以使用ExcelJS库来读取Excel表格中的数据。

huangapple go评论96阅读模式
英文:

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

enter image description here

答案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

你可以使用ExcelJS库来读取Excel表格中的数据。

文件结构

├── src
│   └── read.js
│   └── client.js
└── testData
    └── Full-TestData.xlsx

运行

src 目录下。

node client.js

结果

你可以使用ExcelJS库来读取Excel表格中的数据。

英文:

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

你可以使用ExcelJS库来读取Excel表格中的数据。

File structure

├── src
│   └── read.js
│   └── client.js
└── testData
    └── Full-TestData.xlsx

Run it

at src directory.

node client.js

Result

你可以使用ExcelJS库来读取Excel表格中的数据。

huangapple
  • 本文由 发表于 2023年7月24日 20:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定