JavaScript csv to spreadsheet (xlsx) converter
npm install netas-csv2xlsx Csv2Xlsx.convertCsv(csvUrl, filename, metaData, charset, updateFn, returnAsLink, csvSeparator, formatCodes)
@param {String} csvUrl URL to fetch the csv from
@param {String} filename of the generated file
@param {Object} metaData metaData of the generated xlsx
> .title
> .subject
> .creator
> .company
> .lastModifiedBy
> .created (Date object, default to now)
> .modified (Date object, default to now)
@param {String} charset charset of CSV, default to Windows-1252
@param {Function} updateFn you can set a callback function to receive status informations.
@param {Bool} returnAsLink false, to return a Blob url as string instead of a element
@param {String} csvSeparator csv separator, null for auto
@param {Object} formatCodes excel format codes. defaults to
> .date: 'dd.mm.yyyy'
> .datetime: 'dd.mm.yyyy hh:mm'
> .number: '0'
> .float: '0.0'
> .percentage: '0%'
@return {Promise} which resolves to {String|Element} Blob URL or Element depending on {returnAsLink}, default to String
javascript
async function downloadAsExcel() {
const cvrt = await import('src/Csv2Xlsx.js'); // Meta-Data of the xlsx
const metaData = {
title: 'My Demo Spreadsheet',
subject: 'A Demo for CSV to XLSX',
creator: 'John Doe',
company: 'Super Doe Ltd.',
lastModifiedBy: 'John Doe',
created: new Date(2020, 0, 1, 11, 21),
modified: null // now
};
// download the csv and convert it to a excel file
const aTag = await cvrt.Csv2Xlsx.convertCsv('my/demo/data.csv', 'mynewfilename.xlsx', metaData);
// return value is a element, add it to the DOM to start the download
document.body.appendChild(aTag);
// start the download automatically
aTag.click();
}
``