Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF-8 with BOM doesn't work easily with csv #691

Open
1 of 4 tasks
teamolhuang opened this issue Nov 12, 2024 · 0 comments
Open
1 of 4 tasks

UTF-8 with BOM doesn't work easily with csv #691

teamolhuang opened this issue Nov 12, 2024 · 0 comments

Comments

@teamolhuang
Copy link

teamolhuang commented Nov 12, 2024

Excel Type

  • XLSX
  • XLSM
  • CSV
  • OTHER

Upload Excel File

example).csv

MiniExcel Version

1.34.2

Description

When writing an UTF8 csv and then opening with Excel, it needs to be UTF8 with BOM, otherwise the UTF-8 characters will be messed up.
I've tried methods like,

    private static readonly Encoding _utf8WithBom = new UTF8Encoding(true); // note the `true` here
    private static readonly CsvConfiguration _csvConfiguration = new()
    {
        StreamWriterFunc = stream => new(stream, _utf8WithBom);
    };

doesn't work. Or,

  stream.WriteByte(0xEF);
  stream.WriteByte(0xBB);
  stream.WriteByte(0xBF);
  // ... proceeds to SaveAs()

doesn't work.

Solution

    private static readonly char _utf8bom = '\ufeff';

    private static readonly CsvConfiguration _csvConfiguration = new()
    {
        StreamWriterFunc = stream =>
        {
            StreamWriter sw = new(stream, _utf8WithBom);
            
            sw.Write(_utf8bom);
            return sw;
        }
    };

Since new UTF8Encoding(true) seems the official way in .NET of saying this encoding should come with BOM, would it be supported in the future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant