Asp.net word ecxel file online preview, asp. netecxel

Source: Internet
Author: User
Tags asp net

Asp.net word ecxel file online preview, asp. netecxel

Asp.net word ecxel file online preview

First, you must reference COM:

Microsoft Excel 10 Object Library

Microsoft Word 10 Object Library

 

Or more than 10 Class Libraries

 

I am using: Resource download: http://download.csdn.net/detail/panfuy/3247641 orAttachment

Microsoft Excel 10 Object Library

Microsoft Word 10 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. /// Generate HTML and save the Ecxel File
  26. /// </Summary>
  27. /// <Param name = "FilePath"> path of the ecxel file to be generated </param>
  28. /// <Param name = "saveFilePath"> path for saving the HTML file after generation </param>
  29. /// <Returns> whether the generation is successful. If the generation succeeds, the value true; otherwise, the value 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 the file
  38. /* The following is Microsoft Excel 9 Object Library :*/
  39. /* _ Workbook xls = app. workbooks. open (FilePath, o, o );*/
  40. /* The following is Microsoft Excel 10 Object Library :*/
  41. _ Workbook xls = app. workbooks. open (FilePath, o, o );
  42. /// Convert the format and save it as HTML
  43. /* The following is Microsoft Excel 9 Object Library :*/
  44. /* Xls. SaveAs (saveFilePath, Excel. XlFileFormat. xlHtml, o, XlSaveAsAccessMode. xlExclusive, o, o );*/
  45. /* The following is Microsoft Excel 10 Object Library :*/
  46. Xls. SaveAs (saveFilePath, Excel. XlFileFormat. xlHtml, o, XlSaveAsAccessMode. xlExclusive, o, o );
  47. /// Exit Excel
  48. App. Quit ();
  49. Return true;
  50. }
  51. Catch
  52. {
  53. Return false;
  54. }
  55. Finally
  56. {
  57. // Close the excel process.
  58. Process [] myProcesses = Process. GetProcessesByName ("EXCEL ");
  59. Foreach (Process myProcess in myProcesses)
  60. {
  61. MyProcess. Kill ();
  62. }
  63. }
  64. }
  65. /// <Summary>
  66. /// Generate HTML and save the WinWord File
  67. /// </Summary>
  68. /// <Param name = "FilePath"> path of the word file to be generated </param>
  69. /// <Param name = "saveFilePath"> path for saving the HTML file after generation </param>
  70. /// <Returns> whether the generation is successful. If the generation succeeds, the value true; otherwise, the value 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 the 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 the format and save it as HTML
  82. Type docType = doc. GetType ();
  83. /* The following is 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 Microsoft Word 10 Object Library statement :*/
  86. DocType. InvokeMember ("SaveAs", System. Reflection. BindingFlags. InvokeMethod,
  87. Null, doc, new object [] {saveFilePath, Word. WdSaveFormat. wdFormatFilteredHTML });
  88. /// Exit 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. // Close the 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)

Asp net web page, written in C # language. How can I preview successfully uploaded files such as word, excel, ppt, and txt online?

If there are no special requirements, call the saveas function of the office itself on the server and save it as html. The specific code Baidu office online preview, the first one is

Aspnet is similar to Baidu Library Online Preview word implementation

What is the purpose of Online Preview? If it is to prevent users from downloading, it is necessary to make it the same as Baidu Library. If you look at the web page, you can simply open it on the web page.
The Source Code cannot be provided to you, involving unit information. It can only tell you how to implement it: Use the virtual printing technology to print word into an image, convert it to swf, or convert word to pdf, and then convert the pdf to swf with pdfswf.exe. After converting to swf, use flexpager to browse. There are many materials on the Internet. You need to pay attention to this problem. The Document Style conversion is prone to major problems. We recommend that you use word to print it as an image, the disadvantage is that the file size is large but simple.

Related Article

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.