Skip to main content

CSV and Text

CSV is a common format for data interchange. Spreadsheet applications such as Excel also support other delimiter-separated formats including "Text" (using a tab character as the field separator).

The general write functions (XLSX.write and XLSX.writeFile) support csv and txt (for CSV and tab-separated values respectively), but the specific utility functions generate raw JS strings for further processing.

Live Demo

After choosing a file, the demo will print the data from each worksheet:

Live Editor

Delimiter-Separated Output

Export worksheet data in CSV, TSV, or other delimiter-separated format

var csv = XLSX.utils.sheet_to_csv(ws, opts);

As an alternative to the writeFile CSV type, XLSX.utils.sheet_to_csv also produces CSV output. The function takes an options argument:

Option NameDefaultDescription
FS",""Field Separator" delimiter between fields
RS"\n""Record Separator" delimiter between rows
dateNFFMT 14Use specified date format in string output
stripfalseRemove trailing field separators in each record **
blankrowstrueInclude blank lines in the CSV output
skipHiddenfalseSkips hidden rows/columns in the CSV output
forceQuotesfalseForce quotes around fields
  • strip will remove trailing commas from each line under default FS/RS
  • blankrows must be set to false to skip blank lines.
  • Fields containing the record or field separator will automatically be wrapped in double quotes; forceQuotes forces all cells to be wrapped in quotes.

The following example shows FS and RS options:

Live Editor

CSV Output

Export worksheet data in "Comma-Separated Values" (CSV)

var csv = XLSX.utils.sheet_to_csv(ws, opts);

sheet_to_csv uses the comma character as the field separator by default. This utility function mirrors the csv book type in XLSX.write or XLSX.writeFile.

sheet_to_csv always returns a JS string and always omits byte-order marks.

UTF-16 Text Output

Export worksheet data in "UTF-16 Text" or Tab-Separated Values (TSV)

var txt = XLSX.utils.sheet_to_txt(ws, opts);

sheet_to_txt uses the tab character as the field separator. This utility function matches the txt book type in XLSX.write or XLSX.writeFile.

If encoding support is available, the output will be encoded in CP1200 and the UTF-16 BOM will be added. If encoding support is not available, the output will be encoded as a standard string.

XLSX.utils.sheet_to_txt takes the same arguments as sheet_to_csv.