ASP. NET implements online browsing of Word documents (Word to HTML)

Source: Internet
Author: User
Tags fread

Recently in the Word document online Browsing, find a variety of methods, controls, return to Word to HTML, online browsing ....

Is the background code, the foreground HTML page default code can be.


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;
Without referencing a good component, there will be an error here, no interop, etc.

In this case, you need to refer to the Microsoft.office.interop.visio,microsoft.office.interop.word in the reference component. Net


The background code is as follows:


  protected void Page_Load (object sender, EventArgs e) {string relativepath = request.querystring["File Path "];            Relative path, from the jump page to get the file relative path. if (RelativePath = = "" | |            Relativepath==null) return;  String serverpath = Server.MapPath (RelativePath);            Relative to the server corresponding to the path string html = Serverpath.replace (". Doc", ". html"); if (!                File.exists (@html))//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); Writing a string directly to the Web page will reveal that the text can be displayed, and the picture or table cannot be displayed.                So back to the HTML file page. Fread.                Close (); Fread.            Dispose ();                                } HTML = Relativepath.replace (". Doc", ". html");  The HTML file is also stored in the same path,//only need to change the original path suffix to get the HTML file path Response.Redirect (HTML);          Return }///<summary>//Word to HTML//</summary>//<param name= "Wordfilename             "></param> private string Wordtohtml (object wordfilename) {//Place user code here to initialize page            Word.Application word = new Word.Application (); Type Wordtype = Word.            GetType (); word.documents docs = Word.            Documents; Open 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 format, save 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);            Quit Word wordtype.invokemember ("Quit", System.Reflection.BindingFlags.InvokeMethod, NULL, Word, NULL);        return savefilename.tostring ();  }


ASP. NET implements online browsing of Word documents (Word to HTML)

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.