//替换并保存word
int CaseId; if (Request.QueryString["Id"] != null) { CaseId = Convert.ToInt32(Request.QueryString["Id"].ToString()); } else { return; } CaseReportPrintService cs = new CaseReportPrintService(); DataSet ds = new DataSet(); ds = cs.GetPrintCaseInfo(CaseId); string tmppath = Server.MapPath("~/Data/CaseReport.docx"); string dataDir = Server.MapPath("~/Data/"); Document doc = new Document(tmppath); //载入模板 DocumentBuilder builder = new DocumentBuilder(doc); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = ds.Tables[0].Rows[i]; doc.Range.Replace("{{RegisterSN}}", dr["RegisterSN"].ToString(), false, false); doc.Range.Replace("{{Name}}", dr["Name"].ToString(), false, false); } string fullName = "CaseReportWord_" + System.DateTime.Now.Millisecond.ToString(); string Type = ".docx"; doc.Save(dataDir + fullName + ".docx"); Response.Redirect("/ProjectWeb/Page/CaseReportPrint/DownLoadCaseReport.aspx?FilePath="+ System.Web.HttpUtility.UrlEncode(fullName.ToString())+Type); Context.Response.Write("{success:'true',fullName:'" + fullName + "'}"); } catch (Exception ex) { Context.Response.Write("{success:'false',fullName:'" + ex.Message + "'}"); }
//打开下载word文件
try { string FilePath = Request.QueryString["FilePath"].ToString(); FilePath = "/Data/" + FilePath; string fileName = string.Empty; fileName = FilePath.Substring(FilePath.LastIndexOf(@"/") + 1); FilePath = Server.MapPath(FilePath); FileStream steam = File.Open(FilePath, FileMode.Open, FileAccess.Read); byte[] data = new byte[steam.Length]; int result = steam.Read(data, 0, data.Length); if (!File.Exists(FilePath)) Response.Redirect(""); if (data.Length <= 0) data = new Byte[] { 13, 10 }; int i = FilePath.LastIndexOf('/'); steam.Close(); string UserAgent = Request.ServerVariables["http_user_agent"].ToLower(); if (UserAgent.IndexOf("firefox") == -1) {//非火狐浏览器 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName)); } else { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); } HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString()); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.BinaryWrite(data); HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { Logger.Log.Error(ex.ToString()); Response.Redirect("/Web/Page/Download/FileNotFound.htm"); }