[英]Using ExcelJS, how to read data from excel file using URL?
I have excel file upload in cloud.我在云中上传了 excel 文件。 Using node.js when front end pass URL i need to read data from excel file.
当前端通过 URL 时使用 node.js 我需要从 excel 文件中读取数据。 Below is my code,
下面是我的代码,
var workbook = new ExcelJS.Workbook();
workbook.xlsx.load('https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx')
.then(function() {
var worksheet = workbook.getWorksheet(sheet);
worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
});
});
But I'm getting this error,但我收到这个错误,
Can't find end of central directory: is this a zip file?找不到中央目录的结尾:这是一个 zip 文件吗? If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html
如果是,请参见https://stuk.github.io/jszip/documentation/howto/read_zip.html
Can someone help me to fix this?有人可以帮我解决这个问题吗?
Fixed using the below code.使用以下代码修复。
const axios = require("axios");
const XLSX = require("xlsx");
const jsontoxml = require("jsontoxml");
async function testAxiosXlsx(url) {
const options = {
url,
responseType: "arraybuffer"
}
let axiosResponse = await axios(options);
const workbook = XLSX.read(axiosResponse.data);
let worksheets = workbook.SheetNames.map(sheetName => {
return { sheetName, data: XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]) };
});
console.log("json:\n", JSON.stringify(worksheets), "\n\n");
console.log("xml:\n", jsontoxml(worksheets, {}));
}
testAxiosXlsx("https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx");
Look at the documentation .查看文档。
The load
method expects you to pass a Buffer containing Excel data. load
方法期望您传递一个包含 Excel 数据的 Buffer。
You are passing it a string, and that string contains a URL, not Excel data.您向它传递一个字符串,该字符串包含 URL,而不是 Excel 数据。
You need to fetch the data and convert it to a buffer .您需要获取数据并将其转换为缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.