/// <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;
        }
예제 #2
0
 internal ZipPackagePart(ZipPackage package, ZipEntry entry)
 {
     Package = package;
     Entry = entry;
     SaveHandler = null;
     Uri = new Uri(package.GetUriKey(entry.FileName), UriKind.Relative);
 }
예제 #3
0
 /// <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();
     }
 }
예제 #4
0
        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();
            }
        }
예제 #5
0
 /// <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();
     }
 }
예제 #6
0
        /// <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;
        }
예제 #7
0
 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;
 }
예제 #8
0
 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;
 }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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;
        }
예제 #13
0
        /// <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;
        }
예제 #14
0
        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();
            }
        }
예제 #15
0
 /// <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;
 }
예제 #16
0
 private void ReleaseResources()
 {
     //Release some resources:
     if (_package != null)
     {
         _package.Close();
         _package = null;
     }
     if (_stream != null)
     {
         _stream.Close();
         _stream.Dispose();
         _stream = null;
     }
     _isExternalStream = true;
 }
예제 #17
0
 /// <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();
            }
        }
예제 #19
0
        /// <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();
            }
        }
예제 #20
0
 /// <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();
     }
 }
예제 #21
0
        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;
        }
예제 #22
0
        internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri)
        {
            var id = 1;

            return(GetNewUri(package, sUri, ref id));
        }
예제 #23
0
 internal static Uri GetNewUri(Packaging.ZipPackage package, string sUri)
 {
     return(GetNewUri(package, sUri, 1));
 }