我正在编写一个应用程序,它使用OleDbAdapter
来访问 Excel 文件中的信息。当我尝试创建与 Excel 文件的连接时,如果用户在其桌面上打开了另一个(不相关的)Excel 文件,则适配器连接到的文件会在此窗口中以只读格式打开。如果用户没有打开 Excel 实例,则文件将保持隐藏状态。
这是我的代码:
foreach (item app in apps)
{
DataTable dt = new DataTable();
string CnStr = ("Provider=Microsoft.Jet.OLEDB.4.0;" + ("Data Source="
+ ((app.FilePath) + (";" + "Extended Properties=\"Excel 8.0;\""))));
string OleDbString = ("Select * from [" + app.SheetName + "$]");
OleDbDataAdapter Adapter = new OleDbDataAdapter();
var conn = new OleDbConnection(CnStr);
conn.Open(); <----------------------------This is where the files are being opened.
var cmd = new OleDbCommand(OleDbString, conn);
Adapter.SelectCommand = cmd;
Adapter.Fill(app.DataTable);
conn.Close();
Adapter.Dispose();
}
有谁知道为什么OleDbConnection()
如果 Excel 实例打开了会打开文件,但如果没有打开则不会?