ASP. NET code for online Word document Preview

Source: Internet
Author: User

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 ();
}
}

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.