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

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


相关文章

取消 "将 null 文本或可能的 null 值转换为不可为 null 类型。 "提示

在 .csproj 文件中添加或修改:<PropertyGroup>  <Nullable>disable</Nullable></Pro...

DBeaver连接oracle 下载驱动报错

https://blog.csdn.net/weixin_45764765/article/details/124327194...

asp.net core json解析

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

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

 public void OnGet()  {         ...

asp.net core 绑定下拉

   public List<SelectListItem> zplx { get; set; }  [BindProperty]  public...

普能html页面post到 cshtml页面,出现400错误解决方法

  [IgnoreAntiforgeryToken]  public class keywordsModel : PageModel  {    &n...

发表评论    

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