简体   繁体   English

使用ExcelJS,如何使用URL从excel文件中读取数据?

[英]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.

相关问题 如何使用 Nodejs 从文本文件读取数据并将数据插入 PostgreSQL? - How to read from text file and insert the data into PostgreSQL using Nodejs? 如何使用 spark-java 从 GCS 读取 csv 文件? - How to read csv file from GCS using spark-java? 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 如何使用 (.get) 从 firebase 读取数据(一次读取) - how can i read data from firebase (one time read ) using (.get) 将 csv 文件从 s3 读入 ExcelJS 工作簿 - Reading csv file from s3 into ExcelJS Workbook 无法使用 html 从 firebase 读取数据 - unable to read data from firebase using html 无法使用 nextjs 从 firestore 读取数据 - Failing to read data from firestore using nextjs 如何使用 Python 中的 Pandas 从 s3 存储桶中读取 csv 文件 - How to read a csv file from an s3 bucket using Pandas in Python 如何从 AWS 中的表单数据读取文件 Lambda function - how to read a file from form-Data in an AWS Lambda function 如何使用 excel 或 google 电子表格或 CSV 使用 excel 导入 firebase firestore 数据库中的批量数据 - How to import bulk data in firebase firestore database from excel or google spreadsheet or CSV using flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM