EXCEL文档没有打开时,报错误"外部表不是预期的格式。"

来源:互联网 发布:九型人格出生日期算法 编辑:程序博客网 时间:2024/05/23 01:58

EXCEL文档没有打开时,报错误"外部表不是预期的格式。"打开EXCEL文档时,能正常取到数据,原因可能是:

       Microsoft.Jet.OLEDB.4.0Microsoft Jet引擎,这适用于2003版本(2003之前的我没装,所以也不知道能向下适应到哪个版本),而在2007中,微软对其旗下 Access 与 Excel 的主要文件格式进行修改,并且重命名为 .accdbAccess 2007 数据库文件)与 .xlsxExcel 2007 文件),因此未被 Microsoft Jet 引擎所支持,不过微软也很快的提出了Microsoft Office 2007 Desktop Drivers: Data Connectivity Components 来支持,目前的解决方法就是把连接字符串中的数据提供者改为Microsoft.ACE.OLEDB.12.0。

以上出处:http://blog.csdn.net/laoyebin/article/details/4902313

 


总上所述:


解决方案1:


//2003Microsoft.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);

//2010Microsoft.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);

//office2007之前 仅支持.xls

            //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';";

            //支持.xls.xlsx,即包括office2010等版本的   HDR=Yes代表第一行是标题,不是数据;

            //const string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";

            string strType = System.IO.Path.GetExtension(filepath);

            string strCon = string.Empty;

            if (strType == ".xlsx")

            {

                strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;IMEX=1'";

            }

            else if (strType == ".xls")

            {

                strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";

            } 


解决方案2:

用记事本打开你的excel文件,看看显示是否为乱码。
若是乱码,我这边测试是不会提示这个错误的,可以成功导入。

若是html代码,则表示你的excel文件格式不是标准的excel格式,才会提示外部表不是预期的格式的错误;

总结:如果格式不正确,则通过excel软件另存为标准的2003版本的格式


 


0 0
原创粉丝点击