使用 OleDb 或 ExcelDataReader 读取 Excel 文件会返回编码错误的特殊字符

发布于 2024-12-22 19:52:59 字数 886 浏览 2 评论 0原文

我需要用 C# 读取 EXCEL 二进制文件 (xls)。

基本上它可以工作,但是我遇到了错误编码的字符(例如德语元音变音)的问题。

有什么方法可以指定编码吗? Excel 文件是否有类似编码之类的东西?

Excel 文件中的字符串是

莉迪亚·霍默尔

当使用 Jet 或 EDR 阅读时,我得到:

莉迪亚·赫默尔

我尝试过 OleDb 和 Excel Data Reader 项目。

这是我用来打开和读取文件的代码:

var connectionString =
            string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0};
                          Extended Properties=\"Excel 8.0;HDR=no\";", filePath);

var adapter = new OleDbDataAdapter(
          "SELECT * FROM [Webshop orders$]", connectionString);

var ds = new DataSet();     
adapter.Fill(ds, "Orders");
var data = ds.Tables["Orders"].AsEnumerable();
foreach (var row in data)
{
    var str = Convert.ToString(row[0]);

I need to read an EXCEL binary file (xls) in C#.

Basically its working, but i have problems with wrongly encoded characters (german umlauts for example).

Is there some way to specify an encoding? Does Excel files have something like an encoding at all?

The string in the Excel File is

Lydia Hömmerl

When reading with Jet or EDR i get:

Lydia HŠmmerl

I have tried OleDb and the Excel Data Reader project.

Here is the code i use to open and read the file:

var connectionString =
            string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0};
                          Extended Properties=\"Excel 8.0;HDR=no\";", filePath);

var adapter = new OleDbDataAdapter(
          "SELECT * FROM [Webshop orders$]", connectionString);

var ds = new DataSet();     
adapter.Fill(ds, "Orders");
var data = ds.Tables["Orders"].AsEnumerable();
foreach (var row in data)
{
    var str = Convert.ToString(row[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-29 19:52:59

Unicode with C#

还是有很多人不明白二进制和文本的区别,或者知道什么是字符编码等等。这个页面就是为这些人编写的。它提到了一些高级主题,但只是为了让读者意识到它们的存在,而不是对该主题提供太多指导。

Unicode with C#

There are still many people who don't understand the difference between binary and text, or know what a character encoding is, etc. It is for these people that this page has been written. It mentions a few advanced topics, but only to make the reader aware of their existence, rather than to give much guidance on the topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文