Convert a Word document into an HTML-formatted document

Source: Internet
Author: User
Tags reflection visual studio
Word uses the methods provided by Word.Application to easily translate Word documents into other formats, such as HTML, and the following are all the code that is implemented:

Visual C #

Wordtohtml.aspx

<%@ Page language= "C #" codebehind= "WordToHtml.aspx.cs" autoeventwireup= "false"
inherits= "Aspxwebcs.wordtohtml"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WordToHtml</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
</form>
</body>
</HTML>

WordToHtml.aspx.cs

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using Office;

Namespace Aspxwebcs
{
<summary>
Summary description of the wordtohtml.
To add a reference first: Microsoft Word 9.0 Object Library
</summary>
public class WordToHtml:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
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 ();
Object fileName = "D:\\tmp\\aaa.doc";
Word.Document doc = (word.document) docstype.invokemember ("Open",
System.Reflection.BindingFlags.InvokeMethod, NULL, Docs, new object[] {fileName, True, true});

Convert format, Save As
Type DocType = doc. GetType ();
Object savefilename = "d:\\tmp\\aaa.html";
The following is the wording of the Microsoft Word 9 Object Library, which, if 10, may be written as:
Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
NULL, doc, new Object[]{savefilename, Word.WdSaveFormat.wdFormatFilteredHTML});
Other formats:
wdFormatHTML
wdFormatDocument
wdFormatDOSText
wdFormatDOSTextLineBreaks
Wdformatencodedtext
wdFormatRTF
wdFormatTemplate
Wdformattext
Wdformattextlinebreaks
Wdformatunicodetext
Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
NULL, doc, new Object[]{savefilename, Word.WdSaveFormat.wdFormatHTML});

Quit Word
Wordtype.invokemember ("Quit", System.Reflection.BindingFlags.InvokeMethod,
NULL, Word, NULL);
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}



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.