/// <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) { _stream = new MemoryStream(); if (password != null) { Encryption.IsEncrypted = true; Encryption.Password = password; var encrHandler = new EncryptedPackageHandler(); _stream = encrHandler.DecryptPackage(template, Encryption); encrHandler = null; //throw (new NotImplementedException("No support for Encrypted packages in this version")); } else { byte[] b = System.IO.File.ReadAllBytes(template.FullName); _stream.Write(b, 0, b.Length); } try { _package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); } catch (Exception ex) { if (password == null && EncryptedPackageHandler.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 (ex); } } } else { throw new Exception("Passed invalid TemplatePath to Excel Template"); } //return newFile; }
private void ConstructNewFile(Stream stream, string password) { _stream = stream; if (File != null) { File.Refresh(); } if (File != null && File.Exists) { if (password != null) { var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; Encryption.Password = password; _stream = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; } else { ReadFile(); } try { //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(_stream); } catch (Exception ex) { 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 (ex); } } } else { //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); _package = new Packaging.ZipPackage(_stream); CreateBlankWb(); } }
/// <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) { Stream encrStream = new MemoryStream(); await CopyStreamAsync(input, encrStream, cancellationToken).ConfigureAwait(false); var eph = new EncryptedPackageHandler(); Encryption.Password = Password; ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption); } else { ms = new MemoryStream(); 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; } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
private async Task ConstructNewFileAsync(string password, CancellationToken cancellationToken) { var ms = new MemoryStream(); if (_stream == null) { _stream = new MemoryStream(); } 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); } else { await WriteFileToStreamAsync(File.FullName, ms, cancellationToken).ConfigureAwait(false); } try { _zipPackage = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (password == null && await CompoundDocumentFile.IsCompoundDocumentAsync(File, 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; } } else { _zipPackage = new Packaging.ZipPackage(ms); CreateBlankWb(); } }
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; }
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) { #if !MONO var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; 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(File.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(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); _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.Flush(); 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); } catch (Exception ex) { EncryptedPackageHandler eph = new EncryptedPackageHandler(); if (Password == null && EncryptedPackageHandler.IsStorageILockBytes(eph.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> /// 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) { _stream = new MemoryStream(); if (password != null) { Encryption.IsEncrypted = true; Encryption.Password = password; var encrHandler = new EncryptedPackageHandler(); _stream = encrHandler.DecryptPackage(template, Encryption); encrHandler = null; //throw (new NotImplementedException("No support for Encrypted packages in this version")); } else { byte[] b = System.IO.File.ReadAllBytes(template.FullName); _stream.Write(b, 0, b.Length); } try { _package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); } catch (Exception ex) { if (password == null && EncryptedPackageHandler.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 (ex); } } } else throw new Exception("Passed invalid TemplatePath to Excel Template"); //return newFile; }
private void ConstructNewFile(Stream stream, string password) { _stream = stream; if (File != null) File.Refresh(); if (File != null && File.Exists) { if (password != null) { var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; Encryption.Password = password; _stream = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; } else { ReadFile(); } try { _package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite); } catch (Exception ex) { if (password == null && EncryptedPackageHandler.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 (ex); } } } else { _package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); 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) { #if !Core this._stream.Close(); #endif 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; }