使用 Microsoft Jet Engine 读取 XLS 文件
我想允许我的应用程序从 XLS
文件导入数据。我已经使用 CSV
文件和 XML
文件执行此操作,但希望为用户开放范围。我在加载文件时遇到问题。我们将文件(XLS
、CSV
、XML
)加载到数据集中并从那里对其进行处理。 XLS
的加载代码如下,
FileInfo fi = new FileInfo(filename);
//create and open a connection with the supplied string
OleDbConnection objOleDBConn;
objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fi.FullName));
objOleDBConn.Open();
DataTable dt = objOleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null || dt.Rows.Count == 0)
{
return;
}
string sheet = dt.Rows[0]["TABLE_NAME"].ToString();
//then read the data as usual.
OleDbDataAdapter objOleDBDa;
objOleDBDa = new OleDbDataAdapter(string.Format("select * from [{0}]",sheet), objOleDBConn);
objOleDBDa.Fill(data);
objOleDBConn.Close();
所以我的数据加载正常,但它似乎设置了各个列的数据类型,这是我的其中一个列的问题。这是一个位字段,我们选择接受 False
、True
、Yes
、No
、Y
和 N
。有代码稍后将其转换为布尔值。这在 CSV
文件(连接字符串不同)中工作正常,但在 XLS
中,如果前 10 行为 FALSE
或 TRUE
,然后说第 11 个表示 YES
,然后我就得到一个空条目。我猜它会读取前几个条目并据此确定数据类型?
问题:有没有办法关闭根据前几个条目识别列数据类型的机制?
I want to allow my application to import data from XLS
files. I already do this with CSV
files and XML
files, but would like to open the scope for users. I am having trouble with loading the file. We load the files (XLS
,CSV
,XML
) into a data set and work on it from there. The loading code for XLS
is below
FileInfo fi = new FileInfo(filename);
//create and open a connection with the supplied string
OleDbConnection objOleDBConn;
objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fi.FullName));
objOleDBConn.Open();
DataTable dt = objOleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null || dt.Rows.Count == 0)
{
return;
}
string sheet = dt.Rows[0]["TABLE_NAME"].ToString();
//then read the data as usual.
OleDbDataAdapter objOleDBDa;
objOleDBDa = new OleDbDataAdapter(string.Format("select * from [{0}]",sheet), objOleDBConn);
objOleDBDa.Fill(data);
objOleDBConn.Close();
So my data gets loaded OK, but it appears to set the data types of various columns, and this is a problem for one of my columns. It's a bit field and we have chosen to accept False
, True
, Yes
, No
, Y
, and N
. There is code that transfers this into a boolean later on. This works fine in a CSV
file (for which the connection string is different) but in an XLS
, if the first 10 rows are say FALSE
or TRUE
, and then say the 11th says YES
, then I just get a null entry. I'm guessing that it reads the first few entries and determines the data type based on that?
Question: Is there a way to turn off the mechanism that identifies a column's data type based on the first few entries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题与 Excel cell-values are truncated by OLEDB-provider 非常相似 和 ASP.NET 中的 Excel 读取:如果列具有不同的数据格式,则不会读取数据 看起来这些其他问题中讨论了一些可行的解决方案。
This question is very similar to Excel cell-values are truncated by OLEDB-provider and Excel reading in ASP.NET : Data not being read if column has different data formats Looks like a couple of workable solutions are discussed in these other questions.
有一个注册表设置可以告诉 Jet 提供程序要读取多少行来推断列的数据类型。我相信它默认为 8。它是:(
更改适用的版本)。在您的情况下,它推断出布尔值,因此忽略字符串值“yes”。
There is a registry setting to tell the Jet provider how many rows to read to infer the data type for the column. It defaults to 8 I believe. It is:
(change version as applicable). In your case, it has infered boolean and therefore ignores the string value "yes".
试试这个我通过堆栈溢出发布的OleDBAdapter Excel QA。
我填充了一个工作表列,其中全部为 TRUE 或 FALSE,然后随机输入几个“是”或“否”值,效果很好...
在调试模式下运行,然后在填充后单击数据集可视化工具以查看结果。
或者,将其添加到输出代码的末尾
Try this OleDBAdapter Excel QA I posted via stack overflow.
I populated a worksheet column w/ all TRUE or FALSE and then threw in several "yes" or "no" values at random and it worked fine...
Run in Debug mode, then click on the DataSet Visualizer after it's populated to see results.
Or, add this to the end of the code for the output
技巧是将标题行作为行来推断数据类型,以便所有列都将被读取为字符串。然后,如果需要,您将能够在代码中解析以更正数据类型,而不会丢失值 - 用于此 HDR=No
trick is to include header line as row from which to infer data type, so that all columns will be read as string. Then you will be able to parse in code to correct data type, if you need, without losing values - use for this HDR=No