asp.net word ecxel类型文件在线预览
首先得引用COM:
Microsoft Excel 10 Object Library
Microsoft Word 10 Object Library
或者是 10以上的类库
我现在用的是:资源下载: http://download.csdn.net/detail/panfuy/3247641 或者附件
Microsoft Excel 10 Object Library
Microsoft Word 10 Object Library
代码如下:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO;
- using System.Diagnostics;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Reflection;
- using Microsoft.Office.Interop.Excel;
-
-
- public partial class upload_preview : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- GenerationWordHTML("E://20110502.doc", "E://20110502.html");
- GenerationExcelHTML("E://20110502.xls", "E://20110502.html");
- }
-
-
-
-
-
-
-
- protected bool GenerationExcelHTML(string FilePath, string saveFilePath)
- {
- try
- {
- Excel.Application app = new Excel.Application();
- app.Visible = false;
- Object o = Missing.Value;
-
-
-
-
-
-
- _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o);
-
-
-
-
-
-
- xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);
-
-
- app.Quit();
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
-
- Process[] myProcesses = Process.GetProcessesByName("EXCEL");
- foreach (Process myProcess in myProcesses)
- {
- myProcess.Kill();
- }
- }
- }
-
-
-
-
-
-
-
- private bool GenerationWordHTML(string FilePath, string saveFilePath)
- {
- try
- {
- Word.ApplicationClass word = new Word.ApplicationClass();
- Type wordType = word.GetType();
- Word.Documents docs = word.Documents;
-
-
- Type docsType = docs.GetType();
- Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { FilePath, true, true });
-
-
- Type docType = doc.GetType();
-
-
-
-
-
- docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML });
-
-
- wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
-
- Process[] myProcesses = Process.GetProcessesByName("WINWORD");
- foreach (Process myProcess in myProcesses)
- {
- myProcess.Kill();
- }
- }
- }
- }