[DllImport("user32.dll", EntryPoint = "MoveWindow")] static extern bool MoveWindow( int Wnd, int X, int Y, int Width, int Height, bool Repaint ); [DllImport("user32.dll")] static extern int SetParent(int hWndChild, int hWndNewParent); private Excel.Application _application; public Excel.Application Application { get { return _application; } set { _application = value; } } private int _excelApplicationWnd; public int ExcelApplicationWnd { get { return _excelApplicationWnd; } set { _excelApplicationWnd = value; } } private Form _excelForm; public int ExcelForm { get { return _excelForm; } set { _excelForm = value; } } public ExcelParse(Form form) { this.ExcelForm=form; this.ExcelForm.Resize += new EventHandler(_form_Resize); } void _form_Resize(object sender, EventArgs e) { ExcelApplicationWndResize(); } public void ExcelConfig() { if (this.Application == null) this.Application = new Microsoft.Office.Interop.Excel.ApplicationClass(); this.ControlExcelApplicationWnd(); //this.Application.Visible = true;//设置Application默认打开显示不显示 this.Application.DisplayAlerts = false;//设置不弹出提示 this.Application.DisplayStatusBar = false; Application.DisplayFormulaBar = false;//设置不显示公式计算 IEnumerator ie = Application.CommandBars.GetEnumerator();//禁用所有工具条 while (ie.MoveNext()) { Core.CommandBar commandBar = (Core.CommandBar)ie.Current; string name = commandBar.Name; try { commandBar.Enabled = false; } catch (Exception ex) { throw ex; } } } public void ControlExcelApplicationWnd() { this.ExcelApplicationWnd = this.Application.Hwnd; this.Application.ShowWindowsInTaskbar = false; this.Application.CommandBars.ActiveMenuBar.Enabled = false; SetParent(ExcelApplicationWnd, this.ExcelForm.Handle.ToInt32()); ExcelApplicationWndResize(); } private void ExcelApplicationWndResize() { if (this.ExcelApplicationWnd != 0) { int borderWidth = SystemInformation.Border3DSize.Width; int borderHeight = SystemInformation.Border3DSize.Height; int captionHeight = SystemInformation.CaptionHeight; int statusHeight = SystemInformation.ToolWindowCaptionHeight; MoveWindow(ExcelApplicationWnd, -4 * borderWidth, -3 * borderHeight - captionHeight, this._ExcelForm.Bounds.Width + 8 * borderWidth, this.ExcelForm.Bounds.Height + captionHeight + 4 * borderHeight + statusHeight, true); } }