Asp. NET to realize Word document online Preview function Code _ Basic Application

Source: Internet
Author: User
Tags reflection
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 ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.