csv-writer icon indicating copy to clipboard operation
csv-writer copied to clipboard

add support for bom

Open MonksterFX opened this issue 4 years ago • 1 comments

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'));
  });

MonksterFX avatar Feb 24 '21 10:02 MonksterFX

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:

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

brakmic avatar Jul 16 '24 11:07 brakmic