moqui-framework icon indicating copy to clipboard operation
moqui-framework copied to clipboard

How to add a UTF-8 BOM in wirte csv file ?

Open cleverdb opened this issue 6 years ago • 2 comments

Hello. How to write BOM (\xEF\xBB\xBF) in the begining of outputed file. It solves the problem of opening UTF-8 encoded csv-file in MS Excel. Thanks.

cleverdb avatar Jan 10 '20 06:01 cleverdb

Is this necessary for importing these CSV file in MS Excel? I don't have access to it so couldn't test that.

Also, if the byte order mark characters are added does it cause problems for other things including desktop apps like LibreOffice/OpenOffice, and for importing into Moqui and other systems? A lot of testing would be needed before something like this could be a default.

IMO adding a BOM for UTF-8 encoding is a bad idea... it's not needed for UTF-8 like it is for UTF-16 and such (part of the reason UTF-8 is a much better standard) and from what I understand of the weird world of software it is mostly a Microsoft thing to expect or require BOM characters for UTF-8 encoded text files. For what it's worth, catering to Microsoft weirdness versus the rest of the world is a very low priority for Moqui.

There are now various XLSX export options in Moqui that can be used instead and will generally be much better than a CSV file if the intention is to have it opened in a spreadsheet.

jonesde avatar Feb 10 '20 19:02 jonesde

There's a workaround. You can define your owner csv screen macro file.

<#include "component://webroot/../../template/screen-macro/DefaultScreenMacros.csv.ftl"/>
<#-- add utf8 bom -->
<#macro screen>${"\xfeff"}<#recurse></#macro>

Use this macro file you will get BOM for UTF-8. Be careful, some system will throw an error when import UTF-8-BOM encoded csv file.

chunlinyao avatar Feb 21 '20 09:02 chunlinyao