【发布时间】:2017-06-24 15:14:17
【问题描述】:
我有以下函数可以为我的 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 文件。
我希望它返回一个压缩的单个文件。
这是当前结果的样子。
在使用@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;
}
}
}
}
【问题讨论】:
-
看起来应该是这样。 xlsx 文件是压缩的 opendocument 结构。我认为您的文件浏览器只是在目录层次结构中透明地显示 ZIP 内容(给定左上角的图标)。当您将 .zip 重命名为 .xlsx 时,您将能够使用 Excel 打开它。
-
+ 你从来没有给
newEntryStream写过任何东西?这个函数似乎只是返回带有 .zip 扩展名的 EPPlus 输出流(内部已经是 ZIP 并且应该具有 xlsx 扩展名)。 -
看来你是对的。我去看看。
标签: c# excel zip archive epplus