Export an Array of JSON Objects to Excel with SheetJS (2024)

Table of Contents
PS. Top comments (0)
Export an Array of JSON Objects to Excel with SheetJS (1)

Export an Array of JSON Objects to Excel with SheetJS (2)

raielly

Posted on • Updated on

Export an Array of JSON Objects to Excel with SheetJS (3) Export an Array of JSON Objects to Excel with SheetJS (4) Export an Array of JSON Objects to Excel with SheetJS (5) Export an Array of JSON Objects to Excel with SheetJS (6) Export an Array of JSON Objects to Excel with SheetJS (7)

In this article, we will demonstrate how to convert JSON response data to an Excel file with defined headers and columns that can be downloaded directly by the client.

To accomplish this, we will utilize the SheetJS plugin.

Let's begin, add a button that users will click to generate an export

<button type="button" id="exportExcel" > Export Excel </button>

then create array of "employees" objects

const employees = [ { id: '1', name: 'Saitama', age: '32' }, { id: '2', name: 'Genos', age: "24" }]

Next create a workbook
XLSX.utils.json_to_sheet generates a worksheet:

const worksheet = XLSX.utils.json_to_sheet(employees);

XLSX.utils.book_new creates a new workbook and XLSX.utils.book_append_sheet appends a worksheet to the workbook. The new worksheet will be called "Empoloyess":

const workbook = XLSX.utils.book_new();XLSX.utils.book_append_sheet(workbook, worksheet, "Employees");

Last export the file
XLSX.writeFile creates a spreadsheet file and tries to write it to the system.

compression: true enables ZIP compression for XLSX and other formats.

XLSX.writeFile(workbook, "Employee Lists.xlsx", { compression: true });

That’s it! this is the simplest code to convert JSON data in XLSX EXCEL file which will download on the client side.

Access functioning demo here

PS.

By default, json_to_sheet creates a worksheet with a header row. In this case, the headers come from the JS object keys: "id", "names" and "age".

Let's create array of custom header

const excelHeader = [ "Employee Id", "Full Name", "Age"]

If we want to fix the header and customize the name we can simply use XLSX.utils.sheet_add_aoa that can write text values to the existing worksheet starting at cell A1:

XLSX.utils.sheet_add_aoa(worksheet, [excelHeader], { origin: "A1" });

And since some names may exceed the default column width, we can adjust the column width by defining the "!cols" worksheet property. For example, the following line sets the width of column A to approximately length of the header + 5

// Map the array and get the length and add 5 to add extra spaces.// Push the value to the defined variable `wscols` // Assign the varialbe `wscols` to `worksheet["!cols"]`let wscols = []excelHeader.map(arr => { wscols.push({ wch: arr.length + 5 })})worksheet["!cols"] = wscols;

Access functioning demo here

Top comments (0)

Subscribe

For further actions, you may consider blocking this person and/or reporting abuse

Export an Array of JSON Objects to Excel with SheetJS (2024)
Top Articles
Confusion as anonymous crypto user sends US-based celebrities ether directly from banned Tornado Cash
Guideline Help Center
Kreme Delite Menu
Mcgeorge Academic Calendar
Chris wragge hi-res stock photography and images - Alamy
Poe Pohx Profile
Think Of As Similar Crossword
Mivf Mdcalc
Publix 147 Coral Way
Globe Position Fault Litter Robot
Jcpenney At Home Associate Kiosk
Sports Clips Plant City
WWE-Heldin Nikki A.S.H. verzückt Fans und Kollegen
Lax Arrivals Volaris
Gon Deer Forum
Suffix With Pent Crossword Clue
Bfg Straap Dead Photo Graphic
Soccer Zone Discount Code
Wal-Mart 140 Supercenter Products
Mission Impossible 7 Showtimes Near Marcus Parkwood Cinema
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
If you bought Canned or Pouched Tuna between June 1, 2011 and July 1, 2015, you may qualify to get cash from class action settlements totaling $152.2 million
Hobby Stores Near Me Now
Kringloopwinkel Second Sale Roosendaal - Leemstraat 4e
Ivegore Machete Mutolation
Greenville Sc Greyhound
Munis Self Service Brockton
When Does Subway Open And Close
TeamNet | Agilio Software
Evil Dead Rise Showtimes Near Sierra Vista Cinemas 16
Cona Physical Therapy
Bfsfcu Truecar
Big Boobs Indian Photos
Star News Mugshots
Fbsm Greenville Sc
Mbi Auto Discount Code
Reli Stocktwits
Tamil Play.com
Moxfield Deck Builder
Miracle Shoes Ff6
Top 25 E-Commerce Companies Using FedEx
Thelemagick Library - The New Comment to Liber AL vel Legis
Craigslist En Brownsville Texas
Dwc Qme Database
Alba Baptista Bikini, Ethnicity, Marriage, Wedding, Father, Shower, Nazi
Holzer Athena Portal
From Grindr to Scruff: The best dating apps for gay, bi, and queer men in 2024
Vcuapi
Bones And All Showtimes Near Emagine Canton
Ihop Deliver
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6572

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.