This article describes a function that implements the conversion of Word to HTML, and its key code is as follows:
//The complete path to store word files string wordPath = ("/word/"); //The complete path to store html file string htmlPath = ("/html/"); //Upload word files, since it is just an example, I will not make more judgments on the file type, size, format and whether it exists here. (wordPath); #region File format conversion //Please quote ApplicationClass word = new ApplicationClass(); Type wordType = (); Documents docs = ; //Open the file Type docsType = (); object fileName = wordPath; //"f:\\"; Document doc =(Document)("Open", , null, (object)docs, new Object[] { fileName, true, true}); //Judge whether the file related to file conversion exists, and delete it if it exists. (Here, it is best to determine whether the directory where the file is stored exists. If it does not exist, it will be created) if((htmlPath)) { (htmlPath); } //Each html file has a corresponding folder (html file name.files) that stores html-related elements (html file name.files) if(((".html" ,".files"))) { ((".html", ".files"), true); }; //Convert the format and call the "Save As" method of word Type docType =(); object saveFileName = htmlPath; //"f:\\"; ("SaveAs", , null, doc, new object[] { saveFileName, }); //Exit Word ("Quit", , null, word, null); #endregion
In the above code, in .net framework4.0, a compilation error may occur, as shown below:
The interoperability type "..." cannot be embedded, please use the applicable interface instead.。
After consulting the information, the solution was found as follows:
Select the dll that introduces word in the project, right-click, select properties, and set "Embed Interoperability Type" to False.
Click here for the full code of this exampleDownload this site。