/// <summary> /// Copies the Package to the Outstream /// The package is closed after it has been saved /// </summary> /// <param name="OutputStream">The stream to copy the package to</param> public void SaveAs(Stream OutputStream) { File = null; Save(); if (Encryption.IsEncrypted) { //Encrypt Workbook Byte[] file = new byte[Stream.Length]; long pos = Stream.Position; Stream.Seek(0, SeekOrigin.Begin); Stream.Read(file, 0, (int)Stream.Length); EncryptedPackageHandler eph = new EncryptedPackageHandler(); var ms = eph.EncryptPackage(file, Encryption); CopyStream(ms, ref OutputStream); } else { CopyStream(_stream, ref OutputStream); } }
internal byte[] GetAsByteArray(bool save) { if(save) Workbook.Save(); _package.Close(); Byte[] byRet = new byte[Stream.Length]; long pos = Stream.Position; Stream.Seek(0, SeekOrigin.Begin); Stream.Read(byRet, 0, (int)Stream.Length); //Encrypt Workbook? if (Encryption.IsEncrypted) { EncryptedPackageHandler eph=new EncryptedPackageHandler(); var ms = eph.EncryptPackage(byRet, Encryption); byRet = ms.ToArray(); } Stream.Seek(pos, SeekOrigin.Begin); Stream.Close(); return byRet; }
/// <summary> /// Saves all the components back into the package. /// This method recursively calls the Save method on all sub-components. /// We close the package after the save is done. /// </summary> public void Save() { try { Workbook.Save(); if (File == null) { _package.Close(); } else { if (System.IO.File.Exists(File.FullName)) { try { System.IO.File.Delete(File.FullName); } catch (Exception ex) { throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex)); } } 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); ms.Close(); } else { fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length); Stream.Close(); } fi.Close(); } else { System.IO.File.WriteAllBytes(File.FullName, GetAsByteArray(false)); } } } catch (Exception ex) { if (File == null) { throw (ex); } else { throw (new InvalidOperationException(string.Format("Error saving file {0}", File.FullName), ex)); } } }