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

1 comment:

Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

 JavaScript is another way to interact with web elements when normal selenium methods fail to act. But one issue with javascript is, it does...