两个程序试图读取一个成功的人失败,相同的代码
我有两个彼此不相关的程序,但他们有一个相同的方法,他们尝试从Excel文件中读取行。
计划1
public DataTable GetExcelInfo(string filepath) { DataTable datatab = new DataTable(); try { string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"; using (OleDbConnection conn = new OleDbConnection(connectionString)) { conn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT Format([F1], \"#\"), Format([F2], \"#\"), Format([F3], \"#\") FROM [Sheet1$]", conn); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string[] values = new string[3]; values[0] = reader.GetString(0); values[1] = reader.GetString(1); values[2] = reader.GetString(2); DataRow dr = datatab.NewRow(); dr.ItemArray = values; datatab.Rows.InsertAt(dr, i); i++; } } } }
计划2
private static DataTable GetInvoiceItems(string filepath) { DataTable dt = new DataTable(); string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"; using (OleDbConnection conn = new OleDbConnection(excelConString)) { OleDbDataAdapter ada = new OleDbDataAdapter("SELECT [F1], [F2], [F3], [F4], [F5] FROM [Sheet1$]", conn); conn.Open(); ada.Fill(dt); } return dt; }
现在奇怪的是第一个程序完全运行在同一台电脑上,而第二个程序得到一个错误,说在电脑上没有可安装的ISAM。 有什么build议么?
尽pipe你们的说法是一致的,但如果你仔细观察,他们不是。 看看你的连接string…
计划1
"... Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"
计划2
"... Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"
程序2中的Excel 8.0
中不应该有=
。