標籤:
利用html模板產生Word檔案(伺服器端不需要安裝Word)
由於管理的原因,不能在伺服器上安裝Office相關組件,所以只能採用用戶端讀取Html模板,後台對模板中標記的欄位資料替換並返回給用戶端的方法來實現,經過測試這種方法也是一種不錯的選擇!
首先自己寫一個html網頁模板,代碼如下:
<!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>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 [email protected] "; 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 + "沒有尋找到任何資料!"); } } #endregionView Code
到此,根據模板匯出Word功能就完成了,該方法不需要伺服器端安裝Office組件,即可實現Word或者Excel的相關匯出功能。
利用html模板產生Word檔案(伺服器端不需要安裝Word)