asp.net Word ecxel类型文件在线预览
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
代码如下:
C#代码
- usingSystem;
- usingSystem.Data;
- usingSystem.Configuration;
- usingSystem.Collections;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.IO;
- usingSystem.Diagnostics;
- usingWord=Microsoft.Office.Interop.Word;
- usingExcel=Microsoft.Office.Interop.Excel;
- usingSystem.Reflection;
- usingMicrosoft.Office.Interop.Excel;
- publicpartialclassupload_PReview:System.Web.UI.Page
- {
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- GenerationWorDHTML("E://20110502.doc","E://20110502.html");
- GenerationExcelHTML("E://20110502.xls","E://20110502.html");
- }
- ///<summary>
- ///Ecxel文件生成HTML并保存
- ///</summary>
- ///<paramname="FilePath">需要生成的ecxel文件的路径</param>
- ///<paramname="saveFilePath">生成以后保存HTML文件的路径</param>
- ///<returns>是否生成成功,成功为true,反之为false</returns>
- protectedboolGenerationExcelHTML(stringFilePath,stringsaveFilePath)
- {
- try
- {
- Excel.applicationapp=newExcel.Application();
- app.Visible=false;
- Objecto=Missing.Value;
- ///打开文件
- /*下面是MicrosoftExcel9ObjectLibrary的写法:*/
- /*_Workbookxls=app.Workbooks.Open(FilePath,o,o,o,o,o,o,o,o,o,o,o,o);*/
- /*下面是MicrosoftExcel10ObjectLibrary的写法:*/
- _Workbookxls=app.Workbooks.Open(FilePath,o,o,o,o,o,o,o,o,o,o,o,o,o,o);
- ///转换格式,另存为HTML
- /*下面是MicrosoftExcel9ObjectLibrary的写法:*/
- /*xls.SaveAs(saveFilePath,Excel.XlFileFormat.xlHtml,o,o,o,o,XlSaveAsaccessMode.xlExclusive,o,o,o,o);*/
- /*下面是MicrosoftExcel10ObjectLibrary的写法:*/
- xls.SaveAs(saveFilePath,Excel.XlFileFormat.xlHtml,o,o,o,o,XlSaveAsAccessMode.xlExclusive,o,o,o,o,o);
- ///退出Excel
- app.Quit();
- returntrue;
- }
- catch
- {
- returnfalse;
- }
- finally
- {
- //最后关闭打开的excel进程
- Process[]myProcesses=Process.GetProcessesByName("EXCEL");
- foreach(ProcessmyProcessinmyProcesses)
- {
- myProcess.Kill();
- }
- }
- }
- ///<summary>
- ///WinWord文件生成HTML并保存
- ///</summary>
- ///<paramname="FilePath">需要生成的word文件的路径</param>
- ///<paramname="saveFilePath">生成以后保存HTML文件的路径</param>
- ///<returns>是否生成成功,成功为true,反之为false</returns>
- privateboolGenerationWordHTML(stringFilePath,stringsaveFilePath)
- {
- try
- {
- Word.ApplicationClassword=newWord.ApplicationClass();
- TypewordType=word.GetType();
- Word.Documentsdocs=word.Documents;
- ///打开文件
- TypedocsType=docs.GetType();
- Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,newObject[]{FilePath,true,true});
- ///转换格式,另存为HTML
- TypedocType=doc.GetType();
- /*下面是MicrosoftWord9ObjectLibrary的写法:*/
- /*docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,newobject[]{saveFilePath,Word.WdSaveFormat.wdFormatHTML});*/
- /*下面是MicrosoftWord10ObjectLibrary的写法:*/
- docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,
- null,doc,newobject[]{saveFilePath,Word.WdSaveFormat.wdFormatFilteredHTML});
- ///退出Word
- wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,word,null);
- returntrue;
- }
- catch
- {
- returnfalse;
- }
- finally
- {
- //最后关闭打开的winword进程
- Process[]myProcesses=Process.GetProcessesByName("WINWORD");
- foreach(ProcessmyProcessinmyProcesses)
- {
- myProcess.Kill();
- }
- }
- }
- }
- Microsoft.Office.Interop.Excel_Word.dll.zip(589.3 KB)