通過Win32API方式,將Excel嵌入Form表單

來源:互聯網
上載者:User

 

 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 Application16         {17             get { return _application; }18             set { _application = value; }19         }20 21         private int _excelApplicationWnd;22 23         public int ExcelApplicationWnd24         {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                 try60                 {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         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.