CSV import

Match columns, check the rows and confirm. In this example your file stays in your browser.

Choose a file

Use a CSV with column headings, or try the sample.

View markup
<section class="ex-csv" data-ex-csv-import aria-label="CSV import">
  <ol class="ex-csv-progress" aria-label="Import progress" role="list" hidden>
    <li>1. File</li><li>2. Columns</li><li>3. Review</li>
  </ol>
  <div class="ex-csv-choose" data-csv-choose>
    <h3 tabindex="-1">Choose a file</h3>
    <p>Use a CSV with column headings, or try the sample.</p>
    <label class="ex-csv-file"><span class="ex-label">Choose CSV file</span>
      <input type="file" accept=".csv,text/csv" data-csv-file disabled>
    </label>
    <button type="button" class="ex-button ex-button--secondary" data-csv-sample disabled>Use sample CSV</button>
  </div>
  <div data-csv-stage hidden></div>
  <noscript>Enable JavaScript to match CSV columns and review the rows before importing.</noscript>
  <p class="ex-csv-status" data-csv-status role="status" aria-live="polite"></p>
</section>
Use in a project

Use in a project

Copy recipes/csv-import.html from the visual guide. Load csv-import.css after the core styles. Keep csv-import.mjs and csv-data.mjs together, and load the first as a module.

Choose fields and save an import
import { enhanceCsvImport } from "./visual-guide-8/csv-import.mjs";

const root = document.querySelector("[data-ex-csv-import]");
enhanceCsvImport(root, {
  fields: [
    { key: "title", label: "Title", required: true },
    { key: "copies", label: "Copies", type: "integer", min: 0, required: true }
  ],
  onImport: saveImport
});

Supply your own saveImport({rows, signal, importId}). Validate and save the whole batch, and handle retries with the same import ID. Resolve after saving. A rejected promise keeps the corrected rows available.

Custom fields support text, whole numbers, required values, aliases for column headings, and numeric bounds. Pass sampleCsv to provide an example for your schema.

Supports UTF-8 comma-separated files with a header, up to 1 MiB, 500 data rows and 50 columns. Quoted commas, quotes and line breaks are supported. A malformed CSV identifies the line to fix in the original file.

Download visual guide

Setup instructions · Source for AI tools