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

asp.net word ecxel类型文件在线预览
 
首先得引用COM:
 
Microsoft Excel 10 Object Library
 

Microsoft Word 10 Object Library

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using System.Diagnostics;
  13. using Word = Microsoft.Office.Interop.Word;
  14. using Excel = Microsoft.Office.Interop.Excel;
  15. using System.Reflection;
  16. using Microsoft.Office.Interop.Excel;
  17. public partial class upload_preview : System.Web.UI.Page
  18. {
  19. protected void Page_Load(object sender, EventArgs e)
  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. /// <param name="FilePath">需要生成的ecxel文件的路径</param>
  28. /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>
  29. /// <returns>是否生成成功,成功为true,反之为false</returns>
  30. protected bool GenerationExcelHTML(string FilePath, string saveFilePath)
  31. {
  32. try
  33. {
  34. Excel.Application app = new Excel.Application();
  35. app.Visible = false;
  36. Object o = Missing.Value;
  37. ///打开文件
  38. /*下面是Microsoft Excel 9 Object Library的写法: */
  39. /*_Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o);*/
  40. /*下面是Microsoft Excel 10 Object Library的写法: */
  41. _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o);
  42. ///转换格式,另存为 HTML
  43. /*下面是Microsoft Excel 9 Object Library的写法: */
  44. /*xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);*/
  45. /*下面是Microsoft Excel 10 Object Library的写法: */
  46. xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);
  47. ///退出 Excel
  48. app.Quit();
  49. return true;
  50. }
  51. catch
  52. {
  53. return false;
  54. }
  55. finally
  56. {
  57. //最后关闭打开的excel 进程
  58. Process[] myProcesses = Process.GetProcessesByName("EXCEL");
  59. foreach (Process myProcess in myProcesses)
  60. {
  61. myProcess.Kill();
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// WinWord文件生成HTML并保存
  67. /// </summary>
  68. /// <param name="FilePath">需要生成的word文件的路径</param>
  69. /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>
  70. /// <returns>是否生成成功,成功为true,反之为false</returns>
  71. private bool GenerationWordHTML(string FilePath, string saveFilePath)
  72. {
  73. try
  74. {
  75. Word.ApplicationClass word = new Word.ApplicationClass();
  76. Type wordType = word.GetType();
  77. Word.Documents docs = word.Documents;
  78. /// 打开文件
  79. Type docsType = docs.GetType();
  80. Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { FilePath, true, true });
  81. /// 转换格式,另存为 HTML
  82. Type docType = doc.GetType();
  83. /*下面是Microsoft Word 9 Object Library的写法: */
  84. /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatHTML });*/
  85. /*下面是Microsoft Word 10 Object Library的写法: */
  86. docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
  87. null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML });
  88. /// 退出 Word
  89. wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
  90. return true;
  91. }
  92. catch
  93. {
  94. return false;
  95. }
  96. finally
  97. {
  98. //最后关闭打开的winword 进程
  99. Process[] myProcesses = Process.GetProcessesByName("WINWORD");
  100. foreach (Process myProcess in myProcesses)
  101. {
  102. myProcess.Kill();
  103. }
  104. }
  105. }
  106. }    
  107. 本文来自http://www.1314721.com.cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值