GridView绑定 excel数据源,并导入到数据库时报:外部表不是预期格式原因


我用c#写了个从excel中读取数据,然后存入access数据库程序,在我自己电脑上运行没有问题,拷到另外一台电脑上后老是报外部表不是预期格式的错误,
我在另一台电脑上试过用jet4.0 和ace12.0,offcie我也试过2003和2007,装了2003的时候用ace12.0报没有注册ace12.0,用jet4.0报外部表不是预期格式的错误,
装2007的时候,都报外部表不是预期格式的错误,问题原因:
错误经过:在读取Excel时,出现外部表不是预期的格式
错误原因1: 由于Excel 97-2003的连接格式与Excel 2010 的 不同造成。
以下是从网上摘抄原文
Excel “External table is not in the expected format.” .
Question:
I'm trying to read an Excel (xlsx) file using the code shown below. I get an "External table is not in the expected format." error unless I have the file already open in Excel. In other words, I have to open the file in Excel first before I can read if from my C# program. The xlsx file is on a share on our network. How can I read the file without having to open it first? Thanks
string sql = "SELECT * FROM [Sheet1$]"; string excelConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathname + ";Extended Properties=/"Excel 8.0;HDR=YES;IMEX=1;/""; using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sql, excelConnection)) { DataSet ds = new DataSet(); adaptor.Fill(ds); }
Answers:
"External table is not in the expected format." typically occurs when trying to use an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0
Using the following connection string seems to fix most problems.

public static string path = @"C:/src/RedirectApplication/RedirectApplication/301s.xlsx"; public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
以上出处:http://blog.csdn.net/ldl22847/article/details/6322339
解决方案1:
很多人换了2010后,问的最多的问题之一是2003里最经典的ADO中的“provider=Microsoft.Jet.OLEDB.4.0”这句怎么不能用了。
百度一下可以了解到,Microsoft.Jet.OLEDB.4.0是Microsoft Jet引擎,这适用于2003版本(2003之前的我没装,所以也不知道能向下适应到哪个版本),而在2007中,微软对其旗下 Access 与 Excel 的主要文件格式进行修改,并且重命名为 .accdb(Access 2007 数据库文件)与 .xlsx(Excel 2007 文件),因此未被 Microsoft Jet 引擎所支持,不过微软也很快的提出了Microsoft Office 2007 Desktop Drivers: Data Connectivity Components 来支持,目前的解决方法就是把连接字符串中的数据提供者改为Microsoft.ACE.OLEDB.12.0。
以上出处:http://blog.csdn.net/laoyebin/article/details/4902313
总上所述:
//2003(Microsoft.Jet.Oledb.4.0)
string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
//2010(Microsoft.ACE.OLEDB.12.0)
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
解决方案2:
用记事本打开你的excel文件,看看显示是否为乱码。
若是乱码,我这边测试是不会提示这个错误的,可以成功导入。
若是html代码,则表示你的excel文件格式不是标准的excel格式,才会提示“外部表不是预期的格式”的错误;
总结:如果格式不正确,则通过excel软件另存为标准的2003版本的格式
转自http://blog.csdn.net/jiajiayouba/article/details/7531707
优质内容筛选与推荐>>
1、shell脚本一些基础命令
2、PHP安装swoole扩展
3、Docker拉取mysql并连接
4、前端学习路线
5、Razor:RenderBody,RenderPage和RenderSection


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号