VS代码C#-System.NotSupportedException:没有数据可用于编码1252 [英] VS Code C# - System.NotSupportedException: No data is available for encoding 1252

查看:365
本文介绍了VS代码C#-System.NotSupportedException:没有数据可用于编码1252的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ExcelDataReader在Ubuntu上读取.xls文件。我在C#中使用VS Code。这是代码:

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);

我也尝试过这样:

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

我跑步时遇到以下异常:

When I run, I am getting the following exception:


未处理的异常:System.NotSupportedException:没有数据可用于编码1252。有关定义自定义编码的信息,请参见Encoding.RegisterProvider方法的文档。
在System.Text.Encoding.GetEncoding(Int32代码页)

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)

我已经安装了 libmono -i18n-west4.0-cil (也曾与 libmono-i18n4.0-all 一起尝试过),因为我发现有人建议这样做,但是问题仍然存在。还成功安装了软件包 System.Text.Encoding.CodePages

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?

推荐答案

.net Core应用程序遇到相同的问题。我添加了 System.Text.Encoding.CodePages nuget包,并在 ExcelReaderFactory.CreateReader(stream)之前注册了编码提供程序。

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))
      {
      }
}

这篇关于VS代码C#-System.NotSupportedException:没有数据可用于编码1252的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆