使用 C#/OLEDB 从 Excel 导入时值发生变化

发布于 2024-09-19 10:04:11 字数 785 浏览 0 评论 0原文

我正在尝试使用 OleDB 将一些数据从 Excel 电子表格导入到我的 C# 应用程序中。我使用带有 IMEX=1,HDR=YES 选项的标准连接字符串

Provider=Microsoft.ACE.OLEDB.12.0;数据源={0};扩展 属性=“Excel 12.0;HDR=YES;IMEX=1”

我有一列,其中值主要是文本:TRUE 或 FALSE。中间有几个 BLANK 值。导入时,空白值将替换为空字符串。这可能是因为 OleDB 确定列的数据类型为布尔值,并且不导入任何非布尔值,例如 BLANK。

我尝试了 这个答案但它不起作用。即使我预先创建 DataTable 并将其列设置为全部具有字符串数据类型,空白值仍会继续被删除。

我尝试破解同一问题中建议的注册表项,但没有效果。

有什么办法吗?或者我必须尝试使用​​ COM 吗?我们没有 SpreadsheetGear 的预算,而且我无法使用 excellibrary,因为我有主要处理 .xlsx 文件。

提前致谢

I'm trying to import some data from an Excel spreadsheet into my C# application using OleDB. I use the standard connection string with the IMEX=1,HDR=YES options

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended
Properties="Excel 12.0;HDR=YES;IMEX=1"

I have a column where the values are mostly the text: TRUE or FALSE. Partway down there is a couple of values of BLANK. When the import happens, the BLANK values are replaced empty strings. This is presumably because OleDB determined the data type of the column to be boolean and doesn't import any non-boolean values, like BLANK.

I tried the solution in this answer but it did not work. Even when I pre-create the DataTable and set its columns to all have a datatype of string, the BLANK values continue to be removed.

I have tried hacking the registry key suggested in the same question but to no effect.

Is there any recourse? Or am I going to have to try to use COM? We don't have the budget for SpreadsheetGear and I can't use excellibrary because I have to deal with .xlsx files mostly.

Thanks in advance

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

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

发布评论

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

评论(2

寂寞陪衬 2024-09-26 10:04:11

尝试将 Excel 文件保存为 CSV 格式并进行处理。

Try to save your Excel file to CSV format and process it.

长亭外,古道边 2024-09-26 10:04:11

我知道我迟到了三年,但这可能会对某人有所帮助。所有功劳都归功于 Andrew Garrison,他发布了他的答案此处
您需要修改注册表设置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\

将此项下的 TypeGuessRows 更改为 0,并将 ImportMixedTypes 更改为 Text

ACE 扫描前 8 行并尝试猜测列格式。之后与格式不匹配的所有其他内容都将返回 NULL。将 TypeGuessRow 设置为 0 会强制驱动程序扫描所有行以确定格式(当然会有一些性能影响)。 ImportMixedTypes=text 指定具有混合格式的列(如您的情况)应作为文本导入。

I know I am three years late, but this might help somebody. All credit goes to Andrew Garrison who posted his answer here.
You need to modify a registry setting:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\

Change TypeGuessRows to 0 and ImportMixedTypes to Text under this key.

ACE scans first 8 rows and tries to guess column formats. Everything else that doesn't match the format afterwards returns as NULL. Setting TypeGuessRow to 0 forces the driver to scan all rows to determine format (of course there is some performance hit). ImportMixedTypes=text specifies that columns with mixed format, as in your case, should be imported as text.

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