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:

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...