VS Code C# - System.NotSupportedException: No data is available for encoding 1252
Asked Answered
J

1

78

I am trying to use ExcelDataReader to read an .xls file on Ubuntu. I am using VS Code with C#. Here is the code:

var stream = File.Open(filePath, mode: FileMode.Open, access: FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(stream);

I also tried this:

var reader = ExcelDataReader.ExcelReaderFactory.CreateBinaryReader(stream);

When I run, I am getting the following exception:

Unhandled Exception: System.NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. at System.Text.Encoding.GetEncoding(Int32 codepage)

I already installed the libmono-i18n-west4.0-cil (tried also with libmono-i18n4.0-all) as I found out some people recommending this, but the problem persists. Also installed the package System.Text.Encoding.CodePages without success.

Can anyone help to solve this?

Jibheaded answered 11/3, 2018 at 1:36 Comment(4)
System.Text.Encoding.CodePages... installed, or installed and registered?Dap
Hi, @Dap . If by "registered" you meant include the reference at .csproj , yes, it is also registered (<PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />).Alidis
So you didn't run this snippet somewhere in your code?: Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); This answer suggests that this is required.Dap
Yes, @Spender . I had put that line in my code but I think I forgot something because it did not work. After you warn in this comment, I tried again and it worked. Please put this comment as an answer and I will mark it.Alidis
E
211

I faced the same problem with .net Core application. I added the System.Text.Encoding.CodePages nuget package and registered the encoding provider before ExcelReaderFactory.CreateReader(stream) which resolved the issue.

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
//open file and returns as Stream
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
      using (var reader = ExcelReaderFactory.CreateReader(stream))
      {
      }
}
Electromagnet answered 6/4, 2018 at 21:30 Comment(2)
This is documented on the official page too. github.com/ExcelDataReader/…Precedent
Works for same error for ExecuteReader on Cache ODBC ConnectionMansur

© 2022 - 2024 — McMap. All rights reserved.