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

浪淘沙1年前后端163
   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 发送模板消息

 private readonly HttpClient _httpClient;  public testModel(HttpCli...

asp.net core furion 前端传入json,后端api接入并角析

{   "result": {     "cover": {  ...

JNTemplate 文章标签解析

using JinianNet.JNTemplate; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetC...

asp.net core Aspose.Words 文本换行

using Aspose.Words;// 替换换行符为 Word 中的换行符var placeholders = new&nbs...

asp.net core 图片转正

在 ASP.NET Core 中将图片旋转为正向,通常你需要处理图片的 EXIF 信息,因为一些相机和手机拍摄的照片会包含方向信息,表示照片应该如何旋转才能显示为正确的方向。可以使用一个图像处理库来实...

SqlSugar

   var dt = _db.SqlQueryable<object>("select * fr...

发表评论    

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