ASP. NET Word ecxel type file online preview

Source: Internet
Author: User

ASP. NET Word ecxel type file online preview

First you have to refer to com:

Microsoft Excel Object Library

Microsoft Word Ten Object Library

or a class library of more than 10.

What I'm using now: Resources download: http://download.csdn.net/detail/panfuy/3247641 or accessories

Microsoft Excel Object Library

Microsoft Word Ten Object Library

The code is as follows:

C # code
  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 file generates HTML and saves
  26. // </summary>
  27. /// <param name= "FilePath" > Path of ecxel file to be generated </param>
  28. /// <param name= "Savefilepath" > The path to save HTML files after generation </param>
  29. // <returns> Whether the build succeeds, the success is true, and vice versa is 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. /// Open File
  38. /* The following is the wording of the 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. /* The following is the wording of the Microsoft Excel Object Library: */
  41. _workbook xls = App. Workbooks.Open (FilePath, O, O, O, O, O, O, O, O, O, O, O, O, O, O);
  42. /// convert format, save as HTML
  43. /* The following is the wording of the Microsoft Excel 9 Object Library: */
  44. /*xls. SaveAs (Savefilepath, Excel.XlFileFormat.xlHtml, O, O, O, O, xlsaveasaccessmode.xlexclusive, O, O, O, O); */
  45. /* The following is the wording of the Microsoft Excel Object Library: */
  46. Xls. SaveAs (Savefilepath, Excel.XlFileFormat.xlHtml, O, O, O, O, xlsaveasaccessmode.xlexclusive, O, O, O, O, O);
  47. /// exit Excel
  48. App. Quit ();
  49. return true;
  50. }
  51. Catch
  52. {
  53. return false;
  54. }
  55. finally
  56. {
  57. //Close open Excel process last
  58. process[] myprocesses = Process.getprocessesbyname ("EXCEL");
  59. foreach (Process myprocess in myprocesses)
  60. {
  61. Myprocess.kill ();
  62. }
  63. }
  64. }
  65. // <summary>
  66. /// Winword file generates HTML and saves
  67. // </summary>
  68. /// <param name= "FilePath" > The path to the word file that needs to be generated </param>
  69. /// <param name= "Savefilepath" > The path to save HTML files after generation </param>
  70. // <returns> Whether the build succeeds, the success is true, and vice versa is 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. /// Open File
  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. /// convert format, save as HTML
  82. Type DocType = doc. GetType ();
  83. /* The following is the wording of the Microsoft Word 9 Object Library: */
  84. /*doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, NULL, doc, new object[] { Savefilepath, Word.WdSaveFormat.wdFormatHTML}); * *
  85. /* The following is the wording of the Microsoft Word Object Library: */
  86. Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
  87. null, doc, new object[] {savefilepath, Word.WdSaveFormat.wdFormatFilteredHTML});
  88. /// quit 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. //Finally close open Winword process
  99. process[] myprocesses = Process.getprocessesbyname ("Winword");
  100. foreach (Process myprocess in myprocesses)
  101. {
  102. Myprocess.kill ();
  103. }
  104. }
  105. }
  106. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.