利用html模板生成Word文件(服務器端不須要安裝Word)css
因爲管理的緣由,不能在服務器上安裝Office相關組件,因此只能採用客戶端讀取Html模板,後臺對模板中標記的字段數據替換並返回給客戶端的方法來實現,通過測試這種方法也是一種不錯的選擇!html
首先本身寫一個html網頁模板,代碼以下:sql
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>投注站申請表</title> <style type="text/css"> table { border-collapse: collapse; } table tr td { border: 1px solid black; font-size:17px; } </style> </head> <body> <table cellpadding="1" cellspacing="1" style="margin:10px auto;"> <tr> <td colspan="4" style="font-weight:bold;text-align:center;"> 投注站申請表 </td> </tr> <tr> <td style="width:80px;"> 申請人 </td> <td style="width:220px;"> {ProposerName} </td> <td style="width:150px;"> 電話號碼 </td> <td style="width:130px;"> {PhoneNo} </td> </tr> <tr> <td style="width:80px;"> 申請地址 </td> <td style="width:220px;"> {ProposerAddress} </td> <td style="width:150px;"> 申請房屋面積 </td> <td style="width:130px;"> {HouseArea} </td> </tr> <tr> <td style="width:80px;"> 房屋類型 </td> <td style="width:220px;"> {HouseType} </td> <td style="width:150px;"> 房屋性質 </td> <td style="width:130px;"> {HouseNature} </td> </tr> <tr> <td style="width:80px;"> 申請日期 </td> <td colspan="3"> {ApplyDate} </td> </tr> </table> </body> </html>
後臺讀取該模板並替換返回給客戶端便可,代碼以下:服務器
#region 根據申請單ID號和模板生成word下載文件 public void DownLoadWord(string id) { if (string.IsNullOrEmpty(id)) { id = "0"; } string sql = "SELECT ID,ProposerName,PhoneNo,ProposerAddress,HouseArea,HouseType,HouseNature,ApplyDate" + " from BettingStationApply where ID=@id "; SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@id", int.Parse(id)) }; //根據ID號取得當前申請單的詳細信息 DataTable dt = DBHelper.GetDataSet(sql, parm); if (dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; try { //模板路徑 string tempName = Server.MapPath(@"/BettingStation/ApplyTemplete.html"); string fileContent=File.ReadAllText(tempName, Encoding.UTF8); //替換html模板中相關內容 if (dr["ProposerName"] != DBNull.Value) { fileContent=fileContent.Replace("{ProposerName}", dr["ProposerName"].ToString()); } else { fileContent = fileContent.Replace("{ProposerName}", ""); } if (dr["PhoneNo"] != DBNull.Value) { fileContent = fileContent.Replace("{PhoneNo}", dr["PhoneNo"].ToString()); } else { fileContent = fileContent.Replace("{PhoneNo}", ""); } if (dr["ProposerAddress"] != DBNull.Value) { fileContent = fileContent.Replace("{ProposerAddress}", dr["ProposerAddress"].ToString()); } else { fileContent = fileContent.Replace("{ProposerAddress}", ""); } if (dr["HouseArea"] != DBNull.Value) { fileContent = fileContent.Replace("{HouseArea}", dr["HouseArea"].ToString()); } else { fileContent = fileContent.Replace("{HouseArea}", ""); } if (dr["HouseType"] != DBNull.Value) { fileContent = fileContent.Replace("{HouseType}", dr["HouseType"].ToString()); } else { fileContent = fileContent.Replace("{HouseType}", ""); } if (dr["HouseNature"] != DBNull.Value) { fileContent = fileContent.Replace("{HouseNature}", dr["HouseNature"].ToString()); } else { fileContent = fileContent.Replace("{HouseNature}", ""); } if (dr["ApplyDate"] != DBNull.Value) { fileContent = fileContent.Replace("{ApplyDate}", Convert.ToDateTime(dr["ApplyDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); } else { fileContent = fileContent.Replace("{ApplyDate}", ""); } //替換掉換行 fileContent = fileContent.Replace("\r", "").Replace("\n","").Replace("^p","") ; //文件名字 string fileName = dr["ProposerName"].ToString() + "_投注站申請表.doc"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); // 添加頭信息,爲"文件下載/另存爲"對話框指定默認文件名 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); // 指定返回的是一個不能被客戶端讀取的流,必須被下載 HttpContext.Current.Response.ContentType = "application/ms-word"; // 把文件流發送到客戶端 HttpContext.Current.Response.Write(fileContent); // 中止頁面的執行 HttpContext.Current.Response.End(); } catch (Exception ex) { //writeLog.WriteErrorLog("根據模板生成Word文件出錯!錯誤信息:" + ex.Message); //Message.show("根據模板生成Word文件出錯!錯誤信息:" + ex.Message); } } else { //writeLog.WriteErrorLog("id=" + id + "沒有查找到任何數據!"); //Message.show("id=" + id + "沒有查找到任何數據!"); } } #endregion
到此,根據模板導出Word功能就完成了,該方法不須要服務器端安裝Office組件,便可實現Word或者Excel的相關導出功能。app