winform WebBrowser查看WORD和EXCEL

-------word-------------------- 
private void InitWeb() 
{ 
if (webBrowser1 == null) 
{ 
this.webBrowser1 = new System.Windows.Forms.WebBrowser(); 
this.webBrowser1.AllowWebBrowserDrop = false; 
this.webBrowser1.IsWebBrowserContextMenuEnabled = false; 
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); 
this.webBrowser1.Name = "webBrowser_ " + this.Name; 
this.webBrowser1.WebBrowserShortcutsEnabled = false; 
this.webBrowser1.Location = new System.Drawing.Point(0, 0); 
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
fm.fm.Controls.Add(webBrowser1); 
} 
this.webBrowser1.Size = new System.Drawing.Size(Rect.Width + 20, Rect.Height + 20); 
} 
public void InitPage(bool bFlag) 
{ 
InitWeb(); 
if (!File.Exists(TempHttp)) 
{ 
bFlag = true; 
} 
if (bFlag && File.Exists(FilePath)) 
{ 
#region 保存网页副本 
Word.ApplicationClass word = new Word.ApplicationClass(); 
object missingObj = Type.Missing; 
object readOnly = false; 
object file = FilePath; 
object show = true; 
Word.Document doc = word.Documents.Open(ref file, 
ref missingObj, 
ref readOnly, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref show, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj); 

FilePath = FilePath.ToLower(); 
TempHttp = FilePath.Replace( ".doc ", "_word.htm "); 

object tmpFile = TempHttp; 
object saveType = Word.WdSaveFormat.wdFormatHTML; 

doc.SaveAs(ref tmpFile, 
ref saveType, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj, 
ref missingObj); 
object notSaveChange = false; 
word.Quit(ref notSaveChange, ref missingObj, ref missingObj); 
#endregion 
LoadFlag = false; 
} 
if (LoadFlag == false) 
{ 
webBrowser1.Navigate((string)TempHttp); 
LoadFlag = true; 
} 
} 
----------------------excel------------- 
private void InitWeb() 
{ 
if (webBrowser1 == null) 
{ 
this.webBrowser1 = new System.Windows.Forms.WebBrowser(); 
this.webBrowser1.AllowWebBrowserDrop = false; 
this.webBrowser1.IsWebBrowserContextMenuEnabled = false; 
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); 
this.webBrowser1.Name = "webBrowser_ " + this.Name; 
this.webBrowser1.WebBrowserShortcutsEnabled = false; 
this.webBrowser1.Location = new System.Drawing.Point(0, 0); 
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
fm.fm.Controls.Add(webBrowser1); 
} 
this.webBrowser1.Size = new System.Drawing.Size(Rect.Width + 20, Rect.Height + 20); 
} 
public void InitPage(bool bFlag) 
{ 
InitWeb(); 
if (!File.Exists(TempHttp)) 
{ 
bFlag = true; 
} 
if (bFlag && File.Exists(FilePath)) 
{ 
#region 保存网页副本 
Excel.ApplicationClass execl = new Excel.ApplicationClass(); 

object missingObj = Type.Missing; 
object readOnly = false; 
string file = FilePath; 
object show = true; 

Excel.Workbook doc = execl.Workbooks.Open(file, 
missingObj, 
readOnly, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj); 

FilePath = FilePath.ToLower(); 
TempHttp = FilePath.Replace( ".xls ", "_excel.htm "); 
try 
{ 
if (File.Exists(TempHttp)) 
{ 
File.Delete(TempHttp); 
} 
} 
catch { } 

object tmpFile = TempHttp; 
object saveType = Excel.XlFileFormat.xlHtml; 
doc.SaveAs(tmpFile, 
saveType, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
Excel.XlSaveAsAccessMode.xlNoChange, 
missingObj, 
missingObj, 
missingObj, 
missingObj, 
missingObj); 

object notSaveChange = false; 
execl.Quit(); 
#endregion 
LoadFlag = false; 
} 
if (LoadFlag == false) 
{ 
webBrowser1.Navigate((string)TempHttp); 
LoadFlag = true; 
} 
}


 

你可能感兴趣的:(C#)