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
        // (blank lines, such as the one after a trailing newline, are skipped)
        const lines = csv.split(/\r\n|\n/).filter((line) => line.trim() !== '');
        //console.log("#### lines = "+JSON.stringify(lines));
        // parse the first line containing the csv column headers
        const headers = splitCSVLine(lines[0]);
        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. splitCSVLine() keeps quoted values that contain commas together and turns "" into ". 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 68.0 (Winter '27) and exposes the component to App, Home and Record pages.

Because it is teaching code, it still has limits I would fix before using it on real files: a quoted value that spans several lines isn't supported, 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. In October 2026 I moved it to API version 68.0, fixed quoted commas and blank trailing lines, and added Jest tests (npm test, run with @salesforce/sfdx-lwc-jest) that check the parsing without an org. 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 tutorial, How to Build a Simple CSV Parser with Lightning Web Components, walks through the code.

Built by

Avnish Yadav

AI Automation Engineer & Salesforce Developer

Salesforce developer since June 2018. I build AI agents, n8n workflows and Salesforce integrations, and write up what holds up in real use.

About meLinkedIn

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.