none
关于Excel控件 RRS feed

  • 问题

  • 我在书上看到一段代码,如下:

    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的界面一直停留到我手动关闭程序为止啊???

     

    2012年2月1日 7:58

答案

全部回复

  • 见代码,取得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/

    2012年2月1日 16:20
  • 您如果沒有加入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/
    2012年2月2日 1:20
  • 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/
    2012年2月2日 5:03
  • 原来如此,我明白了,谢谢‘
    2012年2月3日 2:29