Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    • Salesforce Spring ’25 Release: Top Flow Enhancements You Need to Know
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Friday, May 9
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»How to Export Data in Excel with SheetJS in LWC

    How to Export Data in Excel with SheetJS in LWC

    Dhanik Lal SahniBy Dhanik Lal SahniNovember 11, 2024Updated:November 11, 2024No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Export Data in Excel
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Excel exports CRM data in a familiar and flexible format that allows for data manipulation and analysis. CRM systems contain vast amounts of data about customers, leads, sales, and interactions that users need to analyze, transform, or share for decision-making.  Excel sheets offer flexibility when filtering, pivoting, and visualizing data. This helps marketing and sales teams to analyze patterns and make informed decisions. Exporting lead data can help marketers study trends in conversion, segment prospects, or plan targeted campaigns. JS libraries like SheetJS, ExcelJS, PapasParse, FileSaver, and JSXLSX can export data in Excel sheets. This post will provide an implementation of SheetJS in LWC (Lightning Web Component) to export data in Excel Sheets. Most of the steps will be common to other JS library implementations.

    Sometimes, we get the error “Excel cannot open the file because the file format or extension is not valid” when creating an Excel sheet using another method or JS library. Excel sheets created using this library will not cause any such issue.

    Use Case:

    Businesses need Lead information in an Excel sheet. The Phone and Email columns should be highlighted in red colour.

    Steps for SheetJS Integration in Lightning Web Component

    1. Create Apex Class to fetch data
    2. Add SheetJS Library in Salesforce
    3. Create a LWC Component to Export Data
    4. Test the Excel sheet generation

    1. Create Apex Class to fetch Data

    We need to export lead data in an Excel sheet. To do this, we will create an Apex class to get lead information from the Salesforce organization.

    2. Add SheetJS Library in Salesforce

    The SheetJs library can be downloaded from the Github repository. However, we need a pro version to style records in an Excel sheet. The pro version will also support implementation.

    A free version with style support is also available from the repository. I have used this one to handle my use case, but you can use whatever version will handle your use case. Add this library as SheetJS in Static Resource so we can use it in Lightning Web Component.

    Export Data in Excel using SheetJS | SalesforceCodex

    3. Create a LWC Component to Export Data

    Create a Lighting Web Component LeadDataExport, which will use the above uploaded static resource to create an Excel sheet.

    This LWC will load Lead Object Data in the Data table. Once the export button is hit, an Excel sheet with the required data will be created.

    Sheet JS library method usage

    1. json_to_sheet – This will convert the lead data to a worksheet
    2. book_new – It will create a new workbook
    3. book_append_sheet – It will append the sheet to the workbook
    4. writeFile – It will generate an Excel file with the workbook.

    We have to refer to the Excel cell to format Excel sheet data and then apply the style. For example, if we want to make B2 cell data bold, then we will apply the code below.

    ws[`B2''].s = { font: { bold: true }} 

    We have used the same approach in LWC for each cell of the Email and Mobile column.

    4. Test the Excel sheet generation

    Once LWC is created, we can add this component to the lightning page in the required Salesforce Org.

    Export Data in Excel | SalesforceCodex

    References

    • SheetJs Library
    • Export Data from Lightning Web Component to Excel Sheet
    • Download excel cannot open the file because the file format or file extension

    Other Important Posts

    • Ultimate Guide to URL Accessibility in LWC
    • Dynamically Instantiate Components in LWC
    • Generic Notification Component in LWC
    • Reusable Custom Calendar LWC using FullCalendar Js Library
    • Custom Toast with custom duration In LWC
    • Dynamic Interaction Between Two LWCs
    • Sending Wrapper object to Apex from LWC

    Need Help?

    If you need help implementing this feature, connect with me on my LinkedIn profile, Dhanik Lal Sahni.

    apex excel excelsheet lightning lightning component Lightning web component lwc salesforce salesforce apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleBest Practices to Avoid Hardcoding in Apex for Cleaner Salesforce Code
    Next Article How to Manage Technical Debt in Salesforce
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Basic Queueable Chaining in Salesforce Apex

    March 31, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.