Convert JSON Array to Excel with Sheetjs

Original link: https://www.ixiqin.com/2022/08/04/using-sheetjs-converting-the-json-array-to-excel/

34b7bb4c9bd5a732b8cef70181b859a7.jpg

In Using node-excel-stream to process Excel data row by row , I mentioned that node-excel-stream is a good choice if you want to simply do Excel reading and processing. Conversely, if you want to export JSON Array to Excel, Sheetjs is a good choice.

Notice

Sheetjs is different from exceljs, distinguishing between the commercial version and the community version. We are using the community edition Sheetjs CE here

usage

When using Sheetjs to export data, you only need to call json_to_sheet in the XLSX method to turn your JSON Array into a worksheet, and then you only need to put it into a new workbook and export it as a file. JS data can be exported to Excel.

 const XLSX = require("xlsx");const data = [ { ... }, ...]const worksheet = XLSX.utils.json_to_sheet(data);const workbook = XLSX.utils.book_new();XLSX.utils.book_append_sheet(workbook, worksheet, "sheetNameIsFirst");XLSX.writeFile(workbook, "output.xlsx");

This article is reprinted from: https://www.ixiqin.com/2022/08/04/using-sheetjs-converting-the-json-array-to-excel/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment