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
Baker insurance - what kind and at what cost - SBCoverage.com
Types of Insurance Coverage Commercial Trucks Need in Texas
Where To Go After Howling Pit Code Vein
Shoe Game Lit Svg
Midflorida Overnight Payoff Address
Rainfall Map Oklahoma
Cube Combination Wiki Roblox
Www.paystubportal.com/7-11 Login
Craigslist Pets Longview Tx
Bowlero (BOWL) Earnings Date and Reports 2024
Unlv Mid Semester Classes
Po Box 35691 Canton Oh
Walgreens San Pedro And Hildebrand
Grimes County Busted Newspaper
Qhc Learning
Teen Vogue Video Series
Bethel Eportal
A Cup of Cozy – Podcast
Wnem Tv5 Obituaries
How to Make Ghee - How We Flourish
Everything To Know About N Scale Model Trains - My Hobby Models
Restored Republic June 16 2023
Roanoke Skipthegames Com
Claio Rotisserie Menu
Xpanas Indo
Kleinerer: in Sinntal | markt.de
Sam's Club Gas Price Hilliard
Otis Offender Michigan
Ellafeet.official
Grandstand 13 Fenway
Miss America Voy Board
Gyeon Jahee
Wow Quest Encroaching Heat
Ny Post Front Page Cover Today
Cross-Border Share Swaps Made Easier Through Amendments to India’s Foreign Exchange Regulations - Transatlantic Law International
Hisense Ht5021Kp Manual
Quake Awakening Fragments
Mixer grinder buying guide: Everything you need to know before choosing between a traditional and bullet mixer grinder
Flags Half Staff Today Wisconsin
Fetus Munchers 1 & 2
Entry of the Globbots - 20th Century Electro​-​Synthesis, Avant Garde & Experimental Music 02;31,​07 - Volume II, by Various
Home Auctions - Real Estate Auctions
Promo Code Blackout Bingo 2023
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
Atu Bookstore Ozark
Dagelijkse hooikoortsradar: deze pollen zitten nu in de lucht
Random Warzone 2 Loadout Generator
View From My Seat Madison Square Garden
Home | General Store and Gas Station | Cressman's General Store | California
Grace Charis Shagmag
라이키 유출
Bumgarner Funeral Home Troy Nc Obituaries
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.