Asp.net: HTML preview docx document content

Source: Internet
Author: User

Since Gmail launched the Office document preview function, various mailboxes have followed up, but they cannot find mature documents, which is incredible and trade confidential?

A small requirement is to preview the doc document on the webpage,

One solution is to use code to operate the word "Save as" function and save it as HTML when uploading a DOC file,

For more information, see the Google "Asp.net preview Doc" keyword.

This method is limited
1. Each document generates an HTML copy and resource folder, causing serious space waste.

2. The server must install the office. Of course, you can also try to introduce the necessary DLL and copy the DCOM configuration according to the error message. Every time I go crazy, the last step is to install the office...

3. docx is not tested currently.

The general implementation is as follows. We sincerely remind you that it is best to install the office and try again.

After the related DLL (such as Word or Excel) is introduced, refer to the following code:

Using system; using system. data; using system. configuration; using system. collections; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. io; using system. diagnostics; using word = Microsoft. office. interOP. word; using Excel = Microsoft. office. interOP. excel; using system. reflection; using Microsoft. office. interOP. excel; public partial class upload_preview: system. web. UI. page {protected void page_load (Object sender, eventargs e) {generationwordhtml ("E: \ 20110502.doc"," E: \ 20110502.html "); generationexcelhtml (" E: \ 20110502.xls ", "E: \ 20110502.html ");} /// <summary> /// generate HTML for the ecxel file and save it /// </Summary> /// <Param name = "filepath"> path of the ecxel file to be generated </param> /// <Param name = "SaveFilePath"> whether the path of the HTML file to be saved after generation </param> /// <returns> is generated successfully, success is true, otherwise false </returns> protected bool generationexcelhtml (string filepath, string SaveFilePath) {try {excel. application APP = new excel. application (); app. visible = false; object o = missing. value; // open the file/* The following is the Microsoft Excel 9 Object Library statement: * // * _ workbook XLS = app. workbooks. open (filepath, O, O ); * // * The following is the Microsoft Excel 10 Object Library: */_ workbook XLS = app. workbooks. open (filepath, O, O); // The conversion format, save as HTML/* is written in Microsoft Excel 9 Object Library: * // * XLS. saveas (SaveFilePath, Excel. xlfileformat. xlhtml, O, xlsaveasaccessmode. xlexclusive, O, O); * // * The following is the Microsoft Excel 10 Object Library statement: */XLS. saveas (SaveFilePath, Excel. xlfileformat. xlhtml, O, xlsaveasaccessmode. xlexclusive, O, O); // exit the Excel app. quit (); Return true;} catch {return false;} finally {// finally close the opened Excel Process [] myprocesses = process. getprocessesbyname ("Excel"); foreach (process myprocess in myprocesses) {myprocess. kill ();}}} /// <summary> /// generate HTML and save the winword file /// </Summary> /// <Param name = "filepath"> path of the Word file to be generated </param> /// <Param name = "SaveFilePath"> whether the path of the HTML file to be saved after generation </param> /// <returns> is generated successfully, success is true, and vice versa is false </returns> private bool generationwordhtml (string filepath, string SaveFilePath) {try {word. applicationclass word = new word. applicationclass (); Type wordtype = word. getType (); word. required ents docs = word. documents; // open the file type docstype = docs. getType (); word. document Doc = (word. document) docstype. invokemember ("open", system. reflection. bindingflags. invokemethod, null, Docs, new object [] {filepath, true, true}); // convert the format and save it as HTML type doctype = Doc. getType ();/* The following is the Microsoft Word 9 Object Library: * // * doctype. invokemember ("saveas", system. reflection. bindingflags. invokemethod, null, Doc, new object [] {SaveFilePath, word. wdsaveformat. wdformathtml}); * // * The following is the Microsoft Word 10 Object Library: */doctype. invokemember ("saveas", system. reflection. bindingflags. invokemethod, null, Doc, new object [] {SaveFilePath, word. wdsaveformat. wdformatfilteredhtml}); // exit word wordtype. invokemember ("quit", system. reflection. bindingflags. invokemethod, null, word, null); Return true;} catch {return false;} finally {// finally close the opened winword Process [] myprocesses = process. getprocessesbyname ("winword"); foreach (process myprocess in myprocesses) {myprocess. kill ();}}}}

==================

Now the Chinese resources are unavailable, so I continued to look for it and found a foreigner's implementation, but only word2007 is supported. Because office2007 is based on openxml, of course it can be parsed by myself.

See this address

Http://blog.maartenballiauw.be/post/2008/01/11/Preview-Word-files-(docx)-in-HTML-using-ASPNET-OpenXML-and-LINQ-to-XML.aspx

In this case, the URL at the end of .docx can be directly previewed and the download link generation rules are provided.

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.