Bug 33635

Summary: CSV export display broken diacritics in Excel
Product: Koha Reporter: Katrin Fischer <katrin.fischer>
Component: Staff interfaceAssignee: Martin Renvoize (ashimema) <martin.renvoize>
Status: RESOLVED FIXED QA Contact: Lucas Gass (lukeg) <lucas>
Severity: normal    
Priority: P5 - low CC: clemens.tubach, dcook, gmcharlt, martin.renvoize, michaela.sieber
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23685
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
24.11.00
Circulation function:
Attachments: Bug 33635: Add BOM to report exports as CSV
Bug 33635: Add BOM to report exports as CSV
Bug 33635: Add BOM to report exports as CSV

Description Katrin Fischer 2023-04-28 13:11:36 UTC
This has been a re-occurring support issue:

When using one of Koha's many CSV download options, the exported CSV file doesn't open as UTF-8 in Excel, resulting in broken diacritics. LibreOffice doesn't show the issue.

The only way to fix it right now is not to open the file directly, but to use the import functionality in Excel as it lets you pick the encoding and separators. This means a lot of extra steps and clicks, which make the CSV export functionalities in Koha hard to use.

It turns out that Excel will import the CSV correctly, if we convert the encoding of the exported CSV file from UTF-8 to UTF-8-BOM.

Problematic CSV exports include:

* Exporting a basket summary page as CSV
* Using CSV profile based exports in general, including: lists, basket, lost items report
* ...

Can we 'fix' the encoding to use BOM?
Comment 1 Martin Renvoize (ashimema) 2024-09-18 04:35:30 UTC
I started thinking about this after looking  at bug 23685 too.

I'm not entirely sure if we want to add a BOM to our CSV exports or not.. there are still programs out there written to work with ascii only and they struggle with BOM's.

That said, excel is the most likely target so adding a BOM for that would be sensible.

We could do with checking that adding said BOM doesn't break other popular tools like OpenOffice or LibreOffice however.

Text::CSV::Encoded doesn't have any native BOM support, but it appears we're just dealing with strings.

Perhaps we could use File::BOM
Comment 2 Martin Renvoize (ashimema) 2024-09-18 04:44:11 UTC
Created attachment 171650 [details] [review]
Bug 33635: Add BOM to report exports as CSV

When you export a report as CSV excel will not recognise that it's
encoding is utf-8 unless there is a correct Byte Order Mark prepended to
the file.

This patch adds such a BOM to the output file allowing Excel to treat
the file correctly.
Comment 3 Martin Renvoize (ashimema) 2024-09-18 04:44:46 UTC
Please test this
Comment 4 Michaela Sieber 2024-09-18 14:33:37 UTC
Test plan:

1.) search for a title with one item and edit the item
2.) add the internal note: "Test the diacritics ä ö ü  é è ÄÖÜ"
3.) copy the biblionumber of the title
4.) create an sql report:
 SELECT itemnotes_nonpublic from items where biblionumber=<copied biblionumber>
5.) run report
6.) download as csv file
7.) open in excel
8.) Check that the diacritics do not look like above (internal note)
9.) Apply Patch
10.) repeat steps 5-7
11.) Check that the diacritics look like above (internal note)
Comment 5 PTFS Europe Sandboxes 2024-09-18 14:34:19 UTC
Created attachment 171677 [details] [review]
Bug 33635: Add BOM to report exports as CSV

When you export a report as CSV excel will not recognise that it's
encoding is utf-8 unless there is a correct Byte Order Mark prepended to
the file.

This patch adds such a BOM to the output file allowing Excel to treat
the file correctly.

Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
Comment 6 Martin Renvoize (ashimema) 2024-09-20 12:56:44 UTC
You're a star Micheal, thanks for testing ☺️
Comment 7 Lucas Gass (lukeg) 2024-09-20 16:56:16 UTC
Created attachment 171837 [details] [review]
Bug 33635: Add BOM to report exports as CSV

When you export a report as CSV excel will not recognise that it's
encoding is utf-8 unless there is a correct Byte Order Mark prepended to
the file.

This patch adds such a BOM to the output file allowing Excel to treat
the file correctly.

Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Comment 8 David Cook 2024-09-30 01:25:12 UTC
Nice one. This kind of thing has driven me nuts elsewhere in the past...
Comment 9 Martin Renvoize (ashimema) 2024-09-30 14:33:47 UTC
Thanks for all the hard work!

Pushed to main for the next 24.11.00 release as RM Assistant
Comment 10 Lucas Gass (lukeg) 2024-11-18 22:16:08 UTC
Doesn't apply to 24.05.x, rebase if needed.