question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to simply export a Worksheet to xlsx?

See original GitHub issue

Hello, I hope this doesn’t send a blast email to everyone. I apologize if it does… I am out of luck and have been Googling this question for a long time and I hope it is okay to ask here:

  1. How would one export a worksheet to xlsx using SheetJS?
  2. The excel sheet would be formatted and then would save on the clients end?

Example: Using SheetJS, how would I take this Sheet (array of objects):

var ws = XLSX.utils.json_to_sheet([{"name":"John"}, {"city":"Seattle"}]);

and store it in an excel sheet with name and city as header and John and Seattle as the row data?

Thanks in advance.

  • Brian

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

247reactions
SheetJSDevcommented, May 17, 2018

There are two issues:

  1. each object is mapped to a row, so if you want a row with name “John” and city “Seattle”, you should combine in one object. For example, here are 3 people:
var data = [
    {"name":"John", "city": "Seattle"},
    {"name":"Mike", "city": "Los Angeles"},
    {"name":"Zach", "city": "New York"}
];
  1. ws is a worksheet there, you need to make a workbook from it. The simplest way is with:
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "WorksheetName");

https://jsfiddle.net/uxnf00z8/ is a complete example for the web browser, starting from the same data and ultimately triggering a download. The library is loaded as an external reference, and the saveAs function is defined in the FileSaver external reference

https://runkit.com/sheetjs/59c5c7dd804721001214aa74 is a complete example of generating an XLSX if you are using node or webpack (you’ll see a “Download ZIP” at the end, download and rename to “output.xlsx” to see the data – this is unfortunately a runkit limitation)

/* external references:
 - https://rawgit.com/SheetJS/js-xlsx/master/dist/xlsx.full.min.js
*/
/* original data */
var data = [
    {"name":"John", "city": "Seattle"},
    {"name":"Mike", "city": "Los Angeles"},
    {"name":"Zach", "city": "New York"}
];

/* this line is only needed if you are not adding a script tag reference */
if(typeof XLSX == 'undefined') XLSX = require('xlsx');

/* make the worksheet */
var ws = XLSX.utils.json_to_sheet(data);

/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");

/* generate an XLSX file */
XLSX.writeFile(wb, "sheetjs.xlsx");
15reactions
F-loatcommented, Nov 9, 2017

I think these methods like XLSX.utils.book_new() and XLSX.utils.book_append_sheet() should be list in README.md. Some key method to export a xlsx file but I can’t find them easily.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Export data to Excel - Microsoft Support
Open the destination Excel workbook, and then display the worksheet that contains the exported data. Right-click a column or a selected range of...
Read more >
How to export and save each worksheet as separate new ...
Step 2: Right click the worksheet name, and click the Move or Copy from context menu. doc export sheets 1. Step 3: In...
Read more >
How to export tables and worksheets to XLSX, TXT, PDF and ...
On XLTools tab, click the Workbook Organizer button arrow to right · From the list of all worksheets, select one, all or just...
Read more >
Fast Export All Worksheets into Their Own Excel File - YouTube
VBA/Macro Course [40% Discount]: https://www.teachexcel.com/vba-course-update.php?src=youtube_v_description_FEmZC2Z8LXMDownloadable File: ...
Read more >
Export tables to xlsx files
To export expss tables to *.xlsx you need to install excellent openxlsx package. To install it just type in the console install.packages("openxlsx") ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found