This project has moved. For the latest updates, please go here.

FileStream instead of FileInfo

Apr 28, 2011 at 9:40 PM

Hi guys,

I'm trying to create an Excel file using a FileStream object instead of a FileInfo object. I noticed the OfficeOpenXml.ExcelPackage constructor can take a FileStream object, but I'm not having luck with implementing it. I want to use a FileStream instead of FileInfo because I'm using a formatter class that's currenting creating and sharing a FileStream.

Here's an example I've created.

        public void makeStream()
        {
            System.IO.Stream myStream;

            try
            {
                //create a file stream (write only)
                myStream = new System.IO.FileStream("c:\\stream_writeOnly.xlsx", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);

                //make a sample file
                makeExcel(myStream);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                //Message=Can not read from inputstream
                //Source=EPPlus
                                
            }

            try
            {
                //create a file stream read/write
                myStream = new System.IO.FileStream("c:\\stream_readWrite.xlsx", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);

                //make a sample file
                makeExcel(myStream);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                //Message=A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))
                //Source=EPPlus
            }

            try
            {
                //create a file info object
                System.IO.FileInfo myFileInfo = new System.IO.FileInfo("c:\\stream_from_fileInfo.xlsx");
                
                //create a package
                OfficeOpenXml.ExcelPackage jinx = new OfficeOpenXml.ExcelPackage(myFileInfo);
                
                //create a worksheet
                jinx.Workbook.Worksheets.Add("Jinx");

                //save the file
                jinx.Save();

                //make a sample file
                makeExcel(jinx.Stream);                
                //no error, but doesn't work as expected :(

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                //Message=
                //Source=
            }

        }

        public void makeExcel(System.IO.Stream myStream)
        {
            OfficeOpenXml.ExcelPackage myPackage;
            OfficeOpenXml.ExcelWorksheet myWs;

            //create a package.
            myPackage = new OfficeOpenXml.ExcelPackage(myStream);

            //set the package title
            myPackage.Workbook.Properties.Title = "Stream Sample";
            
            //add a worksheet
            myWs = myPackage.Workbook.Worksheets.Add("Stream Test");
            myWs.Cells[1, 1].Value = "Testing";

            //save the file
            myPackage.Save();            
            
        }

Lastly let me just say that EPPlus is an excellent utility and I love using it. Up until now the DLL has worked flawlessly for me :)

 

Coordinator
Apr 29, 2011 at 2:58 PM

Hi,

I suggest you try the ExcelPackage.SaveAs-method to save to your File-stream, something like in web sample 1...

ExcelPackage pck = new ExcelPackage();
...
pck.SaveAs(yourStream);

Your first sample uses an write only stream. That is not supported in the constructor. It must be read/write.

The second sample should work, but the component crashes when you add an empty stream. I'll add a fix for that.

Jan

May 2, 2011 at 3:25 PM
Edited May 2, 2011 at 3:26 PM

Thank you for the reply Jan. Your suggestion will work :)

I never thought about simply creating a Package object and saving it to a stream as the last step.

BTW, I've noticed the "SaveAs" method also works on WRITE only streams.

Have a great week.