【问题标题】:Generate multiple word document from foreach loop从 foreach 循环生成多个 word 文档
【发布时间】:2020-11-18 04:51:30
【问题描述】:

我需要使用 foreach 循环生成多个 word 文档并下载到磁盘。对于 word 文档,我使用的是 Syncfusion。我只能从我的程序中下载一个文档。那是因为我在循环中使用了 return 语句。返回后如何继续循环,或者有什么方法可以在循环内不返回? 这是我的代码

public async Task<IActionResult> CreateDocument(int? id)
    {
        string text = "some text"
        List<OrderUser> teamList = await _context.OrderUser.Where(o => o.OrderId == id).Include(o => o.Order).Include(o => o.ApplicationUser).ToListAsync();
        using (WordDocument document = new WordDocument())
        {
            IWSection section = document.AddSection();
            IWParagraph paragraph = section.AddParagraph();

            paragraph.AppendText(text);

            foreach (var item in teamList)
            {
                paragraph.AppendText("Mr " + item.ApplicationUser.DisplayName);
                MemoryStream stream = new MemoryStream();

                document.Save(stream, FormatType.Docx);

                stream.Position = 0;

                return File(stream, "application/msword", "Result.docx");
            }

            return null;
        }
    }

【问题讨论】:

    标签: c# .net-core syncfusion


    【解决方案1】:

    在我看来,不可能通过 HTTP 发回多个“文档”。

    您可以尝试将文件保存在内存中或将它们存储在临时文件夹中,然后将所有文件压缩在一起并将压缩文件返回给用户。

    【讨论】:

      【解决方案2】:

      为实现此要求,您可以将 Word 文档添加到 zip 文件中,并使用 Syncfusion.Compression 将多个 Word 文档保存在单个 zip 文件中。为满足您的需求,我们建议您使用以下代码 sn-p 一次下载多个 Word 文档。

      public IActionResult Index(string button)
      {
      if (button == null)
      return View();
      
      string text = "some text";
      List<string> items = new List<string>(){ "First","Second","Third","Fourth"};
      //Initializes new instance of ZipAchive
      ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
      //Sets compression level for new items
      zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;
      for(int i=0;i<items.Count;i++)
      {
      using (WordDocument document = new WordDocument())
      {
      //Adds new section to the document
      IWSection section = document.AddSection();
      //Add new paragraph to the section
      IWParagraph paragraph = section.AddParagraph();
      //Appends the specified text to the paragraph
      paragraph.AppendText(text);
      paragraph.AppendText(items[i]);
      MemoryStream stream = new MemoryStream();
      //Save the document
      document.Save(stream, FormatType.Docx);
      stream.Position = 0;
      //Adds new item to the archive
      zipArchive.AddItem("Sample"+i.ToString()+ ".docx", stream, false, (Syncfusion.Compression.FileAttributes)FileAttributes.Normal);
      }
      }
      //Zips the filename
      MemoryStream memoryStream = new MemoryStream();
      //Saves the zip file
      zipArchive.Save(memoryStream, false);
      return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
      } 
      

      更多信息请参考: https://www.syncfusion.com/kb/1922/is-it-possible-to-create-the-zip-files-using-syncfusion-compression-zip

      注意:适用于 Syncfusion。

      【讨论】:

        猜你喜欢
        • 2018-10-04
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 1970-01-01
        相关资源
        最近更新 更多