使用EPPLUS C#创buildExcel XLSX文件的ZipArchive

我有以下函数返回我的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归档结构

使用@kuujinbo给出的帮助,我创build了这个function。 请注意,出于某种原因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; } } } } 

由于EPPlus在内部使用DotNetZip的一个版本(请看源代码),请尝试执行相同的操作。 恕我直言,他们的devise决定告诉了很多关于为什么一些select使用ZipArchive 。

 class TestObject { public int Id { get; set; } public string Name { get; set; } } IEnumerable<TestObject> objectList = new List<TestObject>() { { new TestObject() {Id = 0, Name = "zero" } }, { new TestObject() {Id = 1, Name = "one" } } }; string ExcelName = "test.xlsx"; string ZipName = "test.zip"; public ActionResult DotnetZip() { using (var stream = new MemoryStream()) { using (ZipFile zip = new ZipFile()) { using (var package = new ExcelPackage()) { var sheet = package.Workbook.Worksheets.Add("Sheet1"); sheet.Cells["A1"].LoadFromCollection<TestObject>(objectList, true); zip.AddEntry(ExcelName, package.GetAsByteArray()); zip.Save(stream); return File( stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Zip, ZipName ); } } } } 

testing和工作:

在这里输入图像说明