csv-writer
csv-writer copied to clipboard
add support for bom
For special characters like ÄÖÜ in german language it requires to set a BOM in utf-8 export to have a correct import in programs like MS Excel.
Is it possible to add an parameter to add an bom. Work arround is:
csvWriter.writeRecords(dataExportCsv).then(() => {
fs.writeFileSync('export.csv', '\ufeff' + fs.readFileSync('export.csv'));
});
Hi @MonksterFX ,
It seems the original project is abandoned, so I decided to fork it.
In my fork, I've added a useBom parameter to address this issue. I'm also working on solving other issues and incorporating unmerged PRs from the original project.
You can check out the forked project here:
- GitHub: csv-writer-portable
- npm: csv-writer-portable
Here's an example of how to use the useBom parameter:
import { createObjectCsvWriter } from 'csv-writer-portable';
const csvWriter = createObjectCsvWriter({
path: 'test.csv',
header: [
{ id: 'name', title: 'NAME' },
{ id: 'lang', title: 'LANGUAGE' }
],
useBom: true,
});
const data = [
{ name: 'Müller', lang: 'German, French, English' },
{ name: 'Groß', lang: 'German, English' }
];
async function writeCsv() {
await csvWriter.writeRecords(data);
}
writeCsv().catch(err => console.error('Error writing CSV:', err));
Feel free to try it out and let me know if you run into any issues.
Best, Harris