Asp.net通过Aspose.Words,设置固定的word模板,动态填充数据,保存并下载文件

//替换并保存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");
}

 

转载于:https://www.cnblogs.com/hsdt/p/6149241.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值