无法使用System.Data.OleDb.OleDbConnection打开Excel文件
问题描述:
Iam尝试使用以下代码打开XLSX文件(以便我可以创建数据表) Iam。无法使用System.Data.OleDb.OleDbConnection打开Excel文件
System.Data.OleDb.OleDbConnection oleDbCon;
System.Data.OleDb.OleDbDataAdapter oleDbDataAd;
oleDbCon = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties=Excel 8.0;");
但是当文件路径包含扩展XLSX文件,我得到一个错误 “外部表不是预期的格式。”
上面的代码工作正常时,文件是扩展xls。
我必须更改连接字符串吗?
任何帮助吗?在此先感谢。
答
改变了连接字符串
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ path + ";Extended Properties=Excel 12.0;";
答
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="
+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds,"table1");
return ds;
感谢..Got答案从那里 – Ananth 2010-12-01 09:32:28
@Kboek完美,谢谢! – 2013-05-02 19:54:07