예제 #1
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();
            }
        }
        /// <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;
        }
예제 #3
0
        private void ConstructNewFile(string password)
        {
            var ms = new MemoryStream();

            _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
                {
                    byte[] b = 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 (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;
                    }
                }
            }
            else
            {
                //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite);
                _package = new Packaging.ZipPackage(ms);
                CreateBlankWb();
            }
        }
예제 #4
0
        /// <summary>
        /// Read the package from the OLE document and decrypt it using the supplied password
        /// </summary>
        /// <param name="fi">The file</param>
        /// <param name="encryption"></param>
        /// <returns></returns>
        internal MemoryStream DecryptPackage(FileInfo fi, ExcelEncryption encryption)
        {
            CompoundDocument doc = new CompoundDocument(fi);

            MemoryStream ret = null;

            if (CompoundDocument.IsStorageFile(fi.FullName) == 0)
            {
                ret = GetStreamFromPackage(doc, encryption);
            }
            else
            {
                throw (new InvalidDataException(string.Format("File {0} is not an encrypted package", fi.FullName)));
            }
            return(ret);
        }