Skip to main content
← Back to Projects
Salesforce Historical Lightning Web Component

CSV Parser with LWC

Preview a CSV file inside Salesforce with one Lightning Web Component

A small Lightning Web Component that reads a CSV file in the browser and shows it in a datatable. Tutorial code from January 2023, no Apex needed.

Solo build, companion to a blog tutorial

LWCSalesforceJavaScriptCSVTutorial
01 / Problem

The problem

Sometimes you want to look at a CSV file inside Salesforce before deciding what to do with it, without opening the import wizard or writing server code. In January 2023 I wrote a tutorial on the old version of this blog showing how to do that with a Lightning Web Component, and this repo is its companion code.

02 / Solution

What I built

One Lightning Web Component, createCSVTotable. It shows a card titled "CSV To Datatable" with a file picker that only accepts .csv files. When you pick a file, the component reads it in the browser, turns the first line into table columns and every other line into a row, and shows the result in a lightning-datatable. It only displays the data; it does not create or update records.

03 / Demo

See it running

Demo coming

A recorded walkthrough of CSV Parser with LWC is on the way. Until then, the write-up and the diagram below show how it works.

04 / Architecture

How it fits together

CSV Parser with LWC

From CSV file to datatable, all in the browser

  1. CSV filePicked by the userlightning-input.csv only
    onchange
  2. FileReaderBrowser API, inside the LWCreadAsTextPromise
    text
  3. parseCSV()Component JavaScriptHeader to columnsLines to rows
    columns + data
  4. Datatablelightning-datatable on a Lightning pageRead-onlyNo checkbox column
Runs in the browser on a Lightning page; nothing is saved to Salesforce records.
05 / Write-up

How it works

The component takes the first selected file and reads it with the browser's FileReader.readAsText, wrapped in a Promise so the code can await it. Then parseCSV() does the rest:

    parseCSV(csv) {
        // parse the csv file and treat each line as one item of an array
        const lines = csv.split(/\r\n|\n/);
        //console.log("#### lines = "+JSON.stringify(lines));
        // parse the first line containing the csv column headers
        const headers = lines[0].split(',');
        console.log("#### headers = "+JSON.stringify(headers));
        // iterate through csv headers and transform them to column format supported by the datatable
        this.columns = headers.map((header) => {
          return { label: header, fieldName: header };
        });

Full file: createCSVTotable/createCSVTotable.js. Each later line becomes an object keyed by those headers, and the array is bound to the datatable. There is no Apex, no @wire and no third-party CSV library. The metadata file targets API version 55.0 and exposes the component to App, Home and Record pages.

Because it is teaching code, it has limits I would fix before using it on real files: it splits on every comma, so quoted values that contain commas break the columns; a trailing newline adds an empty row; the datatable's key field is Id; and read errors are caught but never shown.

Stack: Lightning Web Components, JavaScript, the FileReader API, lightning-datatable.

Status and links

This is tutorial code from January 2023 and I have not changed it since. The repo holds only the component folder, so to try it you copy createCSVTotable/ into the force-app/main/default/lwc/ folder of an SFDX project, deploy it with the Salesforce CLI and add it to a Lightning page. The original tutorial, "How to Build a Simple CSV Parser with Lightning Web Components", is being restored on this site and updated for the current API version.

Work with me

Need something like this built?

I build AI automations, agents and Salesforce solutions for teams. Tell me what you want to automate and I’ll tell you honestly whether it’s a fit.

Share
Discussion

Comments

Loading comments...

Add a comment

Comments are reviewed before they appear.