First quote Microsoft.Office.Interop.Word.dll
Conversion method:
Using System;
Using System.Text;
Using MSWord = Microsoft.Office.Interop.Word;
Using System.IO;
Using System.Reflection;
Namespace Readword
{
public class Gethtmlstring
{
<summary>
Word goes to HTML string--will.wang
</summary>
<param name= "Wordpath" >word file absolute path </param>
<returns>html string </returns>
public static string Getprocehtmlstring (String wordpath)
{
String htmlpath = gethtml (Wordpath);
String htmlstring = Procehtmlstring (Htmlpath);
return htmlstring;
}
<summary>
Word goes to HTML and returns the HTML file address
</summary>
<returns></returns>
private static string gethtml (Object path)
{
Msword.application WordApp;
Msword.document WordDoc;
Object nothing = Missing.Value;
WordApp = new Msword.application ();
WordDoc = WORDAPP.DOCUMENTS.ADD (ref path, ref-nothing, ref-nothing, ref-nothing);
Object format = MSWord.WdSaveFormat.wdFormatFilteredHTML;
Object NewPath = path. ToString (). Substring (0, path. ToString (). LastIndexOf ('. ')) + ". html";//html file path
Worddoc.saveas (ref NewPath, ref format, ref nothing, ref nothing, ref nothing,
Ref nothing, ref no, ref nothing, ref nothing, ref nothing, ref nothing,
Ref nothing, ref no, ref nothing, ref nothing, ref nothing);
Worddoc.close (ref nothing, ref no, ref nothing);
Wordapp.quit (ref nothing, ref no, ref nothing);
return newpath.tostring ();
}
<summary>
Reading HTML strings
</summary>
<param name= "Htmlpath" ></param>
<returns></returns>
private static string procehtmlstring (String htmlpath)
{
FileStream fs = new FileStream (Htmlpath, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader (FS, Encoding.default);
String htmlstring = Sr. ReadToEnd ();
Sr. Close ();
Fs. Close ();
return htmlstring;
}
}
}
Word file to HTML string (inclusion format and picture)