November 9, 2020

How to fix "No data is available for encoding 1252" error with ExcelReaderFactory in .Net Core

  November 9, 2020
If you are moving from the .Net framework to .Net core, you may come across many issues.
Especially if you are reading something from excel you may get below issue somewhere around the process.

"No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method."

This happens due to the encoding difference in .Net core.

This issue can be easily be fixed by adding the below line of code in your excel handling methods.

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

Don't forget to add the corresponding NuGet package also.

So while excel reading may look like this.

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

FileStream stream = File.Open(ExcelRepFile, FileMode.Open, FileAccess.Read);

IExcelDataReader excelReader = null;
try
{
	if (ExcelRepFile.EndsWith(".xls"))
	{
		excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
	}
	if (ExcelRepFile.EndsWith(".xlsx"))
	{
		excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
	}

}
catch (Exception)
{
	throw;
}
logoblog

Thanks for reading How to fix "No data is available for encoding 1252" error with ExcelReaderFactory in .Net Core

Previous
« Prev Post

2 comments:

How to send email from current Outlook instance using Java + VBScript

If we are using .Net, we can easily access outlook and send emails using its own libraries. But what if we are using Java? Here we can seek ...