Sheetjs: Newbie to js-xlsx - JSON to xlsx conversion with using angular

Created on 24 May 2017  路  10Comments  路  Source: SheetJS/sheetjs

Anyone can guide me how to convert json to xlsx with using angular?

Here is sample json format:
[ { "agentNo":"324234", "subName":"30, Jul 2013 09:24 AM", "Location":"Singapore", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"444443", "subName":"30, Jul 2013 09:24 AM", "Location":"Malaysia", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"2342234", "subName":"30, Jul 2013 09:24 AM", "Location":"India", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" } ]

Here is xlsx sample that need to be generated:
screen shot 2017-05-24 at 6 16 18 pm

Thanks in advance

Operations Style

Most helpful comment

The new sheet_add_json function can add to an existing worksheet:

var ws = XLSX.utils.aoa_to_sheet([
  ["Member Listing"],
  ["Company Name:", "<Master Policy Holder Name>"],
  ["Report Date:", "<DD/MM/YYYY>"]
]);
XLSX.utils.sheet_add_json(ws, [ { "agentNo":"324234", "subName":"30, Jul 2013 09:24 AM", "Location":"Singapore", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"444443", "subName":"30, Jul 2013 09:24 AM", "Location":"Malaysia", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"2342234", "subName":"30, Jul 2013 09:24 AM", "Location":"India", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" } ],{origin:-1});

To control the order of the fields, pass a header option to sheet_add_json:

XLSX.utils.sheet_add_json(ws, [ ... ], {header: ["agentNo", "subName", "memberName", "certNo", "Gender", "dob"], origin:-1});

And to rewrite the column headers, if the objects don't have the same name as the titles you want, go back and rewrite a new header array in the fourth row (index 3):

XLSX.utils.sheet_add_aoa(ws, [["Agent no", "Subsidiary name", "Member Name", "Cert No", "Gender", "Date of Birth"]], {origin:3});

All 10 comments

@samuelkavin see my answer to your comment on #610

@jomel, thank you for response. All done. Now, I stuck with settting up the title of the excel file which is Member Listing, Company Name and Report Date on A1, A2 and A3 cell. Any idea?

@martinnov92, is it possible insert custom 3 row before header? something like this:
screen shot 2017-05-24 at 6 16 18 pm

Hello @samuelkavin I really don麓t know this, sorry

I added an example on exporting a xlsx file from an array of jsons on Angular2/4:

https://github.com/bogdancar/xlsx-json-to-xlsx-demo-Angular2

@bogdancar your demo isnt avaialble

@indunilw Sorry, fixed the link.

@samuelkavin @jomel did you solve the custom header portion of the sheet, where you want common data for the table in a top section, is this possible? the memberListing portion in the OPs example

The new sheet_add_json function can add to an existing worksheet:

var ws = XLSX.utils.aoa_to_sheet([
  ["Member Listing"],
  ["Company Name:", "<Master Policy Holder Name>"],
  ["Report Date:", "<DD/MM/YYYY>"]
]);
XLSX.utils.sheet_add_json(ws, [ { "agentNo":"324234", "subName":"30, Jul 2013 09:24 AM", "Location":"Singapore", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"444443", "subName":"30, Jul 2013 09:24 AM", "Location":"Malaysia", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" }, { "agentNo":"2342234", "subName":"30, Jul 2013 09:24 AM", "Location":"India", "memberName":42, "certNo": "2342234", "Gender":"Male", "dob":"1 may 1987" } ],{origin:-1});

To control the order of the fields, pass a header option to sheet_add_json:

XLSX.utils.sheet_add_json(ws, [ ... ], {header: ["agentNo", "subName", "memberName", "certNo", "Gender", "dob"], origin:-1});

And to rewrite the column headers, if the objects don't have the same name as the titles you want, go back and rewrite a new header array in the fourth row (index 3):

XLSX.utils.sheet_add_aoa(ws, [["Agent no", "Subsidiary name", "Member Name", "Cert No", "Gender", "Date of Birth"]], {origin:3});

@SheetJSDev OMG! Amazing! This makes it SO much easier. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eyalcohen4 picture eyalcohen4  路  3Comments

thomasledoux1 picture thomasledoux1  路  3Comments

gustavosimil picture gustavosimil  路  3Comments

upasana-shah picture upasana-shah  路  4Comments

DannyRyman picture DannyRyman  路  3Comments