5

I'm having an issue saving an Excel file as a CSV with UTF-8 encoding.

Because I have non standard characters (different language) in my Excel document it caused issues when saved as a CSV. This was solved here by setting the web options encoding to UTF-8.

I am creating a basic C# program that parses data from an Excel file and saves it in CSV format but I cant get it to save using the UTF-8 encoding.

I am using Microsoft.Office.Interop.Excel to work with the Excel files.

This is my code:

private Excel.Application application = new Excel.Application { Visible = false };
private Excel.Workbook Workbook = application.Workbooks.Open(OrigionalFileUrl);
Workbook.SaveAs(NewFileUrl);

I have tried setting

application.DefaultWebOptions.Encoding = MsoEncoding.msoEncodingUTF8;

but it doesnt work and the CSV file that I get is always a mess when it comes to sections with special characters.

Thanks!

Community
  • 1
  • 1
Yuval Feldman
  • 149
  • 1
  • 1
  • 9

2 Answers2

5

I believe you want to do that on the workbook level, not on the application. Also, it's possible because you didn't include the file format as CSV that the SaveAs is using the native format but only changing the file extension.

Try these and see if they address your issue:

Workbook.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
Workbook.SaveAs(NewFileUrl, XlFileFormat.xlCSV);
Hambone
  • 15,600
  • 8
  • 46
  • 69
0

The proposed solution didn't work for me. But according to the documentation there is now a
XlFileFormat for CSV-UTF8:

XlFileFormat.xlCSVUTF8

https://learn.microsoft.com/en-us/office/vba/api/excel.xlfileformat

wasigh
  • 895
  • 1
  • 11
  • 21
  • 2
    .net works via interop, not via vba api. interop api has no such option: https://learn.microsoft.com/dotnet/api/microsoft.office.interop.excel.xlfileformat – valker Oct 15 '19 at 12:53