I am writing a C# library to read in Excel files (both xls and xlsx) and I'm coming across an issue.
Exactly the same as what was expressed in this question, if my Excel file has a column that has string values, but has a numeric value in the first row, the OLEDB provider assumes that column to be numeric and returns NULL
for the values in that column that are not numeric.
I am aware that, as in the answer provided, I can make a change in the registry, but since this is a library I plan to use on many machines and don't want to change every user's registry values, I was wondering if there is a better solution.
Maybe a DB provider other than ACE.OLEDB (and it seems JET is no longer supported well enough to be considered)?
Also, since this needs to work on XLS / XLSX, options such as EPPlus / XML readers won't work for the xls version.
答案 0 :(得分:1)
您的连接字符串应如下所示
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcelfile.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
连接字符串中的IMEX = 1是您需要将列视为混合数据类型的部分。这应该可以正常工作而无需编辑注册表。
HDR =是只是将第一行标记为列标题,在您的特定问题中不需要,但我仍然包含它。
始终使用IMEX = 1是检索混合数据列数据的更安全的方法。
来源:https://www.connectionstrings.com/excel/
编辑:
以下是我正在使用的数据:
这是输出:
这是我使用的确切代码:
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\test.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1""";
using (DbClass db = new DbClass(connString))
{
var x = db.dataReader("SELECT * FROM [Sheet1$]");
while (x.Read())
{
for (int i = 0; i < x.FieldCount; i++)
Console.Write(x[i] + "\t");
Console.WriteLine("");
}
}
DbClass是我为了让生活更轻松而制作的简单包装。它可以在这里找到: