Then consider the existence of an HTML file for each file when it is uploaded, so that the online preview function can be achieved. The main code is as follows
Copy Code code as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using Word = Microsoft.Office.Interop.Word;
public partial class Test:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Wordtohtml ("D:\\yijian.doc");
}
<summary>
Convert Word to HTML
</summary>
<param name= "Wordfilename" ></param>
private string Wordtohtml (object Wordfilename)
{
Place user code here to initialize page
Word.applicationclass Word = new Word.applicationclass ();
Type Wordtype = Word. GetType ();
word.documents docs = Word. Documents;
Open File
Type Docstype = Docs. GetType ();
Word.Document doc = (word.document) docstype.invokemember ("Open", System.Reflection.BindingFlags.InvokeMethod, NULL, Docs, new object[] {wordfilename, True, true});
Convert format, Save As
Type DocType = doc. GetType ();
String wordsavefilename = Wordfilename.tostring ();
String strsavefilename = wordsavefilename.substring (0, wordsavefilename.length-3) + "html";
Object savefilename = (object) strsavefilename;
Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, NULL, doc, new object[] {savefilename, Word.WdSaveFormat.wdFormatFilteredHTML});
Doctype.invokemember ("Close", System.Reflection.BindingFlags.InvokeMethod, NULL, doc, NULL);
Quit Word
Wordtype.invokemember ("Quit", System.Reflection.BindingFlags.InvokeMethod, NULL, Word, NULL);
return savefilename.tostring ();
}
}