在实际开发过程中,经常会遇到在页面上直接显示word文档的内容,当然这里仅仅涉及到查看文档内容,不涉及修改和保存操作,这里是利用Office的COM组件,将word文档转换程html格式后显示在页面中,html页面中显示的风格几乎跟word内容一致。
这里介绍一种可行的方案:
1、首先在项目引用中添加如下引用:
2、假如在项目根目录下有一个专门的文件夹,譬如叫UpLoad的文件夹,专门用来存放上传上来的Word文档,这里是在数据库中保存有文件名的文件存放目录。
3、新建一个Default.aspx页面,用于模拟参数传递
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"FHGC_CZFH_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
</head>
<form id=
"form1"
runat=
"server"
>
<asp:Button ID=
"Button1"
runat=
"server"
OnClick=
"Button1_Click"
Text=
"查看word文档"
/>
</form>
</body>
</html>
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
FHGC_CZFH_Default : System.Web.UI.Page
protected
void
Page_Load(
object
sender, EventArgs e)
protected
void
Button1_Click(
object
sender, EventArgs e)
Response.Redirect(
"CountyTown.aspx?space="
+ Server.UrlEncode(
"江潭乡"
));
4、在CountyTown.aspx页面中接受参数并做word转html处理并显示html页面
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title>城镇防洪预案</title>
</head>
<form id=
"form1"
runat=
"server"
>
</form>
</body>
</html>
using
System.IO;
using
USTC;
public
partial
class
FHGC_CZFH_CountyTown : System.Web.UI.Page
public
string
countyName =
string
.Empty;
public
string
documentFullName =
string
.Empty;
public
string
documentName =
string
.Empty;
protected
void
Page_Load(
object
sender, EventArgs e)
if
(!IsPostBack)
countyName = Server.UrlDecode(Request.QueryString[
"space"
].ToString().Trim());
DM dm =
new
DM();
string
strSQL =
"select 预案文件 from 山洪防治预案 where 乡镇名称='"
+ countyName +
"'"
;
documentFullName = dm.getsql(strSQL).Tables[0].Rows[0][
"预案文件"
].ToString().Trim();
documentName = documentFullName.Substring(0, documentFullName.LastIndexOf(
'.'
));
catch
(Exception)
documentFullName =
""
;
Word.ApplicationClass word =
new
Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
Type docsType = docs.GetType();
object
fileName = Server.MapPath(
"~/Upload/"
) + documentFullName;
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 = Server.MapPath(
"~/Upload/"
) + documentName+
".html"
;
ClientScript.RegisterClientScriptBlock(GetType(),
""
,
"<mce:script type="
text/javascript"><!--
alert(
'"+saveFileName.ToString()+"'
);
docType.InvokeMember(
"SaveAs"
, System.Reflection.BindingFlags.InvokeMethod,
null
, doc,
new
object
[] { saveFileName, Word.WdSaveFormat.wdFormatHTML });
wordType.InvokeMember(
"Quit"
, System.Reflection.BindingFlags.InvokeMethod,
null
, word,
null
);
Response.Redirect(
"~/Upload/"
+ documentName+
".html"
);
5、经过如上处理以后,在Word所在位置会生成一个文件夹和一个同名的html文件,我们要显示的就是这个html的内容,如下图
6、大功告成,看一下效果图:
点击按钮以后,可以查看Word文档转换程html后的内容了,如下图
基本上可以满足一般的查看需求了,简陋之篇,欢迎拍砖,共同探讨,共同进步。
原文
链接:
http://blog.csdn.net/taomanman/article/details/6233930