最近開發 Winform 應用程式,涉及到 嵌入Excel 檔案。技術驗證也快結束,需要整理一下協助文檔了。。
用webBrowser控制項 開啟Excel 。
/// <summary>
///
/// </summary>
public static Microsoft.Office.Interop.Excel.Range range = null;
/// <summary>
///
/// </summary>
public static Microsoft.Office.Interop.Excel.Workbook wbb = null;
/// <summary>
/// 開啟Excel檔案
/// </summary>
/// <param name="filePath">Excel檔案路徑</param>
public void OpenExcel(string filePath)
{
strFilePathAndFileName = filePath;
//Object refmissing = System.Reflection.Missing.Value;
this.webBrowser1.Navigate(strFilePathAndFileName);
//object axWebBrowser = this.webBrowser1.ActiveXInstance;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Object refmissing = System.Reflection.Missing.Value;
object[] args = new object[4];
args[0] = SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS;
args[1] = SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER;
args[2] = refmissing;
args[3] = refmissing;
object axWebBrowser = this.webBrowser1.ActiveXInstance;
axWebBrowser.GetType().InvokeMember("ExecWB", BindingFlags.InvokeMethod, null, axWebBrowser, args);
object oApplication = axWebBrowser.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, axWebBrowser, null);
wbb = (Microsoft.Office.Interop.Excel.Workbook)oApplication;
}