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 }