c# 如何获取excel对象的句柄?????

freewind0521 2009-08-25 06:27:26
C#使用释放excel资源时候,在网上看到这样一段代码,但是却没办法用,请指点

using System.Runtime.InteropServices;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
protected void Button1_Click(object sender, EventArgs e)
{
Excel.ApplicationClass myExcel= new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Open("d:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
IntPtr t = new IntPtr(myExcel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}

//怎样才能获得myExcel.Hwnd?我在myExcel后打点时,没有Hwnd出现,是缺少什么引用吗?

原文地址
http://www.cppblog.com/colys/articles/25265.html
...全文
706 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoutei 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 freewind0521 的回复:]
引用 11 楼 zgke 的回复:
你引用的是什么版本的 EXCEL..

我安装的是office2003但是在COM参照里面添加micrexcel 11.0 object libraryosoft 后并没有在bin文件夹下将相应的dll导入,添加micrexcel 5.0 object libraryosoft 倒是成功了,会不会是5.0的类库不支持呢?
[/Quote]

需要先安装office2003,然后再安装vs,在安装vs时选择office的相关组件,这样才能在vs中正确引用com组件
freewind0521 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zgke 的回复:]
你引用的是什么版本的 EXCEL..
[/Quote]
我安装的是office2003但是在COM参照里面添加micrexcel 11.0 object libraryosoft 后并没有在bin文件夹下将相应的dll导入,添加micrexcel 5.0 object libraryosoft 倒是成功了,会不会是5.0的类库不支持呢?
zgke 2009-08-26
  • 打赏
  • 举报
回复
你引用的是什么版本的 EXCEL..
lfywy 2009-08-26
  • 打赏
  • 举报
回复
学习
freewind0521 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
Excel.Application excelApp= new Excel.Application();
Excel.Workbook excelWorkBook;

            try
            {
              excelWorkBook.Close(true, Type.Missing, Type.Missing);
              excelApp.Quit();
            }
            catch
            {
                IntPtr t = new IntPtr(excelApp.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();
            }
            excelWorkBook = null;
            excelApp = null;
            GC.Collect();

[/Quote]

问题是我得不到excelApp.Hwnd,我发现excelApp里没有Hwnd属性,没有Hwnd的定义,怎样才能定义Hwnd?需要什么引用吗?我的是web
春天的气息 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
Excel.Application excelApp= new Excel.Application();
Excel.Workbook excelWorkBook;

            try
            {
              excelWorkBook.Close(true, Type.Missing, Type.Missing);
              excelApp.Quit();
            }
            catch
            {
                IntPtr t = new IntPtr(excelApp.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();
            }
            excelWorkBook = null;
            excelApp = null;
            GC.Collect();

[/Quote]

顶一下,这个答案解决的很不错
超维电脑科技 2009-08-25
  • 打赏
  • 举报
回复
学习
wuyq11 2009-08-25
  • 打赏
  • 举报
回复
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
Excel.Application excelApp= new Excel.Application();
Excel.Workbook excelWorkBook;

try
{
excelWorkBook.Close(true, Type.Missing, Type.Missing);
excelApp.Quit();
}
catch
{
IntPtr t = new IntPtr(excelApp.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
excelWorkBook = null;
excelApp = null;
GC.Collect();
dancingbit 2009-08-25
  • 打赏
  • 举报
回复
要获得窗口句柄?使用API函数FindWindow。
freewind0521 2009-08-25
  • 打赏
  • 举报
回复
自己顶
freewind0521 2009-08-25
  • 打赏
  • 举报
回复
IntPtr t = new IntPtr(myExcel.Hwnd);
如何得到myExcel.Hwnd?
liffe 2009-08-25
  • 打赏
  • 举报
回复
帮顶

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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