Ejemplo n.º 1
0
 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();
     }
 }
Ejemplo n.º 2
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;
            }

            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;
        }
Ejemplo n.º 3
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)
     {
         _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;
 }