国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

asp.net word ecxel類型文件在線預覽

2019-11-17 01:37:48
字體:
來源:轉載
供稿:網(wǎng)友

asp.net Word ecxel類型文件在線預覽

asp.net word ecxel類型文件在線預覽

首先得引用COM:

Microsoft Excel 10 Object Library

Microsoft Word 10 Object Library

或者是 10以上的類庫

我現(xiàn)在用的是:資源下載: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)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 裕民县| 上蔡县| 莲花县| 崇文区| 淄博市| 将乐县| 田东县| 九寨沟县| 克拉玛依市| 崇文区| 凯里市| 射洪县| 五大连池市| 滨海县| 勐海县| 朔州市| 琼结县| 大理市| 阳曲县| 灵山县| 清河县| 龙南县| 吉木乃县| 同江市| 永清县| 金沙县| 红原县| 吴川市| 双辽市| 青川县| 仙居县| 渭南市| 广宁县| 阜城县| 涡阳县| 岗巴县| 柳江县| 天气| 桃园市| 永康市| 祁连县|