在 C# 中读取 Excel 文件时出现问题

发布于 2024-08-15 18:42:53 字数 539 浏览 4 评论 0原文

我正在使用此代码通过 OLDB 连接读取 Excel 文件

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

        var fileName = string.Format("{0}\\s23.xls", Directory.GetCurrentDirectory());
        var adapter = new OleDbDataAdapter("SELECT * FROM [TEJ3$]  ", connectionString);
        DataTable dt=new DataTable();
        adapter.Fill(dt, "Table1");

,运行此代码后我的数据表已填充。但我有一列有很多字符串单元格和很少的空单元格;在 Excel 文件中,该单元格具有数值。

有人有主意吗?

I'm reading an Excel file with OLDB Connection using this code

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

        var fileName = string.Format("{0}\\s23.xls", Directory.GetCurrentDirectory());
        var adapter = new OleDbDataAdapter("SELECT * FROM [TEJ3$]  ", connectionString);
        DataTable dt=new DataTable();
        adapter.Fill(dt, "Table1");

and after runing this code my data table is filled. But I have a column that has many string cells and few empty cells ; in excel file this cells have numeric values.

Someone has an idea?

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

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

发布评论

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

评论(4

给不了的爱 2024-08-22 18:42:53

在此处查看第一个示例: http://www.connectionstrings.com/excel

经常出错的是Excel 将根据前 X 行估计列的类型。当此后值不匹配时,这些行将获得空值。恐怕有时进入注册表是让 Excel 驱动程序首先扫描所有行的唯一方法(如connectionstrings.com 文章中所述)。

在您的环境中尝试 HDR 和 IMEX 设置。在某些情况下这也会有帮助。

Check the first examples here: http://www.connectionstrings.com/excel

What often goes wrong is that Excel will estimate the type of a column based upon the first X rows. When after that the values don't match, these rows get empty values. I'm afraid that going into the registry is sometime the only way to get the Excel driver to scan all rows first (as described in the connectionstrings.com article).

Play around with the HDR and IMEX settings in your environment. In some cases that will help as well.

千と千尋 2024-08-22 18:42:53

我遇到了这个确切的问题并使用 IMEX 设置解决了它。如果其他人想知道如何包含 IMEX,这是我的连接字符串中的内容

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;IMEX=1';Data Source={0};";
connectionString = string.Format(connectionString, excelWorkbookPath);

I had this exact problem and solve it with using IMEX setting. In case others are wondering how to include the IMEX, here is what I have on my connection string

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;IMEX=1';Data Source={0};";
connectionString = string.Format(connectionString, excelWorkbookPath);
你爱我像她 2024-08-22 18:42:53

SpreadsheetGear for .NET 可让您从 C# 加载 Excel 工作簿并访问底层单元格值或格式化值任何顺序,无论工作簿如何布局或单元格是什么类型。

您可以此处查看实时 ASP.NET 示例并下载免费试用版此处

免责声明:我拥有 SpreadsheetGear LLC

SpreadsheetGear for .NET will let you load Excel workbooks from C# and access the underlying cell values or the formatted values in any order no matter how the workbook is laid out or what the types of the cells are.

You can see live ASP.NET samples here and download the free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

海的爱人是光 2024-08-22 18:42:53

只需确保您的 Excel 文件未打开即可。关闭您的 Excel 应用程序并然后启动你的程序。

苏尼尔

Just make sure that your excel file is not open. Close your excel application & then start your program.

Sunil

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