使用EPPLUS C#

时间:2017-02-07 11:32:05

标签: c# excel zip archive epplus

我有以下函数为我的ASP.NET MVC应用程序返回FileStreamResult

/// <summary>
/// Generates a FileStreamResult containing a zip file with the EXCEL file in it
/// </summary>
/// <typeparam name="T">Type of object in the object list parameter</typeparam>
/// <param name="objectList">The object list enumerable. This contains the data for the EXCEL file</param>
/// <param name="fileName">The file name of the EXCEL</param>
/// <returns>FileStreamResult</returns>
public FileStreamResult CreateZipFileFileStreamResult<T>(IEnumerable<T> objectList, string fileName)
{   
    var ms = new MemoryStream();

    var contentType = System.Net.Mime.MediaTypeNames.Application.Zip;

    ExcelPackage excelPackage = null;

    ZipArchive archive = null;

    try
    {
        excelPackage = new ExcelPackage(ms);

        var workSheet1 = excelPackage.Workbook.Worksheets.Add("Sheet1");

        workSheet1.Cells["A1"].LoadFromCollection<T>(objectList, true);

        excelPackage.SaveAs(ms);



        ms.Seek(0, SeekOrigin.Begin);

        archive = new ZipArchive(excelPackage.Stream, ZipArchiveMode.Create, true);

        var newEntry = archive.CreateEntry(fileName, System.IO.Compression.CompressionLevel.Fastest);

        var newEntryStream = newEntry.Open();

        var fsr = new FileStreamResult(excelPackage.Stream, contentType);
        fsr.FileDownloadName = fileName + ".zip";

        return fsr;
    }
    catch (Exception ex)
    {
        if (archive != null)
            archive.Dispose();

        if (excelPackage != null)
            excelPackage.Dispose();

        if (ms != null)
            ms.Dispose();

        throw;
    }
}

该函数返回一些内容,但它是以分割XML方式而不是一个XLSX文件。

我希望它返回一个ZIPPED单个文件。

这是目前的结果。

ZIP archive structure

使用了@kuujinbo提供的帮助后,我创建了这个功能。 请注意,由于某种原因FileContentResult工作,FileStreamResult不起作用。

        /// <summary>
        /// Generates a FileStreamResult containing a zip file with the EXCEL file in it
        /// </summary>
        /// <typeparam name="T">Type of object in the object list parameter</typeparam>
        /// <param name="objectList">The object list enumerable. This contains the data for the EXCEL file</param>
        /// <param name="fileName">The file name of the EXCEL</param>
        /// <returns>FileStreamResult</returns>        
        public FileContentResult CreateZipFileFileContentResult<T>(IEnumerable<T> objectList, string fileName)
        {
            var contentType = System.Net.Mime.MediaTypeNames.Application.Zip;

            using (var memoryStream = new System.IO.MemoryStream())
            {
                using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                {
                    using (var package = new OfficeOpenXml.ExcelPackage())
                    {
                        var workSheet1 = package.Workbook.Worksheets.Add("Sheet1");
                        workSheet1.Cells["A1"].LoadFromCollection<T>(objectList, true);

                        var firstRow = workSheet1.Row(1);
                        if (firstRow != null)
                            firstRow.Style.Font.Bold = true;

                        zip.AddEntry(fileName, package.GetAsByteArray());
                        zip.Save(memoryStream);
                        var fcr = new FileContentResult(memoryStream.ToArray(), contentType); //NOTE: Using a File Stream Result will not work.
                        fcr.FileDownloadName = fileName + ".zip";
                        return fcr;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:3)

由于EPPlus 内部使用版本DotNetZip ,(看一下源代码)尝试并执行相同的操作。恕我直言,他们的设计决定告诉了很多人为什么选择来使用ZipArchive

_.map(gillFamily, (el) => el.name, (el) => el.age < 50);

经过测试和工作:

enter image description here