当我尝试使用此代码读取 XML 工作表时,我有一个奇怪的行为:
string CONNEC_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No;IMEX=2;\"";
string fullFilePath = @"C:\Tmp\TestFile.xls";
using (OleDbConnection objCon = new OleDbConnection(string.Format(CONNEC_STRING, fullFilePath)))
{
using (OleDbCommand cm = new OleDbCommand("Select * From [MYCELLSRANGE]", objCon))
using (OleDbDataAdapter da = new OleDbDataAdapter(cm))
{
DataTable dt = new DataTable();
objCon.Open();
da.Fill(dt);
objCon.Close();
}
}
如果 Excel 文件已关闭,我会收到错误“外部表不是预期格式”。当我打开文件时,如果我执行上面的代码,它工作正常,我可以读取 MYCELLSRANGE 中包含的数据。那么,有人对这个问题有任何想法吗?感谢您的回答。