今天在写一个把Excel导入到数据的时候报了一个异常“外部表不是预期的格式。”

外部表不是预期的格式。

 

于是到网上搜了一下,我写的着个strConn只试用于office2003版的。而我的是office2010版的。所以这样写是会出现异常的。针对不同的office版本也有不同的写法。

Office2003(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;'", strExcelFileName);

Office2007(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;'", strExcelFileName);

Office2010(Microsoft.ACE.OLEDB.12.0)
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'", strExcelFileName);

还要一个就是“HDR=Yes”这个问题,如果HDR的值是Yes,那么第一行是被当做列名的,不会被导入。只有为“No”才可以被当做数据导入。

相关文章:

  • 2021-05-21
  • 2022-12-23
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-14
  • 2022-01-23
  • 2021-09-11
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案