[转]asp.net页面显示word文档内容

在实际开发过程中,经常会遇到在页面上直接显示word文档的内容,当然这里仅仅涉及到查看文档内容,不涉及修改和保存操作,这里是利用Office的COM组件,将word文档转换程html格式后显示在页面中,html页面中显示的风格几乎跟word内容一致。

这里介绍一种可行的方案:

1、首先在项目引用中添加如下引用:

2、假如在项目根目录下有一个专门的文件夹,譬如叫UpLoad的文件夹,专门用来存放上传上来的Word文档,这里是在数据库中保存有文件名的文件存放目录。

   

3、新建一个Default.aspx页面,用于模拟参数传递

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FHGC_CZFH_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="查看word文档" />  
  11.     </div>  
  12.     </form>  
  13. </body>  
  14. </html>  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class FHGC_CZFH_Default : System.Web.UI.Page  
  8. {  
  9.     protected void Page_Load(object sender, EventArgs e)  
  10.     {  
  11.     }  
  12.     protected void Button1_Click(object sender, EventArgs e)  
  13.     {  
  14.         Response.Redirect("CountyTown.aspx?space=" + Server.UrlEncode("江潭乡"));  
  15.     }  
  16. }  

4、在CountyTown.aspx页面中接受参数并做word转html处理并显示html页面

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.     <title>城镇防洪预案</title>  
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server">  
  8.     </form>  
  9. </body>  
  10. </html>  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Microsoft.Office.Core;  
  8. using System.IO;  
  9. using USTC;  
  10. public partial class FHGC_CZFH_CountyTown : System.Web.UI.Page  
  11. {  
  12.     public string countyName = string.Empty; //乡镇名称  
  13.     public string documentFullName = string.Empty; //预案全称(包括后缀名)  
  14.     public string documentName = string.Empty; //预案名称  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             try  
  20.             {  
  21.                 //根据传递过来的乡镇名称获取到文件名称  
  22.                 countyName = Server.UrlDecode(Request.QueryString["space"].ToString().Trim());  
  23.                 DM dm = new DM();  
  24.                 string strSQL = "select 预案文件 from 山洪防治预案 where 乡镇名称='" + countyName + "'";  
  25.                 documentFullName = dm.getsql(strSQL).Tables[0].Rows[0]["预案文件"].ToString().Trim();  
  26.                 documentName = documentFullName.Substring(0, documentFullName.LastIndexOf('.'));  
  27.             }  
  28.             catch (Exception)  
  29.             {  
  30.                 documentFullName = "";  
  31.             }  
  32.         }  
  33.         // 在此处放置用户代码以初始化页面   
  34.         Word.ApplicationClass word = new Word.ApplicationClass();  
  35.         Type wordType = word.GetType();  
  36.         Word.Documents docs = word.Documents; // 打开文件   
  37.         Type docsType = docs.GetType();  
  38.         object fileName = Server.MapPath("~/Upload/") + documentFullName;  
  39.         Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, truetrue }); // 转换格式,另存为   
  40.         Type docType = doc.GetType();  
  41.         object saveFileName = Server.MapPath("~/Upload/") + documentName+".html";  
  42.         ClientScript.RegisterClientScriptBlock(GetType(),"","<mce:script type="text/javascript"><!--  
  43. alert('"+saveFileName.ToString()+"');  
  44. // --></mce:script>");  
  45.           
  46.         //保存HTML  
  47.         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatHTML });  
  48.         // 退出 Word   
  49.         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);  
  50.         //跳转显示预案信息  
  51.        Response.Redirect("~/Upload/" + documentName+".html");  
  52.     }  
  53. }  

5、经过如上处理以后,在Word所在位置会生成一个文件夹和一个同名的html文件,我们要显示的就是这个html的内容,如下图

6、大功告成,看一下效果图:

点击按钮以后,可以查看Word文档转换程html后的内容了,如下图

基本上可以满足一般的查看需求了,简陋之篇,欢迎拍砖,共同探讨,共同进步。

原文链接:http://blog.csdn.net/taomanman/article/details/6233930


posted @ 2011-07-26 17:27  jayccc  阅读(2850)  评论(0编辑  收藏  举报