Open the word file in office:
Public void OpenFile ()
{
Microsoft. Office. Interop. Word. Application wapp = new Microsoft. Office. Interop. Word. Application (); // open
Wapp. Visible = true;
Object filename = "F :\\ wordtest .doc ";
Object isread = false;
Object isvisible = true;
Object miss = System. Reflection. Missing. Value;
Wapp. Documents. Open (ref filename, ref miss, ref isread, ref miss,
Ref miss, ref isvisible, ref miss, ref miss );
Wapp = null;
}
Alternatively, you can save the file as an html file and open the page to directly preview the file online. The main 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 Htmltest: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void btn_Click (object sender, EventArgs e)
{
WordToHtml ("F: \ wordtest .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 ();
}
}