在实际开发过程中,经常会遇到在页面上直接显示word文档的内容,当然这里仅仅涉及到查看文档内容,不涉及修改和保存操作,这里是利用Office的COM组件,将word文档转换程html格式后显示在页面中,html页面中显示的风格几乎跟word内容一致。
补充说明:
代码中使用了类库dll文件,及操作的DM类,这里提供下载dll的地址:http://d.download.csdn.net/down/3499114/taomanman
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后的内容了,如下图
基本上可以满足一般的查看需求了,简陋之篇,欢迎拍砖,共同探讨,共同进步。
===========================================================================
如果觉得对您有帮助,微信扫一扫支持一下: