Asp.net allows you to browse word documents online (word to html ),

Source: Internet
Author: User

Asp.net allows you to browse word documents online (word to html ),

Recently, I am engaged in online browsing of word documents. After finding various methods and controls, I will go back to word to HTML for online browsing ....

The following is the background code, which is the default code for the front-end html page.


Because the file is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using Word = Microsoft.Office.Interop.Word;



using Word = Microsoft.Office.Interop.Word;
If the component is not referenced, an error is reported here, such as Interop cannot be found,

In this case, you need to reference Microsoft. Office. Interop. Visio, Microsoft. Office. Interop. Word under the. net Component in the reference.


The background code is as follows:


Protected void Page_Load (object sender, EventArgs e) {string relativePath = Request. QueryString ["FilePath"]; // relative path, which is obtained from the jump page. If (relativePath = "" | relativePath = null) return; string serverPath = Server. mapPath (relativePath); // corresponding path to the server to convert string html = serverPath. replace (". doc ",". html "); if (! File. exists (@ html) // The html page does not exist. convert word to html {string filename = WordToHtml (serverPath); StreamReader fread = new StreamReader (filename, System. text. encoding. getEncoding ("gb2312"); string ss = fread. readToEnd (); Response. write (ss); // directly Write a string to the webpage and you will find that the text is displayed, and images and tables cannot be displayed. Therefore, you can redirect to the html file page later. Fread. close (); fread. dispose ();} html = relativePath. replace (". doc ",". html "); // The html file is also stored in the same path. // you only need to change the suffix of the original path to get the html file path Response. redirect (html); return ;} /// <summary> /// convert word to html /// </summary> /// <param name = "wordFileName"> </param> private string WordToHtml (object wordFileName) {// put the user code here to initialize the page Word. application word = new Word. application (); 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 [] {wordFileName, true, true}); // convert the format and save it as Type docType = doc. getType (); string wordSaveFileName = wordFileName. toString (); string strSaveFileName = wordSaveFileName. substring (0, wordSaveFileName. length-3) + "html"; object saveFileName = (object) strSaveFileName; docType. invokeMember ("SaveAs", System. reflection. bindingFlags. invokeMethod, null, doc, new object [] {saveFileName, Word. wdSaveFormat. wdFormatFilteredHTML}); docType. invokeMember ("Close", System. reflection. bindingFlags. invokeMethod, null, doc, null); // exit Word wordType. invokeMember ("Quit", System. reflection. bindingFlags. invokeMethod, null, word, null); return saveFileName. toString ();}


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.