Skip to content

Repository files navigation

csv4xls

Convert a 2D array to xls-compatible CSV, TSV, or XLSX file.

Usage

include csv4xls script, and use:

// always catch exception in case XLSX fails to convert.
try {
// Default: XLSX
ret = csv4xls.toBlob([[1,2,3],[4,5,6]])
// Tab-separated (TSV) / comma-separated (CSV)
ret = csv4xls.toBlob([[1,2,3],[4,5,6]], {format: 'tsv'})
ret = csv4xls.toBlob([[1,2,3],[4,5,6]], {format: 'csv'})
// XLSX (default; uses the builtin 0-dependency writer)
ret = csv4xls.toBlob([[1,2,3],[4,5,6]], {format: 'xlsx'})
// XLSX keeping native number / boolean types
ret = csv4xls.toBlob([[1,2,3],[4,5,6]], {format: 'xlsx', forceText: false})
// XLSX via SheetJS instead of the builtin writer (requires global XLSX)
ret = csv4xls.toBlob([[1,2,3],[4,5,6]], {format: 'xlsx', engine: 'sheetjs'})
// Download with options
csv4xls.download([[1,2,3],[4,5,6]], "mydata") // Will download as mydata.xlsx
csv4xls.download([[1,2,3],[4,5,6]], "mydata", {format: 'csv'}) // Will download as mydata.csv
csv4xls.download([[1,2,3],[4,5,6]], "mydata", {format: 'tsv'}) // Will download as mydata.tsv
} catch(e) {
}

API

csv4xls provides following APIs:

  • toString(data, delimiter = '\t') - convert given 2D array to CSV/TSV in String format.
  • toArray(data, delimiter = '\t') - convert given 2D array to an xls-compatible CSV/TSV file in the returned Uint8Array.
  • toXlsx(data, options = {forceText: true, sheetName: 'Sheet1', engine: 'builtin'}) - convert given 2D array to xlsx file bytes in a Uint8Array.
    • Uses the builtin, dependency-free writer. Pass engine: 'sheetjs' to use the global XLSX (SheetJS) library instead.
  • toWorkbook(data, options = {forceText: false}) - convert given 2D array to a SheetJS workbook object (requires XLSX library).
  • toBlob(data, options = {delimiter: '\t', format: 'auto', forceText: true}) - convert data to blob with specified format.
    • If format is 'xlsx', returns XLSX blob
    • If format is 'html', returns HTML table as blob with MIME type text/html
    • If format is 'xls-html', returns HTML table with BOM as blob with MIME type application/vnd.ms-excel
      • Uses mso-number-format:'\@' style to prevent Excel from changing formats
    • If format is 'auto' (default), returns XLSX. Use 'csv' / 'tsv' for delimiter-separated output
    • HTML options can be passed via options.html object (see toHtml for available options)
    • If forceText is true (default for XLSX), all cells are written as text so Excel won't reinterpret values like "00123". Set to false to keep native number / boolean types
    • options.sheetName sets the worksheet name (default 'Sheet1')
  • toHref(data, options = {delimiter: '\t', format: 'auto', forceText: true}) - same as toBlob but return a corresponding object url.
  • toHtml(data, options = {}) - convert given 2D array to HTML table format.
    • options.tableClass - CSS class for the table (default: 'csv4xls-table')
    • options.cellStyle - Whether to apply mso-number-format:'\@' style to cells to prevent Excel from changing formats (default: true)
    • options.headerRow - Whether to treat the first row as a header row using <th> tags (default: false)
  • download(data, name = "data", options = {delimiter: '\t', format: 'auto', forceText: true}) - trigger file download
    • If format is 'xlsx', file extension will be .xlsx
    • If format is 'html', file extension will be .html
    • If format is 'xls-html', file extension will be .xls (HTML with BOM that Excel can open)
    • If format is 'auto' (default), uses XLSX
    • If using CSV/TSV: delimiter ','.csv, delimiter '\t'.tsv

Limitation

  • CSV/TSV format only works with Excel if file is opened directly. Doesn't work when importing with text import wizard.
  • Default format is xlsx. For CSV/TSV output, set format (or delimiter) explicitly; default delimiter is tab ('\t').
  • File extension and MIME type are automatically set based on the format and delimiter:
    • XLSX → .xlsx with MIME type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    • Comma (,) → .csv with MIME type text/csv
    • Tab (\t) → .tsv with MIME type text/tab-separated-values
  • XLSX output is generated by a builtin writer with zero dependencies. It writes a minimal package (inline strings, no styling, STORE-mode zip), which Excel opens directly.
  • SheetJS is only used when engine: 'sheetjs' is given explicitly, in which case the global XLSX must be available.
  • When format is set to 'auto' (default), XLSX is used.

Note

When using in Nodejs environment, version 18+ is required for Blob support which is used in toBlob function. Also, download api trigger download via link clicking thus it's also a frontend-only feature.

License

MIT

About

Convert a 2D array to xls-compatible CSV file.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages