在Reactjs中读取excel文件 [英] Reading excel file in Reactjs
问题描述
在尝试读取Reactjs中的excel文件时,我正在尝试并敲打我的脑袋。
I am trying and banging my head already while trying to read excel file in Reactjs.
我已尝试过多个库,例如 Sheetjs , excel-parser , exceljs 等等(如8-9)库。
I have tried multiple libraries out there like, Sheetjs , excel-parser, exceljs and so on (like 8-9) libraries.
我在每个库中都会遇到奇怪和不同的错误。
I am getting weird and different errors in every library.
例如我正在使用 excel-parser 并收到以下错误
For example i am using excel-parser and getting following error
Module not found: 'child_process'
这是因为它是一个节点模块,不能在浏览器中运行。
That is because it is a node module and won't work in browser.
任何人都知道一些好的和简单的库可以在浏览器中使用 reactjs 吗?
Anyone know some good and easy library that can work with reactjs in browser?
推荐答案
我已经使用 Sheetjs 的npm版 xlsx 成功阅读了excel文件。
I have successfully read excel file using Sheetjs's npm version xlsx.
以下是代码:
import * as XLSX from 'xlsx';
//f = file
var name = f.name;
const reader = new FileReader();
reader.onload = (evt) => { \\evt = on_file_select event
/* Parse data */
const bstr = evt.target.result;
const wb = XLSX.read(bstr, {type:'binary'});
/* Get first worksheet */
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Convert array of arrays */
const data = XLSX.utils.sheet_to_csv(ws, {header:1});
/* Update state */
console.log("Data>>>"+data);
};
reader.readAsBinaryString(f);
这篇关于在Reactjs中读取excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!