SheetJS: Do Not Include Headers In json_to_sheet

有些话、适合烂在心里 提交于 2021-01-05 07:36:10

问题


The SheetJS documentation shows a way to take a JSON Object and convert it to an Excel sheet. From their example:

var ws = XLSX.utils.json_to_sheet([
  {S:1,h:2,e:3,e_1:4,t:5,J:6,S_1:7},
  {S:2,h:3,e:4,e_1:5,t:6,J:7,S_1:8}
], {header:["S","h","e","e_1","t","J","S_1"]});

By default, the header information is Object.keys.

The output looks like this in excel:

My question: how do I leave out the header when converting from Json_to_sheet? I do not want the header in my output, only the numbers in the order of Object.keys.


回答1:


Please find the updated answer below:

if(typeof XLSX == 'undefined') XLSX = require('xlsx');
var wb = XLSX.utils.book_new();

var ws = XLSX.utils.json_to_sheet([
  {S:1,h:2,e:3,e_1:4,t:5,J:6,S_1:7},
  {S:2,h:3,e:4,e_1:5,t:6,J:7,S_1:8}
], {skipHeader: 1});

XLSX.utils.book_append_sheet(wb, ws, "No Header");

var wbout = XLSX.write(wb, {bookType:'xlsx', type:'array'});
saveAs(new Blob([wbout],{type:"application/octet-stream"}), "test.xlsx");

Here we can use skipHeader option inorder to skip the JSON Key values to default as a column name.



来源:https://stackoverflow.com/questions/47424755/sheetjs-do-not-include-headers-in-json-to-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!