我正在尝试使用 ExcelDataReader 在 Ubuntu 上读取 .xls 文件。我在 C# 中使用 VS Code。这是代码:
var stream = File.Open(filePath, mode: FileMode.Open, access: FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(stream);
我也试过这个:
var reader = ExcelDataReader.ExcelReaderFactory.CreateBinaryReader(stream);
当我运行时,出现以下异常:
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
但没有成功。
谁能帮忙解决这个问题?
最佳答案
我在 .net Core 应用程序中遇到了同样的问题。我添加了 System.Text.Encoding.CodePages
nuget 包并在 ExcelReaderFactory.CreateReader(stream)
之前注册了编码提供程序,这解决了这个问题。
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))
{
}
}
关于c# - VS 代码 C# - System.NotSupportedException : No data is available for encoding 1252,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49215791/