vb.net中怎样关闭excel文件

hvnicd 2007-12-12 10:06:55
我要在vb.net中实现对一个存在的excel文件进行写操作.

自己在调试过程中执行到 xlBook.Close() 这个操作的时候,就停止不动了.
请问怎样关闭excel.
...全文
1221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yangbin990 2007-12-15
  • 打赏
  • 举报
回复
up
dmarkplus 2007-12-15
  • 打赏
  • 举报
回复
Start Excel and get its process ID:

'
' Verify the Excel file to populate exists.
'
Dim strExcelFile As String = "C:\Application_Directory\MyExcel.xls"
If Not IO.File.Exists(strExcelFile) Then Return
'
' Start Microsoft Excel and set it visible.
'
myExcel = New Excel.Application

If myExcel Is Nothing Then
Throw (New Exception("Failed to Start Microsoft Excel."))
Else
myExcel.Visible = True
myExcel.WindowState = Excel.XlWindowState.xlMaximized
'
' Get the process ID of the Excel instance. This is used
' in the Closing Event to prevent orphaned Excel processes.
'
Dim aProcesses() As Process = Process.GetProcesses
Dim i As Integer

For i = 0 To aProcesses.GetUpperBound(0)
If aProcesses(i).MainWindowHandle.ToString = myExcel.Hwnd.ToString Then
myExcelPID = aProcesses(i).Id
Exit For
End If
Next
End If
zch260812542 2007-12-14
  • 打赏
  • 举报
回复
请问如何在开启excel的时候获得它的进程ID啊?
dmarkplus 2007-12-14
  • 打赏
  • 举报
回复
楼上的方法不可取,因为如果有别人也在server 上用excel,都会被KILL 掉。

建议lz,在你开启excel的时候,得到Process ID, 等用完以后,你可以KILL Process ID.
xiaoqmei 2007-12-13
  • 打赏
  • 举报
回复
窗体代码前定义实例对象: Private AppExcel As Excel.Application = New Excel.Application
然后在窗体关闭或退出时加入以下代码:
AppExcel.Application.Workbooks.Close() '关闭Excel表格
AppExcel.Quit() '销毁Excel.exe进程

我这么全OK!
chenglidexiaoxue 2007-12-13
  • 打赏
  • 举报
回复
Dim pProcess() As Process
pProcess = Process.GetProcesses()
Dim i As Integer
For i = 0 To pProcess.Length() - 1
If (pProcess(i).ProcessName = "EXCEL") Then
pProcess(i).Kill() ''关闭进程
End If
Next
yagangshi 2007-12-13
  • 打赏
  • 举报
回复
xlBook.Close(nothing,nothing,nothing)
xiaoyuandy 2007-12-12
  • 打赏
  • 举报
回复
application.close
水如烟 2007-12-12
  • 打赏
  • 举报
回复
以前写的,你可以参考你需要的部分.

http://dev.csdn.net/author/lzmtw/4db1da0f4a3b489da42e33b94b2a38e2.html

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧