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

浪淘沙1年前后端145
   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");


相关文章

点击按钮,禁用提示,加导出word

@page @model xxtsoft.Web.Entry.Pages.sysadmins.kh.kh_outModel @{     Lay...

asp.net core弹出对话框最佳实践

 public void OnGet()  {         ...

asp.net core razor onpost

不管 OnGet 填充多少东西OnPost 必须再填一次因为 Razor Pages 不会“记住” ViewModelPOST 后是重新执行一个请求,Model 全部重新创建。不像 WebForms,...

asp.net core json解析

一、json{   "cover": {     "title": &quo...

发表评论    

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