C # convert a Word document to HTML

Source: Internet
Author: User

In daily life, we always edit Text in Word. It not only saves Text, but also saves the Text format. What should I do if I want to display the content on a Word document on a webpage? Here I provide a small tool that you can convert Word to Html. If you need to display it, You can directly access the Html. If you don't talk about it, let's look at the code below. Page code: [html] <span style = "font-size: 18px; "> <div> <input id =" File1 "type =" file "runat =" server "/> <asp: button ID = "btnConvert" runat = "server" Text = "" OnClick = "btnConvert_Click"/> </div> </span> C # code: [csharp] <span style = "font-size: 18px;"> using System; using System. data; using System. configuration; using System. collections; using System. collections. generic; using System. linq; using System. web; using S Ystem. 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; protected void Page_Load (object sender, EventArgs e) {}/// <summary> /// convert word to Html /// </summary> /// <param name = "sender"> </param> // /<param name = "e"> </param> protected void btnConvert_Click (object sender, eventArgs e) {try {// Upload // uploadWord (File1); // convert wordToHtml (File1);} catch (Exception ex) {throw ex;} finally {Response. write ("congratulations, switch successful! ") ;}} // Upload the file and convert it to html wordToHtml (wordFilePath) /// <summary> /// upload the file and save it as html /// </summary> /// <param name = "wordFilePath"> location of the word file on the client </ param> // <returns> address of the html file to be uploaded </returns> public string wordToHtml (System. web. UI. htmlControls. htmlInputFile wordFilePath) {Microsoft. office. interop. word. applicationClass word = new Microsoft. office. interop. word. applicationClass (); Type wordType = word. getType (); M Icrosoft. office. interop. word. required ents docs = word. documents; // open the file Type docsType = docs. getType (); // upload the file to the server and parse it as html string filePath = uploadWord (wordFilePath ); // determine whether the file is successfully uploaded if (filePath = "0") return "0"; // determine whether it is a word file if (filePath = "1 ") return "1"; object fileName = filePath; Microsoft. office. interop. word. document doc = (Microsoft. office. interop. word. document) docsType. invokeMember ("Open", System. reflection. bindingFlags. invokeMethod, null, docs, new Object [] {fileName, true, true}); // convert the format and save it as html Type docType = doc. getType (); string filename = System. dateTime. now. year. toString () + System. dateTime. now. month. toString () + System. dateTime. now. day. toString () + System. dateTime. now. hour. toString () + System. dateTime. now. minute. toString () + System. dateTime. now. second. toString (); // Determine whether a folder exists in the specified directory. if not, create if (! Directory. Exists (Server. MapPath ("~ \ Html ") {// create up folder Directory. CreateDirectory (Server. MapPath ("~ \ Html ");} // location where the converted html document is saved string ConfigPath = HttpContext. current. server. mapPath ("html/" + filename + ". html "); object saveFileName = ConfigPath;/* The following is the Microsoft Word 9 Object Library statement. If it is 10, it may be written as * docType. invokeMember ("SaveAs", System. reflection. bindingFlags. invokeMethod, * null, doc, new object [] {saveFileName, Word. wdSaveFormat. wdFormatFilteredHTML}); * Other format: * wdFormatHTML * wdFormatDocum Ent * wdFormatDOSText * wdFormatDOSTextLineBreaks * wdFormatEncodedText * wdFormatRTF * wdFormatTemplate * wdFormatText * wdFormatTextLineBreaks * wdFormatUnicodeText */docType. invokeMember ("SaveAs", System. reflection. bindingFlags. invokeMethod, null, doc, new object [] {saveFileName, Microsoft. office. interop. word. wdSaveFormat. wdFormatFilteredHTML}); // close the docdoctype. invokeMember ("Close", System. re Flection. bindingFlags. invokeMethod, null, doc, new object [] {null, null, null}); // exit Word wordType. invokeMember ("Quit", System. reflection. bindingFlags. invokeMethod, null, word, null); // go to the new page return ("/" + filename + ". html ");} public string uploadWord (System. web. UI. htmlControls. htmlInputFile uploadFiles) {if (uploadFiles. postedFile! = Null) {string fileName = uploadFiles. postedFile. fileName; int extendNameIndex = fileName. lastIndexOf (". "); string extendName = fileName. substring (extendNameIndex); string newName = ""; try {// verify whether it is in the word format if (extendName = ". doc "| extendName = ". docx ") {DateTime now = DateTime. now; newName = now. dayOfYear. toString () + uploadFiles. postedFile. contentLength. toString (); // determines whether a folder exists in the specified directory. If not Create if (! Directory. Exists (Server. MapPath ("~ \ WordTmp ") {// create up folder Directory. CreateDirectory (Server. MapPath ("~ \ WordTmp ");} // the upload path refers to the wordTmp path uploadFiles under the directory at the same level on the current upload page. postedFile. saveAs (System. web. httpContext. current. server. mapPath ("wordTmp/" + newName + extendName);} else {return "1" ;}} catch {return "0" ;}www.2cto.com // return "http: // "+ HttpContext. current. request. url. host + HttpContext. current. request. applicationPath + "/wordTmp/" + newName + extendName; return System. web. httpContext. curren T. server. mapPath ("wordTmp/" + newName + extendName);} else {return "0" ;}}</span>: in this way, the converted Html file can simply display the content in the Word document in Html without editing it on its own. Of course, if necessary, you can save the converted Html path to the database and directly access it based on different conditions.

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.