This project has moved and is read-only. For the latest updates, please go here.

SaveAs after Save throw an exception

Oct 21, 2011 at 10:14 AM

When i'm trying to SaveAs after Save it throws an exception, becouse the package is disposed.

I'm seeing that at source code in save method. I think is the issue.

if (Stream is MemoryStream)
{
    _package.Close();
    var fi = new FileStream(File.FullName, FileMode.Create);
    //EncryptPackage
    if (Encryption.IsEncrypted)
    {
        byte[] file = ((MemoryStream)Stream).ToArray();
        EncryptedPackageHandler eph = new EncryptedPackageHandler();
        var ms = eph.EncryptPackage(file, Encryption);

        fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
    }
    else
    {
        fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
    }
    fi.Close();
}

http://www.java2s.com/Open-Source/CSharp/Chart/EPPlus/OfficeOpenXml/ExcelPackage.cs.htm

How can i do it?

PD: that's not my release. I have the last one.

 

if (Stream is MemoryStream)
                    {
                        _package.Close();
                        var fi = new FileStream(File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (Encryption.IsEncrypted)
                        {
                            byte[] file = ((MemoryStream)Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
                        }
                        else
                        {
                            fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
                        }
                        fi.Close();
                    }
Oct 28, 2011 at 3:16 PM

Yes, the package is closed on save. Use a file copy, save it to a stream or somthing like this ...

byte[] doc = package.GetAsByteArray();
File.WriteAllByte("c:\yourfile1.xlsx", doc);
File.WriteAllByte("c:\yourfile2.xlsx", doc);