将Word文档转化为HTML格式的文档
2024-08-26 00:15:32
供稿:网友
利用word.application提供的方法,可以很轻易地将word文档转化为html等其它格式,下面就是实现的全部的代码:
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>
/// wordtohtml 的摘要说明。
/// 首先要添加引用:microsoft word 9.0 object library
/// </summary>
public class wordtohtml : system.web.ui.page
{
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
word.applicationclass word = new word.applicationclass();
type wordtype = word.gettype();
word.documents docs = word.documents;
// 打开文件
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});
// 转换格式,另存为
type doctype = doc.gettype();
object savefilename = "d://tmp//aaa.html";
//下面是microsoft word 9 object library的写法,如果是10,可能写成:
//doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
null, doc, new object[]{savefilename, word.wdsaveformat.wdformatfilteredhtml});
///其它格式:
///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});
// 退出 word
wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod,
null, word, null);
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}