Load and save CSV file

Importing CSV data

Use PapaParse to load remote CSV data. PapaParse loads your data and converts it to an array of objects that is compatible with DataGridXL.

Hit the Browse-button to load one of your local CSV-files into the sheet. If you don't have a CSV-file lying around, you can download this mock-data first, then load it in to the grid.

Code

<style>
#grid {
  height: 400px;
}

#toolbar {
  background: var(--tableodd);
  margin-bottom: 12px;
  padding: 8px;

  display: flex;
  justify-content: space-between;
}
</style>

<div id="toolbar" class="dgxl-app">
  <div>
    <input type="file" id="csv-file" name="files-csv" class="custom-file-input" accept=".csv"><label for="csv-file">Browse…</label>
  </div>
  <div>
    <button class="button" id="button-save">Save</button>
  </div>
</div>

<div id="grid"></div>

<script src="https://code.datagridxl.com/datagridxl2.js"></script>
<!-- Papa Parse (to import and parse CSV files) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js"></script>
<script>
var csvFile = document.getElementById("csv-file");

// create empty grid
var grid = new DataGridXL("grid", {
  data: DataGridXL.createEmptyData(20,20)
});

csvFile.onchange = function(e){
  var file = e.target.files[0];

    Papa.parse(file, {
      header: true,
      dynamicTyping: true,
      complete: function(results) {

        // overwrite grid with new data

        grid = new DataGridXL("grid", {
          data: results.data
        });

      }
    });

}

document.getElementById("button-save").onclick = function(){
  grid.downloadDataAsCSV();
}
</script>

Leave email to receive latest updates!