Therefore, an HTMl file is saved when each file is uploaded, so that the online preview function can be realized. The main code is as follows:
Copy codeThe Code is 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 the page
Word. ApplicationClass word = new Word. ApplicationClass ();
Type wordType = word. GetType ();
Word. Documents docs = word. Documents;
// Open the 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 the format and save it
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 );
// Exit Word
WordType. InvokeMember ("Quit", System. Reflection. BindingFlags. InvokeMethod, null, word, null );
Return saveFileName. ToString ();
}
}