OleDbConnection()在任何打开的Excel窗口中打开一个Excel文件。但是如果没有一个窗口打开,则不会 [英] OleDbConnection() opens an Excel file in any open Excel window. But does not if there isnt a window open

查看:148
本文介绍了OleDbConnection()在任何打开的Excel窗口中打开一个Excel文件。但是如果没有一个窗口打开,则不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 OleDbAdapter 来访问Excel文件中的信息的应用程序。当我尝试创建与Excel文件的连接时,如果用户在桌面上打开了另一个(不相关的)Excel文件,则该适配器连接的文件将以只读格式在此窗口中打开。如果用户没有Excel的实例打开,则文件保持隐藏。

I am writing an application which uses an OleDbAdapter to access information in an Excel file. When I try to create a connection to the Excel file if the user has another (unrelated) Excel file open on their desktop then the file being connected to by the adapter opens in this window in Read-Only format. If the user does not have an instance of Excel open then the files stay hidden.

这是我的代码:

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的一个实例是开放的,但不会,如果没有?

Does anybody know why the OleDbConnection() would open a file if an instance of Excel was open but would not if one was not?

推荐答案

您应该发布代码来初始化您的应用程序变量。很可能你的问题的答案在于那里。
它是否使用GetObject或CreateObject方法?

You should post the code to initialize your apps variable. Most probably the answer to your question lies in there. Does it use a GetObject or CreateObject method?

这篇关于OleDbConnection()在任何打开的Excel窗口中打开一个Excel文件。但是如果没有一个窗口打开,则不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆