积极答复者
关于Excel控件

问题
-
我在书上看到一段代码,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; using System.Reflection; using System.Threading; namespace 显示Excel { class Program { static void Main(string[] args) { Microsoft.Office.Interop.Excel.Application excel = new Application(); excel.Visible = true; Thread.Sleep(10000); } } }
上面是显示 Excel的界面,其中thread.sleep(10000)是让界面停留10秒钟那我要怎么修改才使得:Excel的界面一直停留到我手动关闭程序为止啊???
答案
-
dear
1.您指的是Console的界面还是EXCEL的界面?
若是Console就用 Console.ReadKey();或 Console.Read();
你可能也会面临EXCEL界面消失,但事实上它还是在背景里执行
你必须要处理Excel的回收
http://www.dotblogs.com.tw/yc421206/archive/2009/07/16/9553.aspx
2.下列还有一个EXCEL使用参考连结
http://www.dotblogs.com.tw/yc421206/archive/2009/01/11/6727.aspx
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/- 已编辑 Lie YouModerator 2012年2月2日 6:25 links
- 已建议为答案 Lie YouModerator 2012年2月2日 6:35
- 已标记为答案 星空雏菊 2012年2月3日 2:29
全部回复
-
见代码,取得Excel对象的句柄,然后循环检查即可,VS2010+office2010下通过,同时期待win8是不是可以不用dllImport
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; using System.Reflection; using System.Threading; using System.Runtime.InteropServices; namespace ConsoleApplication3 { class Program { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindow(IntPtr hWnd); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); static void Main(string[] args) { Microsoft.Office.Interop.Excel.Application excel = new Application(); excel.Visible = true; Thread t = new Thread( new ThreadStart(() => { while (true) { Console.WriteLine("Now Date:{0}", DateTime.Now); if (!IsWindowVisible((IntPtr)excel.Hwnd)) { Console.WriteLine("end"); Thread.CurrentThread.Abort(); } Thread.Sleep(1000); } } )); t.Start(); } } }
帮助大家解决问题咯~~小站:http://www.cnblogs.com/knightluffy/
- 已编辑 knightluffy 2012年2月1日 16:21
- 已建议为答案 Lie YouModerator 2012年2月2日 6:35
-
您如果沒有加入Console.ReadKey();的話,Console程式會自動結束哦!
所以在最後加個Console.ReadKey();就可以了吧!
不過,這是您要的嗎!?
static void Main(string[] args)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
Thread.Sleep(10000);
Console.ReadKey();
}
亂馬客blog: http://www.dotblogs.com.tw/rainmaker/- 已建议为答案 Lie YouModerator 2012年2月2日 6:35
-
dear
1.您指的是Console的界面还是EXCEL的界面?
若是Console就用 Console.ReadKey();或 Console.Read();
你可能也会面临EXCEL界面消失,但事实上它还是在背景里执行
你必须要处理Excel的回收
http://www.dotblogs.com.tw/yc421206/archive/2009/07/16/9553.aspx
2.下列还有一个EXCEL使用参考连结
http://www.dotblogs.com.tw/yc421206/archive/2009/01/11/6727.aspx
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/- 已编辑 Lie YouModerator 2012年2月2日 6:25 links
- 已建议为答案 Lie YouModerator 2012年2月2日 6:35
- 已标记为答案 星空雏菊 2012年2月3日 2:29