System.NotSupportedException: No data is available for encoding 1252

nuget:
Install-Package System.Text.Encoding.CodePages -Version 5.0.0

code:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


.NET Core supports only ASCII, ISO-8859-1 and Unicode encodings, whereas .NET Framework supports much more.

However, .NET Core can be extended to support additional encodings like Windows-1252, Shift-JIS, GB2312 by registering the CodePagesEncodingProvider from the System.Text.Encoding.CodePages NuGet package.

After the NuGet package is installed the following steps as described in the documentation for the CodePagesEncodingProvider class must be done to register the provider:

  1. Add a reference to the System.Text.Encoding.CodePages.dll assembly to your project.
  2. Retrieve a CodePagesEncodingProvider object from the static Instance property.
  3. Pass the CodePagesEncodingProvider object to the Encoding.RegisterProvider method.

What ckuri said. Just to be clear, you need the following line of code before opening the stream (steps 2,3):

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

ExcelDataReader - Important note on .NET Core

By default, ExcelDataReader throws a NotSupportedException "No data is available for encoding 1252." on .NET Core.

To fix, add a dependency to the package System.Text.Encoding.CodePages and then add code to register the code page provider during application initialization (f.ex in Startup.cs):

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

This is required to parse strings in binary BIFF2-5 Excel documents encoded with DOS-era code pages. These encodings are registered by default in the full .NET Framework, but not on .NET Core.

Tags:

C#