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.