I'm trying to create several workbooks in memory and putting them into a zip file that will be returned to the browser for download. Combining the example here to deliver an XLWorkbook for download and using ZipArchive functionality in the newest versions of ASP, here's a snippet of what I've done:
using (var ZipStream = new MemoryStream())
I've also tried other variations in my code like using
streamWriter.Write(workbook);
instead of
Workbook.SaveAs(ZipStream);
and I end up getting a zip file w/ all the workbooks I wanted. But I can't open any of the workbooks and instead I get a 'file corrupted' message.
Has anyone else been able to write and zip several Excel docs in memory and delivered directly to the browser for download?
using (var ZipStream = new MemoryStream())
{
using (var Archive = new ZipArchive(ZipStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < ExperimentList.Count(); i++)
{
WorkbookFileName = "Experiment_" + ExperimentList[i].ExperimentID.ToString() + ".xlsx";
dt = GetData(ExperimentList[i].ExperimentID);
Workbook = GenerateExcelDoc(dt, WorkbookFileName, SheetName);
var File = Archive.CreateEntry(WorkbookFileName);
using (var entryStream = File.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
Workbook.SaveAs(ZipStream);
ZipStream.Seek(0, SeekOrigin.Begin);
}
}
}
// create HTTP Response
HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
httpResponse.Clear();
httpResponse.ContentType = "application/zip";
httpResponse.AddHeader("content-disposition", "attachment; filename=" + ZipFileName);
// deliver zip file from memory stream to browser
//ZipStream.Seek(0, SeekOrigin.Begin);
ZipStream.WriteTo(httpResponse.OutputStream);
ZipStream.Seek(0, SeekOrigin.Begin);
ZipStream.Close();
httpResponse.End();
}
When I open the zip file, I don't see any workbooks. Instead I get what I believe are components to make an XLWorkbook. Here's the top directory in the zip:- _rels/
- docProps/
- package/
-
xl/
[Content_Types].xml
I've also tried other variations in my code like using
streamWriter.Write(workbook);
instead of
Workbook.SaveAs(ZipStream);
and I end up getting a zip file w/ all the workbooks I wanted. But I can't open any of the workbooks and instead I get a 'file corrupted' message.
Has anyone else been able to write and zip several Excel docs in memory and delivered directly to the browser for download?