简体   繁体   English

Excel 文件在使用 EPPLUS 生成多个 excel 文件并使用 DotNetZip 从 C# 中的 ActionResult 压缩后损坏

[英]Excel file is corrupted after generating multiple excel files using EPPLUS and Zipped using DotNetZip from an ActionResult in C#

I need to export multiple excel containing data from database.我需要从数据库中导出多个包含数据的 excel。 I'm using EPPLUS to do that.我正在使用 EPPLUS 来做到这一点。 Since I can't return multiple file from an ActionResult I've to ZIP the excel files and then return that ZIP file.由于我无法从 ActionResult 返回多个文件,因此我将 ZIP excel 文件返回,然后返回 ZIP 文件。 I've successfully zipped it and downloaded the zip file but when I try to open any excel file in the zip file it says my excel file is corrupted. I've successfully zipped it and downloaded the zip file but when I try to open any excel file in the zip file it says my excel file is corrupted. If there is a single excel file in the zip file it works fine.如果 zip 文件中有一个 excel 文件,则它可以正常工作。

Here is my code-这是我的代码-

public ActionResult ExportExcel()
        {
            try
            {
                projects="project1,project2";
                var memoryStream = new MemoryStream();
                var zip = new ZipFile();
                var arrProject = projects.Split(',');
                foreach (var pro in arrProject)
                {
                    var v = GetProjectData(pro);//method to get data from database
                    //Construct DataTable
                    var dt = new DataTable();
                    dt.Columns.Add("Model", typeof(string));
                    dt.Columns.Add("Color", typeof(string));
                    dt.Columns.Add("BarCode", typeof(long));
                    dt.Columns.Add("BarCode2", typeof(long));
                    //Load data to DataTable
                    foreach (var item in v)
                    {
                        var row = dt.NewRow();
                        row["Model"] = item.Model;
                        row["Color"] = item.Color;
                        row["BarCode"] = Int64.Parse(item.BarCode);
                        row["BarCode2"] = Int64.Parse(item.BarCode2);
                        dt.Rows.Add(row);
                    }
                    //transfer data from DataTable to worksheet
                    using (var package = new ExcelPackage())
                    {
                        var worksheet = package.Workbook.Worksheets.Add("IMEI");
                        worksheet.Cells["A1"].LoadFromDataTable(dt, PrintHeaders: true);
                        for (var col = 1; col < dt.Columns.Count + 1; col++)
                        {
                            worksheet.Column(col).AutoFit();
                        }
                        zip.AddEntry(pro + ".xlsx", package.GetAsByteArray());
                        zip.Save(memoryStream);
                    }
                }
                return File(memoryStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Zip, "report.zip");
            }
            catch (Exception ex)
            {
                
            }
            
        }

The problem with your code at line zip.Save(memoryStream); zip.Save(memoryStream);行代码的问题which is being saving on each file.正在保存在每个文件上。

Move that line before return statement would make one zip file containing all files.在 return 语句之前移动该行将生成一个包含所有文件的 zip 文件。 Convert to as follows.转换为如下。

zip.Save(memoryStream);
return File(memoryStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Zip, "report.zip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM