I created a console application in C#. The program refreshes host names in my database while reading data from an Excel sheet using ExcelDataReader.
The Excel file is saved in .CSV format. One of the rows has a foreign name, for example:
AZULCÃNEPAHowever, when I open it in my program or even in Notepad, the name appears incorrectly as:
AZULCÁNEPAIf I save the file as CSV UTF-8 or as .XLSX, then I get the correct name (
AZULCÃNEPA) in my program.Why does this happen when saving as normal
.CSV, and how can I make ExcelDataReader read the correct characters?
What I tried:
Opened the .CSV file in Notepad and confirmed the issue is the same (foreign characters appear corrupted).
Tried reading the file using ExcelDataReader in C#, but still got the corrupted text (AZULCÁNEPA).
Tried reading the file using FallbackEncoding = Encoding.UTF8
Tried to convert the file from UTF-8 to UTF-8 BOM using code
Tried to convert the file Bytes File.WriteAllText(outputPath1, File.ReadAllText(inputPath, Encoding.Default), new UTF8Encoding(true));
Saved the file again in UTF-8 CSV and also as .XLSX — in both cases, the characters display correctly in my program.
What I expected: I expected the characters to appear as AZULCÃNEPA even when the Excel file is saved as a normal .CSV (not just UTF-8 or .XLSX). or can what can i do to get it right ?
Encoding.Defaultwhen you open it. But if you have control over the CSV encoding, use UTF8 to fix it.