Aspose.Words asp.net core razor 根据模板文件导出多个内容并合在一个文档中下载

浪淘沙2年前后端246
   DataTable dt = _db.Ado.GetDataTable("select  * from pub_member where groupid<>3 and xld<>1 order by id asc");

   string templatePath = FileHelper.absolutPath("wwwroot/mb/kh.docx"); //模板文件路径
                                                                       //
   var template = new Document(templatePath); //加载模板

   // 创建合并文档
   var combinedDocument = new Document();
   combinedDocument.RemoveAllChildren();

   // 生成多个副本并合并
   for(int i=0;i<dt.Rows.Count;i++)
   {
       var tempDocument = template.Clone();

      
       #region 替换占位符
       var placeholders = new Dictionary<string, string>
   {
           { "{xm}", "111" }
          
   };

       foreach (var placeholder in placeholders)
       {
           tempDocument.Range.Replace(placeholder.Key, placeholder.Value, new Aspose.Words.Replacing.FindReplaceOptions());
       }
       #endregion
       // 合并到目标文档
       combinedDocument.AppendDocument(tempDocument, ImportFormatMode.KeepSourceFormatting);
   }

   var stream = new MemoryStream();
   combinedDocument.Save(stream, SaveFormat.Docx);
   stream.Position = 0;

   // 返回文件时不使用 using,避免释放 Stream
   return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "1.docx");


相关文章

ubuntu 删除asp.net core 8.0

1. 查找已安装的 ASP.NET Core 8.0 版本dotnet --list-runtimes dotnet --list-sdks2. 删除 ASP.NET Core...

asp.net core razor 输出html

@Html.Raw(Model.HtmlContent)...

asp.net core 中文 url 编码

 return Redirect("tj_send?fst=" + fst + "&rq1=" + rq1 + "&rq2=...

asp.net core rzaor page 中的ViewComponent 视图用法

asp.net core rzaor page 中的ViewComponent 视图用法

1、/Pages/Components 新建:MyViewComponent.csusing Microsoft.AspNetCore.Mvc; namespace xxts...

asp.net core razor 远程请求 认证Basic auth 模式 body传递 json

private readonly HttpClient _httpClient;    public RemoteReq...

asp.net core razor 获取当前url

 @Request.Scheme://@Request.Host/anysc/dd_ts_stu...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。