·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> asp.net word ecxel类型文件在线预览

asp.net word ecxel类型文件在线预览

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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#代码收藏代码
  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Configuration;
  4. usingSystem.Collections;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.WebControls;
  9. usingSystem.Web.UI.WebControls.WebParts;
  10. usingSystem.Web.UI.HtmlControls;
  11. usingSystem.IO;
  12. usingSystem.Diagnostics;
  13. usingWord=Microsoft.Office.Interop.Word;
  14. usingExcel=Microsoft.Office.Interop.Excel;
  15. usingSystem.Reflection;
  16. usingMicrosoft.Office.Interop.Excel;
  17. publicpartialclassupload_PReview:System.Web.UI.Page
  18. {
  19. protectedvoidPage_Load(objectsender,EventArgse)
  20. {
  21. GenerationWorDHTML("E://20110502.doc","E://20110502.html");
  22. GenerationExcelHTML("E://20110502.xls","E://20110502.html");
  23. }
  24. ///<summary>
  25. ///Ecxel文件生成HTML并保存
  26. ///</summary>
  27. ///<paramname="FilePath">需要生成的ecxel文件的路径</param>
  28. ///<paramname="saveFilePath">生成以后保存HTML文件的路径</param>
  29. ///<returns>是否生成成功,成功为true,反之为false</returns>
  30. protectedboolGenerationExcelHTML(stringFilePath,stringsaveFilePath)
  31. {
  32. try
  33. {
  34. Excel.applicationapp=newExcel.Application();
  35. app.Visible=false;
  36. Objecto=Missing.Value;
  37. ///打开文件
  38. /*下面是MicrosoftExcel9ObjectLibrary的写法:*/
  39. /*_Workbookxls=app.Workbooks.Open(FilePath,o,o,o,o,o,o,o,o,o,o,o,o);*/
  40. /*下面是MicrosoftExcel10ObjectLibrary的写法:*/
  41. _Workbookxls=app.Workbooks.Open(FilePath,o,o,o,o,o,o,o,o,o,o,o,o,o,o);
  42. ///转换格式,另存为HTML
  43. /*下面是MicrosoftExcel9ObjectLibrary的写法:*/
  44. /*xls.SaveAs(saveFilePath,Excel.XlFileFormat.xlHtml,o,o,o,o,XlSaveAsaccessMode.xlExclusive,o,o,o,o);*/
  45. /*下面是MicrosoftExcel10ObjectLibrary的写法:*/
  46. xls.SaveAs(saveFilePath,Excel.XlFileFormat.xlHtml,o,o,o,o,XlSaveAsAccessMode.xlExclusive,o,o,o,o,o);
  47. ///退出Excel
  48. app.Quit();
  49. returntrue;
  50. }
  51. catch
  52. {
  53. returnfalse;
  54. }
  55. finally
  56. {
  57. //最后关闭打开的excel进程
  58. Process[]myProcesses=Process.GetProcessesByName("EXCEL");
  59. foreach(ProcessmyProcessinmyProcesses)
  60. {
  61. myProcess.Kill();
  62. }
  63. }
  64. }
  65. ///<summary>
  66. ///WinWord文件生成HTML并保存
  67. ///</summary>
  68. ///<paramname="FilePath">需要生成的word文件的路径</param>
  69. ///<paramname="saveFilePath">生成以后保存HTML文件的路径</param>
  70. ///<returns>是否生成成功,成功为true,反之为false</returns>
  71. privateboolGenerationWordHTML(stringFilePath,stringsaveFilePath)
  72. {
  73. try
  74. {
  75. Word.ApplicationClassword=newWord.ApplicationClass();
  76. TypewordType=word.GetType();
  77. Word.Documentsdocs=word.Documents;
  78. ///打开文件
  79. TypedocsType=docs.GetType();
  80. Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,newObject[]{FilePath,true,true});
  81. ///转换格式,另存为HTML
  82. TypedocType=doc.GetType();
  83. /*下面是MicrosoftWord9ObjectLibrary的写法:*/
  84. /*docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,newobject[]{saveFilePath,Word.WdSaveFormat.wdFormatHTML});*/
  85. /*下面是MicrosoftWord10ObjectLibrary的写法:*/
  86. docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,
  87. null,doc,newobject[]{saveFilePath,Word.WdSaveFormat.wdFormatFilteredHTML});
  88. ///退出Word
  89. wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,word,null);
  90. returntrue;
  91. }
  92. catch
  93. {
  94. returnfalse;
  95. }
  96. finally
  97. {
  98. //最后关闭打开的winword进程
  99. Process[]myProcesses=Process.GetProcessesByName("WINWORD");
  100. foreach(ProcessmyProcessinmyProcesses)
  101. {
  102. myProcess.Kill();
  103. }
  104. }
  105. }
  106. }

  • Microsoft.Office.Interop.Excel_Word.dll.zip(589.3 KB)