zoukankan      html  css  js  c++  java
  • 通过Win32API方式,将Excel嵌入Form窗体

     

     1  [DllImport("user32.dll", EntryPoint = "MoveWindow")]
     2         static extern bool MoveWindow(
     3             int Wnd,
     4             int X,
     5             int Y,
     6             int Width,
     7             int Height,
     8             bool Repaint
     9             );
    10         [DllImport("user32.dll")]
    11         static extern int SetParent(int hWndChild, int hWndNewParent);
    12 
    13         private Excel.Application _application;
    14 
    15         public Excel.Application Application
    16         {
    17             get { return _application; }
    18             set { _application = value; }
    19         }
    20 
    21         private int _excelApplicationWnd;
    22 
    23         public int ExcelApplicationWnd
    24         {
    25             get { return _excelApplicationWnd; }
    26             set { _excelApplicationWnd = value; }
    27         }
    28 
    29         private Form _excelForm;
    30          public Form ExcelForm        {
    31             get { return _excelForm; }
    32             set { _excelForm = value; }
    33         }
    34 
    35 
    36         public ExcelParse(Form form)
    37         {
    38            this.ExcelForm=form;
    39            this.ExcelForm.Resize += new EventHandler(_form_Resize);
    40         }
    41          void _form_Resize(object sender, EventArgs e)
    42         {
    43             ExcelApplicationWndResize();
    44         }
    45          public void ExcelConfig()
    46         {
    47             if (this.Application == null)
    48                 this.Application = new Microsoft.Office.Interop.Excel.ApplicationClass();
    49             this.ControlExcelApplicationWnd();
    50             //this.Application.Visible = true;//设置Application默认打开显示不显示
    51             this.Application.DisplayAlerts = false;//设置不弹出提示
    52             this.Application.DisplayStatusBar = false;
    53             Application.DisplayFormulaBar = false;//设置不显示公式计算
    54             IEnumerator ie = Application.CommandBars.GetEnumerator();//禁用所有工具条
    55             while (ie.MoveNext())
    56             {
    57                 Core.CommandBar commandBar = (Core.CommandBar)ie.Current;
    58                 string name = commandBar.Name;
    59                 try
    60                 {
    61                     commandBar.Enabled = false;
    62                 }
    63                 catch (Exception ex)
    64                 {
    65                     throw ex;
    66                 }
    67 
    68             }
    69            
    70            
    71         }
    72  public void ControlExcelApplicationWnd()
    73         {
    74             this.ExcelApplicationWnd = this.Application.Hwnd;
    75             this.Application.ShowWindowsInTaskbar = false;
    76             this.Application.CommandBars.ActiveMenuBar.Enabled = false;
    77             SetParent(ExcelApplicationWnd, this.ExcelForm.Handle.ToInt32());
    78            
    79             ExcelApplicationWndResize();
    80         }
    81 
    82         private void ExcelApplicationWndResize()
    83         {
    84             if (this.ExcelApplicationWnd != 0)
    85             {
    86                 int borderWidth = SystemInformation.Border3DSize.Width;
    87                 int borderHeight = SystemInformation.Border3DSize.Height;
    88                 int captionHeight = SystemInformation.CaptionHeight;
    89                 int statusHeight = SystemInformation.ToolWindowCaptionHeight;
    90                 MoveWindow(ExcelApplicationWnd, -4 * borderWidth, -3 * borderHeight - captionHeight, this._ExcelForm.Bounds.Width + 8 * borderWidth, this.ExcelForm.Bounds.Height + captionHeight + 4 * borderHeight + statusHeight, true);
    91             }
    92         }
  • 相关阅读:
    javaBean的理解
    配置tomcat8数据源(采用局部数据源方式)
    windows下apache报os 10048错误
    Windows下Apache的下载安装启动停止
    java通过数据库连接池链接oracle
    java连接oracle数据库
    eclipse配置svn方法
    JAVA多线程中start方法与run方法区别
    java程序在没有java环境的电脑上执行的方法(关键词jar,exe)
    js监听不到组合键
  • 原文地址:https://www.cnblogs.com/Ruiky/p/2550477.html
Copyright © 2011-2022 走看看