注册 登录
编程论坛 C# 论坛

[求助]紧急 请问如何获得已经打开的excel文件的文件路径,文件名?

yuanpu111 发布于 2006-08-23 10:56, 5321 次点击


[求助]紧急 请问如何获得已经打开的excel文件的文件路径,文件名?
我目前的程序需要获得当前正在打开的所有excel文件的列表,
(所有当前正在打开的excel文件都需要取得,可以用任何方式打开。)
我已经查找了很久,都不能找到C#的方法,
请高手帮忙,多谢!!!
跪求高手帮忙!!!
相关:
这里有一个VB的方法
http://www.bczs.net/xml/2003/4/26/1711527.xml

关于此方法的msn信息在GetObject函数 Visual Basic中。

贴上一段相关代码:
' Add Option Strict Off to the top of your program.
Option Strict Off
' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application.
' If the application is not running, an error occurs.
MyXL = GetObject(, "Excel.Application")
If Err().Number <> 0 Then ExcelWasNotRunning = True
Err().Clear() ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel()

' Set the object variable to reference the file you want to see.
MyXL = GetObject("c:\vb\TEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
' Do manipulations of your file here.
'
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage(hWnd, WM_USER + 18, 0, 0)
End If
End Sub

[此贴子已经被作者于2006-8-23 11:08:57编辑过]

10 回复
#2
mylover6242006-08-23 11:01
OpenFileDialog fd = new OpenFileDialog();
if( fd.ShowDialog() == DialogResult.OK )
{
this.Lable1.Text = fd.FileName;
}
#3
yuanpu1112006-08-23 11:05

谢谢楼上的热心人!
不过文件可能不是通过我的程序打开的,比如用户在windows下双击打开,就是说凡是当前系统中打开的excel文件都需要找出来,
而不仅仅是通过执行我程序中的openfiledialog打开的。
谢谢各位帮忙!

[此贴子已经被作者于2006-8-23 11:07:15编辑过]

#4
mylover6242006-08-23 11:11

那你通過系統的“任務管理器”找打開狀態的“Excel”線程吧

#5
yuanpu1112006-08-23 11:21

我曾经试过word有3个线程,当打开一个新文件的时候并不增加新的线程(我也很奇怪)。
excel应该也是这样。
而且我不知道怎么通过线程或进程取得打开的文件名。
通过excel的进程主模块, 只能取得excel.exe的路径,并不能得到被它打开的文件的信息。

#6
chenjin1452006-08-23 11:28

能不能彎一下
去找註冊表中的打開文件歷史紀錄

#7
小海龟2006-08-25 13:13
以下是引用yuanpu111在2006-8-23 10:56:55的发言:


[求助]紧急 请问如何获得已经打开的excel文件的文件路径,文件名?
我目前的程序需要获得当前正在打开的所有excel文件的列表,
(所有当前正在打开的excel文件都需要取得,可以用任何方式打开。)
我已经查找了很久,都不能找到C#的方法,
请高手帮忙,多谢!!!
跪求高手帮忙!!!
相关:
这里有一个VB的方法
http://www.bczs.net/xml/2003/4/26/1711527.xml

关于此方法的msn信息在GetObject函数 Visual Basic中。

贴上一段相关代码:
' Add Option Strict Off to the top of your program.
Option Strict Off
' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application.
' If the application is not running, an error occurs.
MyXL = GetObject(, "Excel.Application")
If Err().Number <> 0 Then ExcelWasNotRunning = True
Err().Clear() ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel()

' Set the object variable to reference the file you want to see.
MyXL = GetObject("c:\vb\TEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
' Do manipulations of your file here.
'
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage(hWnd, WM_USER + 18, 0, 0)
End If
End Sub


只有本站会员才能查看附件,请 登录

看看吧,不过打开的Excel文件要刷新才能全部获得。
#8
live412006-08-25 13:59

不错啊,还差路径

#9
小海龟2006-08-25 14:12
路径实际上不是已经获得了吗?字符串的前部分。
#10
xslonglianxi2009-07-23 15:52
我想得到当前打开的Excel的所有Application对象!!!!怎么办啊!!
#11
kemiao2014-05-13 14:42
这个问题解决了吗??求答案
1