asp.net how to convert Word files to HTML _ practical tips

Source: Internet
Author: User

This article describes the ability to convert Word to HTML by a ASP.net implementation, with key code as follows:

The full path to the Word file, string wordpath = Server.MapPath ("/word/test.doc");
 Full path to store HTML file string htmlpath = Server.MapPath ("/html/test.html");
 Upload Word file, because just do the example, here do not do more file type, size, format and the existence of the Judgment Fileupload1.saveas (Wordpath);
#region file format conversion//Please refer to Microsoft.Office.Interop.Word applicationclass Word = new ApplicationClass (); Type Wordtype = Word.
GetType (); Documents docs = Word.

 Documents; Open file Type docstype = Docs.
 GetType (); 
 Object fileName = Wordpath;
 "F:\\cc.doc"; Document doc = (document) Docstype.invokemember ("Open", BindingFlags.InvokeMethod, NULL, (object) Docs, new object[] {

 FileName, True, true}); Determine if the file that is associated with the file conversion exists, or if it exists, delete it.
 (Here, it's best to judge if the directory that holds the file exists, if it doesn't exist, create it) if (file.exists (Htmlpath)) {file.delete (Htmlpath);} Each HTML file has a corresponding folder that holds HTML-related elements (HTML file name. files) if (Directory.Exists (Htmlpath.replace (". html", ". Files")) {Direct Ory.
 Delete (Htmlpath.replace (". html", ". Files"), true);

 }; Transform the format to invoke Word Save As method Type DocType =doc.
 GetType (); 
 Object savefilename = Htmlpath; //"F:\\aaa.html "; Doctype.invokemember ("SaveAs", BindingFlags.InvokeMethod, NULL, doc, new object[] {savefilename,
 wdsaveformat.wdformathtml});
 Quit Word wordtype.invokemember ("Quit", BindingFlags.InvokeMethod, NULL, Word, NULL);

 #endregion

The above code, in. NET framework4.0, may produce a compilation error, as follows:
Unable to embed interop type "...", use the applicable interface instead .

After consulting the data, find the solution as follows:
Select the DLL that introduces word in the project, right-click, select Properties, and set "embed Interop Type" to false.

This instance complete code clicks here the website to download.

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.