C#读取Excel时报错:External table is not in the expected format.

在上一篇日志《用C#读取Excel返回DataSet》中,提到了用C#读取Excel的方法,可以发现,用这种方法读取excel2003完全没有问题,可是,当我们读取Excel2007时,就会发现visual studio报错:External table is not in the expected format.

其实,这个错误是由于Excel版本的连接字符串引起的错误,

在上一篇日志提到的方法中,excel的连接字符串如下:

public static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\WorkSpace\MyDocument\Samples2.xlsx;Extended Properties=""Excel 8.0;HDR=YES;""";

用这种方法读取Excel2007时候,必须先在当前电脑上手动打开Excel摆放在那里,然后才能运行程序,显然这不会是我们的需求。

 

要解决这个问题更改连接字符串的OLEDB和Extended Properties版本即可,如下:

public static string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\WorkSpace\MyDocument\Samples.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";

posted @ 2010-11-19 21:18  leco_lin  阅读(4656)  评论(5编辑  收藏  举报