將Word文檔轉化為HTML格式的文檔讀懂您就是高手

利用Word.Application提供的方法,可以很輕易地將Word文檔轉化為HTML等其它格式,下面就是實現的全部的代碼:
VisualC#
WordToHtml.aspx
【將Word文檔轉化為HTML格式的文檔讀懂您就是高手】 <%@Pagelanguage="c#"Codebehind="WordToHtml.aspx.cs"AutoEventWireup="false"
Inherits="aspxWebcs.WordToHtml"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WordToHtml</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="
將Word文檔轉化為HTML格式的文檔讀懂您就是高手http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
</form>
</body>
</HTML>
WordToHtml.aspx.cs
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingOffice;
namespaceaspxWebcs
{
///<summary>
///WordToHtml的摘要說明 。
///首先要添加引用:MicrosoftWord9.0ObjectLibrary
///</summary>
publicclassWordToHtml:System.Web.UI.Page
{
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此處放置用戶代碼以初始化頁面
Word.ApplicationClassword=newWord.ApplicationClass();
TypewordType=word.GetType();
Word.Documentsdocs=word.Documents;
//打開文件
TypedocsType=docs.GetType();
objectfileName="d:\\tmp\\aaa.doc";
Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod,null,docs,newObject[]{fileName,true,true});

//轉換格式,另存為
TypedocType=doc.GetType();
objectsaveFileName="d:\\tmp\\aaa.html";
//下面是MicrosoftWord9ObjectLibrary的寫法,如果是10,可能寫成:
//docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,
null,doc,newobject[]{saveFileName,Word.WdSaveFormat.wdFormatFilteredHTML});
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,
null,doc,newobject[]{saveFileName,Word.WdSaveFormat.wdFormatHTML});
//退出Word
wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,
null,word,null);
}
#regionWeb窗體設計器生成的代碼
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:該調用是ASP.NETWeb窗體設計器所必需的 。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///設計器支持所需的方法-不要使用代碼編輯器修改
///此方法的內容 。
///</summary>
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}

    推薦閱讀