我有以下函數爲我的ASP.NET MVC應用程序返回FileStreamResult
。使用EPPLUS創建Excel XLSX文件的ZipArchive C#
/// <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單個文件。
這是當前結果的樣子。
已經使用我創造了這個功能@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打開它。 – dlatikay
+你從不向'newEntryStream'寫任何東西?這個函數似乎只是返回帶有.zip擴展名的EPPlus輸出流(內部已經是ZIP,應該有xlsx擴展名)。 – dlatikay
它說你是對的。我會看看。 –