[英]Problems opening .xlsx file created with EPPlus and zipped in a folder with ICSharpCode.SharpZipLib
I am creating a list of ExcelPackages (xlsx documents) using EPPlus, and adding them to a ZipOutputStream as ZipEntries. 我正在使用EPPlus创建ExcelPackages(xlsx文档)列表,并将它们作为ZipEntries添加到ZipOutputStream。 I think the Excel documents should be valid, as I can open them just fine when I write one of them to the Response object without zipping.
我认为Excel文档应该是有效的,因为当我将其中一个文件写入Response对象而不进行压缩时,我可以正常打开它们。
The zip folder is created as expected, and the file(s) are there and doesnt seem to be empty, but when I try to open them I get the following error in Excel: zip文件夹是按预期创建的,文件在那里并且似乎没有空,但是当我尝试打开它们时,我在Excel中收到以下错误:
Excel cannot open the file {Name}.xlsx because the file format or file extension is not valid.
Excel无法打开文件{Name} .xlsx,因为文件格式或文件扩展名无效。 Verify that file has not been corrupted and that the file extension matches the format of the file
验证文件是否已损坏,以及文件扩展名是否与文件格式匹配
List<ExcelPackage> documents = new List<ExcelPackage>();
List<string> fileNames = new List<string>();
//Code for fetching documents and filenames here (removed for the sake of readability)
Response.Clear();
Context.Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=\"random-foldername.zip\"");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
zipOutputStream.SetLevel(3); //0-9, 9 being the highest level of compression
byte[] buffer = null;
for (int i = 0; i < documents.Count; i++)
{
MemoryStream ms = new MemoryStream();
documents[i].SaveAs(ms);
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(fileNames[i]));
zipOutputStream.PutNextEntry(entry);
buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
entry.Size = ms.Length;
ms.Close();
zipOutputStream.Write(buffer, 0, buffer.Length);
zipOutputStream.CloseEntry();
}
zipOutputStream.Finish();
zipOutputStream.Close();
Response.End();
As for the list of filenames, im just generating a name based on some arbitrary stuff and adding a ".xlsx"-extension to the end of it. 至于文件名列表,我只是根据一些任意的东西生成一个名称,并在其末尾添加一个“.xlsx” - 扩展。
Im not sure where im going wrong here, any suggestions? 我不知道我在哪里出错了,有什么建议吗?
You have to rewind memory stream before you can read something (after write operation file pointer is that its end): 您必须先回滚内存流,然后才能读取内容(在写入操作文件指针之后):
ms.Seek(0, SeekOrigin.Begin)
ms.Read(buffer, 0, buffer.Length);
That said a MemoryStream
is nothing more than a byte array so you don't even need to allocate and read a new one then this code: 这就是说
MemoryStream
只不过是一个字节数组,所以你甚至不需要分配和读取一个新的代码:
buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
entry.Size = ms.Length;
ms.Close();
zipOutputStream.Write(buffer, 0, buffer.Length);
Can be simply replaced with: 可以简单地替换为:
entry.Size = ms.Length;
zipOutputStream.Write(ms.GetBuffer(), 0, ms.Length);
ms.Close();
Final note: if you don't want to use internal MemoryStream
buffer (for any reason) and you want a trimmed copy of it (as you're doing manually) then simply use ToArray()
method like this: 最后注意:如果您不想使用内部
MemoryStream
缓冲区(出于任何原因)并且您想要它的修剪副本(当您手动执行时),那么只需使用ToArray()
方法,如下所示:
var buffer = ms.ToArray();
ms.Close();
entry.Size = buffer.Length;
zipOutputStream.Write(buffer, 0, buffer.Length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.