您的位置:首页 > 编程语言 > C#

C#导入Excel数据时的一个BUG

2011-01-26 13:27 525 查看
在进行SQL数据导入时无法将第二行和第五行的value值导入,图示粘贴不过来,所以进行口述,大致情况是:

第二行和第五行的value值是数字型,而其他几行的value是字符型。

 

经过在网上查找和实际操作检验,最终找到了一个解决办法:

1、在连接Excel的Oledb串中加上
IMEX=1,使得 Excel
表格中混合列(即该列中的包含有多种类型的数据)被
ADO.NET 认为其数据类型是 String
。如果某列的数据全部都是一种类型,如数字型,那么 ADO.NET
仍然认为该是列的类型是其数据的类型。

@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

"HDR=Yes;" indicates that the first row contains columnnames, not data

"IMEX=1;" tells the driver to always read "intermixed" data columns as text

TIP! SQL syntax: "SELECT * FROM [sheet1$]" - i.e. worksheet name followed by a "$" and wrapped in "[" "]" brackets.

2、修改注册表键值。

[HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel]

located registry REG_DWORD "TypeGuessRows". That's the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt
performance.
TypeGuessRows:修改为0。(检索所有行)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel c# properties sql string c