/// <summary> /// Create a new file from a template /// </summary> /// <param name="template">An existing xlsx file to use as a template</param> /// <param name="password">The password to decrypt the package.</param> /// <returns></returns> private void CreateFromTemplate(FileInfo template, string password) { if (template != null) { template.Refresh(); } if (template.Exists) { if (_stream == null) { _stream = new MemoryStream(); } var ms = new MemoryStream(); if (password != null) { #if !MONO Encryption.IsEncrypted = true; Encryption.Password = password; var encrHandler = new EncryptedPackageHandler(); ms = encrHandler.DecryptPackage(template, Encryption); encrHandler = null; #endif #if MONO throw (new NotImplementedException("No support for Encrypted packages in Mono")); #endif //throw (new NotImplementedException("No support for Encrypted packages in this version")); } else { byte[] b = System.IO.File.ReadAllBytes(template.FullName); ms.Write(b, 0, b.Length); } try { //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { #if !MONO if (password == null && CompoundDocument.IsStorageFile(template.FullName) == 0) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } #endif #if MONO throw; #endif } } else { throw new Exception("Passed invalid TemplatePath to Excel Template"); } //return newFile; }
internal ZipPackagePart(ZipPackage package, ZipEntry entry) { Package = package; Entry = entry; SaveHandler = null; Uri = new Uri(package.GetUriKey(entry.FileName), UriKind.Relative); }
/// <summary> /// Closes the package. /// </summary> public void Dispose() { if (_package != null) { if (_isExternalStream == false && _stream != null && (_stream.CanRead || _stream.CanWrite)) { _stream.Close(); } CloseStream(); _package.Close(); if (_isExternalStream == false) { ((IDisposable)_stream).Dispose(); } if (_workbook != null) { _workbook.Dispose(); } _package = null; _images = null; _file = null; _workbook = null; _stream = null; _workbook = null; GC.Collect(); } }
private void ConstructNewFile(string password) { var ms = new MemoryStream(); if (this._stream == null) { this._stream = new MemoryStream(); } if (this.File != null) { this.File.Refresh(); } if (this.File != null && this.File.Exists) { if (password != null) { #if !MONO var encrHandler = new EncryptedPackageHandler(); this.Encryption.IsEncrypted = true; this.Encryption.Password = password; ms = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; #endif #if MONO throw new NotImplementedException("No support for Encrypted packages in Mono"); #endif } else { byte[] b = System.IO.File.ReadAllBytes(this.File.FullName); ms.Write(b, 0, b.Length); } try { this._package = new Packaging.ZipPackage(ms); } catch (Exception ex) { #if !MONO if (password == null && CompoundDocument.IsStorageFile(File.FullName) == 0) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } #endif #if MONO throw; #endif } } else { //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); this._package = new Packaging.ZipPackage(ms); this.CreateBlankWb(); } }
/// <summary> /// Closes the package. /// </summary> public void Dispose() { if (this._package != null) { if (this._isExternalStream == false && Stream != null && (this.Stream.CanRead || this.Stream.CanWrite)) { this.Stream.Close(); } this._package.Close(); if (this._isExternalStream == false) { ((IDisposable)this._stream).Dispose(); } if (this._workbook != null) { this._workbook.Dispose(); } this._package = null; this.Images = null; this._file = null; this._workbook = null; this._stream = null; this._workbook = null; GC.Collect(); } }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="output"></param> /// <param name="Password"></param> private void Load(Stream input, Stream output, string Password) { //Release some resources: if (this._package != null) { this._package.Close(); this._package = null; } if (this._stream != null) { this._stream.Close(); this._stream.Dispose(); this._stream = null; } _isExternalStream = true; if (input.Length == 0) // Template is blank, Construct new { _stream = output; ConstructNewFile(Password); } else { Stream ms; this._stream = output; if (Password != null) { Stream encrStream = new MemoryStream(); CopyStream(input, ref encrStream); EncryptedPackageHandler eph = new EncryptedPackageHandler(); Encryption.Password = Password; ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption); } else { ms = new MemoryStream(); CopyStream(input, ref ms); } try { //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { EncryptedPackageHandler eph = new EncryptedPackageHandler(); if (Password == null && CompoundDocument.IsCompoundDocument((MemoryStream)_stream)) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
internal ZipPackagePart(ZipPackage package, Uri partUri, string contentType, CompressionLevel compressionLevel) { Package = package; //Entry = new ZipEntry(); //Entry.FileName = partUri.OriginalString.Replace('/','\\'); Uri = partUri; ContentType = contentType; CompressionLevel = compressionLevel; }
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri, ref int id) { Uri uri = new Uri(string.Format(sUri, id), UriKind.Relative); while (package.PartExists(uri)) { uri = new Uri(string.Format(sUri, ++id), UriKind.Relative); } return uri; }
private Uri GetNewUri(Packaging.ZipPackage package, string sUri) { Uri uri; do { uri = new Uri(string.Format(sUri, _id++), UriKind.Relative); }while (package.PartExists(uri)); return(uri); }
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri, int id) { Uri uri; do { uri = new Uri(string.Format(sUri, id++), UriKind.Relative); }while (package.PartExists(uri)); return(uri); }
private void ConstructNewFile(string password) { var ms = new MemoryStream(); if (_stream == null) { _stream = new MemoryStream(); } if (File != null) { File.Refresh(); } if (File != null && File.Exists) { if (password != null) { var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; Encryption.Password = password; ms = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; } else { WriteFileToStream(File.FullName, ms); } try { //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (password == null && CompoundDocument.IsCompoundDocument(File)) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } } } else { //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(ms); CreateBlankWb(); } }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="output"></param> /// <param name="Password"></param> private void Load(Stream input, Stream output, string Password) { //Release some resources: if (this._package != null) { this._package.Close(); this._package = null; } if (this._stream != null) { this._stream.Close(); this._stream.Dispose(); this._stream = null; } if (Password != null) { Stream encrStream = new MemoryStream(); CopyStream(input, ref encrStream); EncryptedPackageHandler eph = new EncryptedPackageHandler(); Encryption.Password = Password; this._stream = eph.DecryptPackage((MemoryStream)encrStream, Encryption); } else { this._stream = output; CopyStream(input, ref this._stream); } try { //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(_stream); } catch (Exception ex) { EncryptedPackageHandler eph = new EncryptedPackageHandler(); if (Password == null && CompoundDocument.IsStorageILockBytes(CompoundDocument.GetLockbyte((MemoryStream)_stream)) == 0) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw (ex); } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="output"></param> /// <param name="Password"></param> /// <param name="cancellationToken"></param> private async Task LoadAsync(Stream input, Stream output, string Password, CancellationToken cancellationToken) { ReleaseResources(); if (input.CanSeek && input.Length == 0) // Template is blank, Construct new { _stream = output; await ConstructNewFileAsync(Password, cancellationToken).ConfigureAwait(false); } else { Stream ms; _stream = output; if (Password != null) { using (var encrStream = RecyclableMemory.GetStream()) { await CopyStreamAsync(input, encrStream, cancellationToken).ConfigureAwait(false); var eph = new EncryptedPackageHandler(); Encryption.Password = Password; ms = eph.DecryptPackage(encrStream, Encryption); } } else { ms = RecyclableMemory.GetStream(); await CopyStreamAsync(input, ms, cancellationToken).ConfigureAwait(false); } try { _zipPackage = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (Password == null && await CompoundDocumentFile.IsCompoundDocumentAsync((MemoryStream)_stream, cancellationToken).ConfigureAwait(false)) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } throw; } finally { ms.Dispose(); } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
private async Task ConstructNewFileAsync(string password) { var ms = new MemoryStream(); if (_stream == null) { _stream = new MemoryStream(); } if (File != null) { File.Refresh(); } if (File != null && File.Exists) { if (password != null) { var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; Encryption.Password = password; ms = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; } else { await WriteFileToStreamAsync(File.FullName, ms); } try { _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (password == null && await CompoundDocumentFile.IsCompoundDocumentAsync(File).ConfigureAwait(false)) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } } } else { _package = new Packaging.ZipPackage(ms); CreateBlankWb(); } }
/// <summary> /// Create a new file from a template /// </summary> /// <param name="template">An existing xlsx file to use as a template</param> /// <param name="password">The password to decrypt the package.</param> /// <returns></returns> private void CreateFromTemplate(FileInfo template, string password) { if (template != null) { template.Refresh(); } if (template.Exists) { if (_stream == null) { _stream = new MemoryStream(); } var ms = new MemoryStream(); if (password != null) { Encryption.IsEncrypted = true; Encryption.Password = password; var encrHandler = new EncryptedPackageHandler(); ms = encrHandler.DecryptPackage(template, Encryption); encrHandler = null; } else { WriteFileToStream(template.FullName, ms); } try { //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (password == null && CompoundDocument.IsCompoundDocument(ms)) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } } } else { throw new Exception("Passed invalid TemplatePath to Excel Template"); } //return newFile; }
private void ReleaseResources() { //Release some resources: if (_package != null) { _package.Close(); _package = null; } if (_stream != null) { _stream.Close(); _stream.Dispose(); _stream = null; } _isExternalStream = true; }
/// <summary> /// Closes the package. /// </summary> public void Dispose() { if (_package != null) { if (Stream != null && (Stream.CanRead || Stream.CanWrite)) { Stream.Close(); } _package.Close(); ((IDisposable)_stream).Dispose(); ((IDisposable)_workbook).Dispose(); _package = null; _images = null; _file = null; _workbook = null; _stream = null; _workbook = null; } }
/// <summary> /// Create a new instance of the ExcelPackage class based on a stream /// </summary> /// <param name="newStream">The stream object can be empty or contain a package. The stream must be Read/Write</param> /// <param name="Password">The password to decrypt the document</param> public ExcelPackage(Stream newStream, string Password) { if (!(newStream.CanRead && newStream.CanWrite)) { throw new Exception("The stream must be read/write"); } Init(); if (newStream.Length > 0) { Load(newStream, Password); } else { _stream = newStream; //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); TODO:Remove _package = new Packaging.ZipPackage(_stream); CreateBlankWb(); } }
/// <summary> /// Create a new instance of the ExcelPackage class based on a stream /// </summary> /// <param name="newStream">The stream object can be empty or contain a package. The stream must be Read/Write</param> /// <param name="Password">The password to decrypt the document</param> public ExcelPackage(Stream newStream, string Password) { if (!(newStream.CanRead && newStream.CanWrite)) { throw new Exception("The stream must be read/write"); } this.Init(); if (newStream.Length > 0) { this.Load(newStream, Password); } else { this._stream = newStream; this._isExternalStream = true; this._package = new Packaging.ZipPackage(_stream); this.CreateBlankWb(); } }
/// <summary> /// Closes the package. /// </summary> public void Dispose() { if (_package != null) { if (_isExternalStream == false && _stream != null && (_stream.CanRead || _stream.CanWrite)) { CloseStream(); } _package.Close(); if (_workbook != null) { _workbook.Dispose(); } _package = null; File = null; _workbook = null; _stream = null; _workbook = null; GC.Collect(); } }
private void Load(Stream input, Stream output, string password) { //Release some resources: if (this._package != null) { this._package.Close(); this._package = null; } if (this._stream != null) { this._stream.Close(); this._stream.Dispose(); this._stream = null; } this._isExternalStream = true; if (input.Length == 0) // Template is blank, Construct new { this._stream = output; this.ConstructNewFile(password); } else { Stream ms; this._stream = output; if (password != null) { #if !MONO Stream encrStream = new MemoryStream(); ExcelPackage.CopyStream(input, ref encrStream); EncryptedPackageHandler eph = new EncryptedPackageHandler(); this.Encryption.Password = password; ms = eph.DecryptPackage((MemoryStream)encrStream, this.Encryption); #endif #if MONO throw new NotSupportedException("Encryption is not supported under Mono."); #endif } else { ms = new MemoryStream(); ExcelPackage.CopyStream(input, ref ms); } try { //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite); this._package = new Packaging.ZipPackage(ms); } catch (Exception ex) { #if !MONO EncryptedPackageHandler eph = new EncryptedPackageHandler(); if (password == null && CompoundDocument.IsStorageILockBytes(CompoundDocument.GetLockbyte((MemoryStream)_stream)) == 0) { throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex); } else { throw; } #endif #if MONO throw; #endif } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri) { var id = 1; return(GetNewUri(package, sUri, ref id)); }
internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri) { return(GetNewUri(package, sUri, 1)); }