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

查看:37
本文介绍了VS Code 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);

运行时出现以下异常:

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

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

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