利用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="
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
}
}
推荐阅读
- 不打开Word一样能“统计字数”让您百战百胜
- 保护Word安全性教你五招2020开心玩
- 如何将手机照片一次性传到电脑 手机照片怎么一次性传到电脑
- 扮酷你的Word右键菜单一览大全
- 批量转换Word文档图文攻略666
- 浅谈Word中“分离下划线”推荐阅读
- 再谈Word“空行替换”[图文攻略]
- Word自带的“转换向导”批量转换Word文档新发布的哦
- Word中鼠标左击两次的妙用热门推荐
- Word2003新鲜体验:网上信息随手得新发布的哦