询问者
Excel 导入格式问题

问题
-
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + Server.MapPath("\\Excel") + "\\" + strFileName + ";Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'"; if (strExt == ".xlsx") { strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Server.MapPath("\\Excel") + "\\" + strFileName + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; } OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [Sheet1$]", conn); DataSet ds = new DataSet(); adapter.Fill(ds, "Table1");
使用以上方式导入excel,excel中有一个手机列,某些行填写的手机号,excel标识为“此单元格中的数字为文本格式,或者其前面有撇号”,这样的数据读取后,是空字符串。请教程序上怎么解决?
开心了就笑,不开心了就过会儿再笑
全部回复
-
您好,
我测试了您的代码,分别用于xls和xlsx文件上,都可以读取到文本格式或者加撇号的电话号码。Excel中数据格式如下图所示。请提供更多的信息,以获取更多的帮助。
Best Regards,
Li WangWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
您好,
您也可以把不能读取的部分截图发上来。有没有可能是你的电话号码中包含半角字符。比如4434 4424?
Best Regards,
Li WangWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
我也试了下您的代码,号码都是可以读到的。你可以把你的excel发到jerrywng@163.com
-
测试了,都可以取的到,使用的是如下代码。
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=D:\\textPone.xls;Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [Sheet1$]", conn); DataSet ds = new DataSet(); adapter.Fill(ds, "Table1"); for (int i = 0; i < 32; i++) { Console.WriteLine(ds.Tables[0].Rows[i][0] + "/" + ds.Tables[0].Rows[i][4] + "/" + ds.Tables[0].Rows[i][8]); }
得到的结果是,
-
你好,
请问你的问题解决了吗?如果解决的话,可以分享一下你的解决方案吗给我们大家吗?如果没有的话,也可以发一份数据给我,我也可以帮你看看是什么原因。我的email是 coleasp@163.com
Best regards,
Cole