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

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


相关文章

asp.net core webapi 调用furion 的webapi json接口

/// <summary> /// 根据学校读取支付科次 /// </summary> /// <returns>&l...

Razor 遍历 DataTable

@using System.Data <table class="table table-bordered table-striped&n...

设置edge浏览器自动填充 input password值

<form autocomplete="off">    <input type="password" name=&quo...

ubuntu 删除asp.net core 8.0

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

文件上传

@page @model xxtsoft.Pages.uploadModel @{    } <h1>@ViewData["Title...

发表评论    

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