yuhuihu / excellibrary

Automatically exported from code.google.com/p/excellibrary
0 stars 0 forks source link

Unable to create xls stream in memory without writing on disk #62

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.I am trying to create web server that will generate excel file from some 
database data and offer user to download it.
2.
3.

What is the expected output? What do you see instead?
I have to save temporary file on disk and then make stream of it.

What version of the product are you using? On what operating system?

Please provide any additional information below.
It will be simple to fix this problem. Only following method should be added in 
CompoundDocument class:

public static CompoundDocument Create(MemoryStream stream)
{
CompoundDocument document = new CompoundDocument(stream, new 
CompoundFileHeader());
      document.WriteHeader();
      document.MasterSectorAllocation.AllocateSATSector();
      document.InitializeDirectoryEntries();
      return document;
}

Original issue reported on code.google.com by milorad....@gmail.com on 20 Jul 2010 at 3:47

GoogleCodeExporter commented 9 years ago
I have the same issue. Saving to a memory stream does not appear to work 
correctly, and results in corrupted files. Saving exactly the same document to 
disk results in a correct file.

Inspecting the bytes, it appears that a header is not being added to the stream 
version...

Original comment by snick...@gmail.com on 26 Jan 2012 at 11:07

GoogleCodeExporter commented 9 years ago
Is this fixed?  Is there a work around that doesn't involve saving the .xls 
file to the server?
thanks!

Original comment by scif...@gmail.com on 1 Feb 2013 at 1:50

GoogleCodeExporter commented 9 years ago
I have been troubleshooting this today and found that the stream created in 
memory (i.e., the MemoryStream) just needs to be "rewound" since it returns 
partially advanced. You can inspect the Position property of your memory stream 
and see it is at 512 instead of zero like it should be. So in my case, one line 
of code allowed me to work around this:

MemoryStream memStream = new MemoryStream();
book.Save(memStream);
// Per ExcelLibrary issue #62, must rewind the MemoryStream position back to 
zero!
memStream.Position = 0;
// Yay! File generated by memStream is no longer corrupt.

Best of luck!

Original comment by funka...@gmail.com on 28 Feb 2013 at 7:53