0

I copied from an excel file to a txt file the column names. I read from the txt file the following way:

CultureInfo cultureHU;
Encoding encodingHU;
cultureHU = CultureInfo.GetCultureInfo("hu-HU");
encodingHU = Encoding.GetEncoding(cultureHU.TextInfo.ANSICodePage);
using (StreamReader sr = new StreamReader("settings.txt", encodingHU, true))
{
...
}

How do I read from an Excel file with the same encoding? If I do it the default way (xlRange.Cells[1, i].Value.ToString()), then I get wrong values:

  • in the excel and txt file I have: "Szerzõdõ"
  • reading from the text file in encodingHU encoding I get: "Szerződő" (this is the correct format)
  • reading from excel in C# i get: "Szerzõdõ"

2 Answers 2

2

You can convert the bad string to the correct encoding like this:

Console.WriteLine(encodingHU.GetString(Encoding.Default.GetBytes(str)));
Sign up to request clarification or add additional context in comments.

Comments

0

I used ExcelDataReader. I needed coding 1250

ExcelReaderConfiguration conf = new ExcelReaderConfiguration();
conf.FallbackEncoding = Encoding.GetEncoding(1250);

using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream, conf))
{
...
}



                    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.