1 usingMicrosoft.Office.Interop.Word;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.IO;5 usingSystem.Linq;6 usingSystem.Text;7 usingsystem.web;8 usingSystem.Web.UI;9 usingSystem.Web.UI.WebControls;Ten One namespaceAdmin A { - Public Partial classTestDemo:System.Web.UI.Page - { the protected voidPage_Load (Objectsender, EventArgs e) - { - stringWordfilename ="~/uploads/tutorcv/111.docx"; - stringHtmlfilename ="~/uploads/tutorcv/"; + //getpathbydoctohtml (Server.MapPath (Wordfilename)); - } + Private stringGetpathbydoctohtml (stringstrfile) A { at if(string. IsNullOrEmpty (strfile)) - { - return "0";//No files - } - - //Microsoft.Office.Interop.Word.ApplicationClass Word = new Microsoft.Office.Interop.Word.ApplicationClass (); inMicrosoft.office.interop.word._application Word =Newapplication (); -Type Wordtype =Word. GetType (); toMicrosoft.Office.Interop.Word.Documents docs =Word. Documents; + - //Open File theType Docstype =Docs. GetType (); * $ ObjectFileName =strfile;Panax Notoginseng -Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document) docstype.invokemember ("Open", theSystem.Reflection.BindingFlags.InvokeMethod,NULL, Docs,NewObject[] {fileName,true,true }); + A //convert format, save as HTML theType DocType =Doc. GetType (); + //to re -name a file - stringfilename = 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 (); $ - stringStrfilefolder ="~/uploads/tutorcv/"; -DateTime dt =DateTime.Now; the //Create a subfolder name in YYYYMMDD form - stringStrfilesubfolder =dt. Year.tostring ();WuyiStrfilesubfolder + = (dt. Month <Ten) ? ("0"+dt. Month.tostring ()): dt. Month.tostring (); theStrfilesubfolder + = (dt. Day <Ten) ? ("0"+dt. Day.tostring ()): dt. Day.tostring (); - stringstrFilePath = Strfilefolder + Strfilesubfolder +"/"; Wu //determines whether a folder exists under the specified directory, and if it does not exist, creates - if(!directory.exists (Server.MapPath (strFilePath))) About { $ //Create up folder - directory.createdirectory (Server.MapPath (strFilePath)); - } - A //The location where the converted HTML document is saved + //HttpContext.Current.Server.MapPath ("html" + strfilesubfolder + filename + ". html") the stringConfigpath = Server.MapPath (strFilePath + filename +". html"); - ObjectSavefilename =Configpath; $ the /*The following is the wording of the Microsoft Word 9 Object Library, which, if it is 10, may be written as: the * Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, the * NULL, doc, new Object[]{savefilename, Word.WdSaveFormat.wdFormatFilteredHTML}); the * Other formats: - * wdformathtml in * wdFormatDocument the * wdFormatDOSText the * wdFormatDOSTextLineBreaks About * Wdformatencodedtext the * wdFormatRTF the * wdFormatTemplate the * Wdformattext + * Wdformattextlinebreaks - * Wdformatunicodetext the */BayiDoctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, the NULL, Doc,New Object[] {savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML}); the - //doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, - //NULL, doc, new object[] {savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML}); the the //Close Document theDoctype.invokemember ("Close", System.Reflection.BindingFlags.InvokeMethod, the NULL, Doc,New Object[] {NULL,NULL,NULL }); - the //quit Word theWordtype.invokemember ("Quit", System.Reflection.BindingFlags.InvokeMethod,NULLWordNULL); the //go to the newly generated page94 //return ("/" + filename + ". html"); the the //Convert HTML page uniform encoding format the transhtmlencoding (configpath);98 About return(strFilePath + filename +". html"); - }101 Private voidTranshtmlencoding (stringstrFilePath)102 {103 Try104 { theSystem.IO.StreamReader sr =NewSystem.IO.StreamReader (strFilePath, Encoding.GetEncoding (0));106 stringHTML =Sr. ReadToEnd ();107 Sr. Close ();108HTML = System.Text.RegularExpressions.Regex.Replace (HTML,@"<meta[^>]*>","<meta http-equiv=content-type content= ' text/html; charset=gb2312 ' >", System.Text.RegularExpressions.RegexOptions.IgnoreCase);109System.IO.StreamWriter SW =NewSystem.IO.StreamWriter (strFilePath,false, Encoding.default); the 111 SW. Write (HTML); the SW. Close ();113 } the Catch(Exception ex) the { thePage.registerstartupscript ("alt","<script>alert ('"+ ex. Message +"') </script>");117 }118 }119 - }121}
Convert C#word files to HTML