我有两个彼此无关的程序,但它们具有相同的方法,它们尝试从 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++;
}
}
}
}
节目二
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。有什么建议么?