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

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

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

补充说明:

代码中使用了类库dll文件,及操作的DM类,这里提供下载dll的地址:http://d.download.csdn.net/down/3499114/taomanman

和DM.cs类得代码,放在App_Code目录下即可。

using System;
using System.Configuration;
using System.Data;

namespace USTC
{
    /// <summary>
    /// 连接sql数据库。
    /// </summary>
    public class DM
    {
        public USTC.SqlDbc db;
        public DM()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            db = new USTC.SqlDbc(ConfigurationSettings.AppSettings["ConnectionString"]);
            //db.open(ConfigurationSettings.AppSettings["ConnectionString"]);

        }

        public USTC.SqlDbc getDataBase()
        {

            return db;
        }

        public DataSet getTable(string tablename)
        {
            db.open();
            string tmpstr = "select * from " + tablename;
            DataSet result = (DataSet)db.getData(tmpstr, false);
            db.close();
            return result;
        }

        public void deleteTable(string tablename)
        {
            db.open();
            string tmpstr = "drop table " + tablename;
            db.getData(tmpstr, false);
            db.close();

        }
        public int execsql1(string sql)
        {
            db.open();
            int result = (int)db.getData(sql, false);
            db.close();
            return result;
        }

        public void execsql(string sql)
        {
            db.open();

            db.getData(sql, false);
            db.close();
        }

        public DataSet getsql(string sql)
        {
            db.open();

            DataSet result = (DataSet)db.getData(sql, false);
            db.close();
            return result;
        }
        public static string Database2String(object data, int bz, string format)
        {
            string data1 = data.ToString();
            switch (bz)
            {
                case 1: //数字
                    double d = 0;
                    data1 = (double.TryParse(data1, out d) ? d.ToString(format) : "");
                    break;
                case 2://日期
                    DateTime dt = DateTime.Now;
                    data1 = (DateTime.TryParse(data1, out dt) ? dt.ToString(format) : "");
                    break;
                default://字符串
                    break;
            }
            return data1;
        }
        public static string Database2String(object data, int bz)
        {
            switch (bz)
            {
                case 1: //数字
                    return Database2String(data, bz, "");
                case 2://日期
                    return Database2String(data, bz, "yyyy-MM-dd");
                default://字符串
                    return Database2String(data, bz, "");
            }
        }
        public static DateTime Database2String(string date)
        {
            DateTime dt = Convert.ToDateTime(date);
            string time = dt.ToString("yyyy-MM-dd");
            DateTime dt1 = Convert.ToDateTime(time);
            return dt1;
        }
    }
}




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

 

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> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="查看word文档" /> </div> </form> </body> </html>

 

using System; using System.Collections.Generic; using System.Linq; using System.Web; 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> <body> <form id="form1" runat="server"> </form> </body> </html>

 

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Office.Core; 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) { try { //根据传递过来的乡镇名称获取到文件名称 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()+"'); // --></mce:script>"); //保存HTML 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); //跳转显示预案信息 Response.Redirect("~/Upload/" + documentName+".html"); } }

 

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

 

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

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

 

 

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


===========================================================================

如果觉得对您有帮助,微信扫一扫支持一下:



 

相关文章
|
JavaScript 前端开发
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
1718 0
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
32 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
JavaScript
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
|
9月前
|
开发框架 数据可视化 前端开发
ASP.NET Core MVC+Quartz实现定时任务可视化管理页面
ASP.NET Core MVC+Quartz实现定时任务可视化管理页面
327 0
|
10月前
|
开发框架 JavaScript .NET
Asp.net C#页面传参的几种方式
Asp.net C#页面传参的几种方式
107 0
|
11月前
mvc.net分页查询案例——前台页面(Index.aspx)
mvc.net分页查询案例——前台页面(Index.aspx)
47 0
|
开发框架 JSON 前端开发
【C#】.net core2.1,自定义全局类对API接口和视图页面产生的异常统一处理
在开发一个网站项目时,异常处理和过滤功能是最基础的模块 本篇文章就来讲讲,如何自定义全局异常类来统一处理
204 0
|
开发框架 程序员 API
【C#】.net core2.1,通过扩展状态代码页方法对404页面进行全局捕抓并响应信息
在开发一个网站项目时,除了异常过滤功能模块,还需要有针对404不存在的api接口和页面处理功能 本篇文章就来讲讲,如何自定义全局请求状态类来统一处理
184 0
|
开发框架 .NET Windows
真正解决ASP.NET每一个页面首次访问超级慢的问题
真正解决ASP.NET每一个页面首次访问超级慢的问题
217 0