使用 Microsoft Jet Engine 读取 XLS 文件

发布于 2024-08-13 06:06:58 字数 1347 浏览 11 评论 0原文

我想允许我的应用程序从 XLS 文件导入数据。我已经使用 CSV 文件和 XML 文件执行此操作,但希望为用户开放范围。我在加载文件时遇到问题。我们将文件(XLSCSVXML)加载到数据集中并从那里对其进行处理。 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();

所以我的数据加载正常,但它似乎设置了各个列的数据类型,这是我的其中一个列的问题。这是一个位字段,我们选择接受 FalseTrueYesNoYN。有代码稍后将其转换为布尔值。这在 CSV 文件(连接字符串不同)中工作正常,但在 XLS 中,如果前 10 行为 FALSETRUE,然后说第 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 技术交流群。

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

发布评论

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

评论(4

美人如玉 2024-08-20 06:06:58

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.

酒与心事 2024-08-20 06:06:58

有一个注册表设置可以告诉 Jet 提供程序要读取多少行来推断列的数据类型。我相信它默认为 8。它是:(

HKLM\Software\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows

更改适用的版本)。在您的情况下,它推断出布尔值,因此忽略字符串值“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:

HKLM\Software\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows

(change version as applicable). In your case, it has infered boolean and therefore ignores the string value "yes".

╰ゝ天使的微笑 2024-08-20 06:06:58

试试这个我通过堆栈溢出发布的OleDBAdapter Excel QA

我填充了一个工作表列,其中全部为 TRUE 或 FALSE,然后随机输入几个“是”或“否”值,效果很好...

在调试模式下运行,然后在填充后单击数据集可视化工具以查看结果。
或者,将其添加到输出代码的末尾

// DataSet:          
Object row11Col3 = ds.Tables["xlsImport"].Rows[11][3];
string rowElevenColumn3 = row11Col3.ToString();

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

// DataSet:          
Object row11Col3 = ds.Tables["xlsImport"].Rows[11][3];
string rowElevenColumn3 = row11Col3.ToString();
夏夜暖风 2024-08-20 06:06:58

技巧是将标题行作为行来推断数据类型,以便所有列都将被读取为字符串。然后,如果需要,您将能够在代码中解析以更正数据类型,而不会丢失值 - 用于此 HDR=No

objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data  Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=1'", fi.FullName));

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

objOleDBConn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data  Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=1'", fi.FullName));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文