示例#1
0
        public static WorkBook Load(string excelFilePath, string password)
        {
            FileInfo excelFile = new FileInfo(excelFilePath);

            ExceptionHelper.FalseThrow(excelFile.Exists, "文件不存在!");
            excelFile.Refresh();

            if (!string.IsNullOrEmpty(password))
            {
                EncryptedPackageHandler encrHandler    = new EncryptedPackageHandler();
                ExcelEncryption         fileEncryption = new ExcelEncryption();
                fileEncryption.IsEncrypted = true;
                fileEncryption.Password    = password;
                using (MemoryStream excelStream = encrHandler.DecryptPackage(excelFile, fileEncryption))
                {
                    return(Load(excelStream));
                }
            }
            else
            {
                using (FileStream stream = excelFile.OpenRead())
                {
                    return(Load(stream));
                }
            }
        }
示例#2
0
        /// <summary>
        /// 保存工作表为指定文件设置密码
        /// </summary>
        /// <param name="targetFilePath"></param>
        /// <param name="password"></param>
        public void Save(string targetFilePath, string password)
        {
            FileInfo targetFile = new FileInfo(targetFilePath);

            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            ExcelEncryption fileEncryption = new ExcelEncryption();

            fileEncryption.Password = password;

            if (fileEncryption.IsEncrypted)
            {
                using (FileStream fi = new FileStream(targetFile.FullName, FileMode.Create))
                {
                    byte[] file = this.SaveAsBytes();
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    using (MemoryStream ms = eph.EncryptPackage(file, fileEncryption))
                    {
                        fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    }
                }
            }
            else
            {
                using (Package package = Package.Open(targetFile.FullName, FileMode.Create, FileAccess.ReadWrite))
                {
                    InnerSave(package);
                }
            }
        }
示例#3
0
        internal byte[] GetAsByteArray(bool save)
        {
            if (save)
            {
                Workbook.Save();
            }
            _package.Close();

            Byte[] byRet = new byte[Stream.Length];
            long   pos   = Stream.Position;

            Stream.Seek(0, SeekOrigin.Begin);
            Stream.Read(byRet, 0, (int)Stream.Length);

            //Encrypt Workbook?
            if (Encryption.IsEncrypted)
            {
                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                var ms = eph.EncryptPackage(byRet, Encryption);
                byRet = ms.ToArray();
            }

            Stream.Seek(pos, SeekOrigin.Begin);
            Stream.Close();
            return(byRet);
        }
示例#4
0
 /// <summary>
 /// Sets a password for the workbook. This does not encrypt the workbook.
 /// </summary>
 /// <param name="Password">The password. </param>
 public void SetPassword(string Password)
 {
     if (string.IsNullOrEmpty(Password))
     {
         DeleteNode(workbookPasswordPath);
     }
     else
     {
         SetXmlNodeString(workbookPasswordPath, ((int)EncryptedPackageHandler.CalculatePasswordHash(Password)).ToString("x"));
     }
 }
        //todo:
        /// <summary>
        /// 加密类型
        /// </summary>
        // public bool revisionsAlgorithmName

        /// <summary>
        /// 设置工作簿的密码。这不加密的工作簿。
        /// </summary>
        /// <param name="Password">密码 </param>
        public void SetPassword(string Password)
        {
            if (string.IsNullOrEmpty(Password))
            {
                base.Attributes.Remove("workbookPassword");
            }
            else
            {
                base.SetAttribute("workbookPassword", ((int)EncryptedPackageHandler.CalculatePasswordHash(Password)).ToString("x"));
            }
        }
示例#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;
            }

            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;
        }
示例#7
0
        /*public string Password
         * {
         *      get
         *      {
         *              return base.GetAttribute("password");
         *      }
         *      set
         *      {
         *              base.SetAttribute("password", value);
         *      }
         * }*/

        public void SetPassword(string password)
        {
            if (this.IsProtected == false)
            {
                this.IsProtected = true;
            }

            password = password.Trim();
            if (string.IsNullOrEmpty(password))
            {
                base.Attributes.Remove("password");
                return;
            }
            int hash = EncryptedPackageHandler.CalculatePasswordHash(password);

            base.SetAttribute("password", ((int)hash).ToString("x"));
        }
示例#8
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;
 }
示例#9
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();
     }
 }
示例#10
0
        /// <summary>
        /// 加密后Excel文件
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public byte[] SaveAsBytes(string password)
        {
            byte[]          byRet          = null;
            ExcelEncryption fileEncryption = new ExcelEncryption();

            fileEncryption.Password = password;
            if (fileEncryption.IsEncrypted)
            {
                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                using (MemoryStream encryptStream = eph.EncryptPackage(SaveAsBytes(), fileEncryption))
                {
                    byRet = encryptStream.ToArray();
                }
            }
            else
            {
                byRet = SaveAsBytes();
            }

            return(byRet);
        }
示例#11
0
 public static WorkBook Load(Stream input, string password)
 {
     if (!string.IsNullOrEmpty(password))
     {
         EncryptedPackageHandler encrHandler    = new EncryptedPackageHandler();
         ExcelEncryption         fileEncryption = new ExcelEncryption();
         fileEncryption.IsEncrypted = true;
         fileEncryption.Password    = password;
         using (MemoryStream copyStream = new MemoryStream())
         {
             input.CopyTo(copyStream);
             //ExcelHelper.CopyStream(input, copyStream);
             MemoryStream decrytStrem = encrHandler.DecryptPackage(copyStream, fileEncryption);
             return(Load(decrytStrem));
         }
     }
     else
     {
         return(null);
     }
 }
示例#12
0
        public void Save(Stream targetStream, string password)
        {
            ExcelEncryption fileEncryption = new ExcelEncryption();

            fileEncryption.Password = password;

            if (fileEncryption.IsEncrypted)
            {
                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                using (MemoryStream encryptStream = eph.EncryptPackage(SaveAsBytes(), fileEncryption))
                {
                    encryptStream.Seek(0, SeekOrigin.Begin);
                    encryptStream.CopyTo(targetStream);
                    //ExcelHelper.CopyStream(encryptStream, targetStream);
                }
            }
            else
            {
                Save(targetStream);
            }
        }
示例#13
0
        /// <summary>
        /// Copies the Package to the Outstream
        /// The package is closed after it has been saved
        /// </summary>
        /// <param name="OutputStream">The stream to copy the package to</param>
        public void SaveAs(Stream OutputStream)
        {
            File = null;
            Save();

            if (Encryption.IsEncrypted)
            {
                //Encrypt Workbook
                Byte[] file = new byte[Stream.Length];
                long   pos  = Stream.Position;
                Stream.Seek(0, SeekOrigin.Begin);
                Stream.Read(file, 0, (int)Stream.Length);

                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                var ms = eph.EncryptPackage(file, Encryption);
                CopyStream(ms, ref OutputStream);
            }
            else
            {
                CopyStream(_stream, ref OutputStream);
            }
        }
示例#14
0
        /// <summary>
        /// Sets a password for the sheet.
        /// </summary>
        /// <param name="Password"></param>
        public void SetPassword(string Password)
        {
            if (IsProtected == false)
            {
                IsProtected = true;
            }

            Password = Password.Trim();
            if (Password == "")
            {
                var node = TopNode.SelectSingleNode(_passwordPath, NameSpaceManager);
                if (node != null)
                {
                    (node as XmlAttribute).OwnerElement.Attributes.Remove(node as XmlAttribute);
                }
                return;
            }

            int hash = EncryptedPackageHandler.CalculatePasswordHash(Password);

            SetXmlNodeString(_passwordPath, ((int)hash).ToString("x"));
        }
示例#15
0
        /// <summary>
        /// Copies the Package to the Outstream
        /// The package is closed after it has been saved
        /// </summary>
        /// <param name="OutputStream">The stream to copy the package to</param>
        public void SaveAs(Stream OutputStream)
        {
            File = null;
            Save();

            if (Encryption.IsEncrypted)
            {
                //Encrypt Workbook
                Byte[] file = new byte[Stream.Length];
                long pos = Stream.Position;
                Stream.Seek(0, SeekOrigin.Begin);
                Stream.Read(file, 0, (int)Stream.Length);

                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                var ms = eph.EncryptPackage(file, Encryption);
                CopyStream(ms, ref OutputStream);
            }
            else
            {
                CopyStream(_stream, ref OutputStream);
            }
        }
示例#16
0
        /// <summary>
        /// Saves all the components back into the package.
        /// This method recursively calls the Save method on all sub-components.
        /// We close the package after the save is done.
        /// </summary>
        public void Save()
        {
            try
            {
                Workbook.Save();
                if (File == null)
                {
                    _stream = _package.Save();
                    _package.Close();
                }
                else
                {
                    if (System.IO.File.Exists(File.FullName))
                    {
                        try
                        {
                            System.IO.File.Delete(File.FullName);
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex));
                        }
                    }
                    if (Stream is MemoryStream)
                    {
                        _package.Close();
                        _stream = _package.Save();
                        var fi = new FileStream(File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (Encryption.IsEncrypted)
                        {
                            byte[] file = ((MemoryStream)Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
                        }
                        else
                        {
                            fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
                        }
                        fi.Close();
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(File.FullName, GetAsByteArray(false));
                    }
                }
            }
            catch (Exception ex)
            {
                if (File == null)
                {
                    throw;
                }
                else
                {
                    throw (new InvalidOperationException(string.Format("Error saving file {0}", File.FullName), ex));
                }
            }
        }
示例#17
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);
                _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;
                }
            }

            //Clear the workbook so that it gets reinitialized next time
            this._workbook = null;
        }
示例#18
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);
             _package = new Packaging.ZipPackage(_stream);
         }
         catch (Exception ex)
         {
             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;
             }
         }
     }
     else
         throw new Exception("Passed invalid TemplatePath to Excel Template");
     //return newFile;
 }
示例#19
0
        /// <summary>
        /// Saves all the components back into the package.
        /// This method recursively calls the Save method on all sub-components.
        /// We close the package after the save is done.
        /// </summary>
        public void Save()
        {
            try
            {
                Workbook.Save();
                if (File == null)
                {
                    _package.Close();
                }
                else
                {
                    if (System.IO.File.Exists(File.FullName))
                    {
                        try
                        {
                            System.IO.File.Delete(File.FullName);
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex));
                        }
                    }
                    if (Stream is MemoryStream)
                    {
                        _package.Close();
                        var fi = new FileStream(File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (Encryption.IsEncrypted)
                        {
                            byte[] file = ((MemoryStream)Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
                            ms.Close();
                        }
                        else
                        {
                            fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
                            Stream.Close();
                        }
                        fi.Close();
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(File.FullName, GetAsByteArray(false));
                    }
                }
            }
            catch (Exception ex)
            {
                if (File == null)
                {
                    throw (ex);
                }
                else
                {
                    throw (new InvalidOperationException(string.Format("Error saving file {0}", File.FullName), ex));
                }
            }
        }
示例#20
0
        /// <summary>
        /// Copies the Package to the Outstream
        /// The package is closed after it has been saved
        /// </summary>
        /// <param name="OutputStream">The stream to copy the package to</param>
        public void SaveAs(Stream OutputStream)
        {
            File = null;
            Save();

            if (OutputStream != _stream)
            {
                if (Encryption.IsEncrypted)
                {
#if !MONO
                    //Encrypt Workbook
                    Byte[] file = new byte[Stream.Length];
                    long pos = Stream.Position;
                    Stream.Seek(0, SeekOrigin.Begin);
                    Stream.Read(file, 0, (int) Stream.Length);
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    var ms = eph.EncryptPackage(file, Encryption);
                    CopyStream(ms, ref OutputStream);
#endif
#if MONO
                throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                }
                else
                {
                    CopyStream(_stream, ref OutputStream);
                }
            }
        }
示例#21
0
        /// <summary>
        /// Saves all the components back into the package.
        /// This method recursively calls the Save method on all sub-components.
        /// We close the package after the save is done.
        /// </summary>
        public void Save()
        {
            try
            {
                Workbook.Save();
                if (File == null)
                {
                    if(Encryption.IsEncrypted)
                    {
#if !MONO
                        var ms = new MemoryStream();
                        _package.Save(ms);
                        byte[] file = ms.ToArray();
                        EncryptedPackageHandler eph = new EncryptedPackageHandler();
                        var msEnc = eph.EncryptPackage(file, Encryption);
                        CopyStream(msEnc, ref _stream);
#endif
#if MONO
                        throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                    }
                    else
                    {
                        _package.Save(_stream);
                    }
                    _stream.Flush();
                    _package.Close();
                }
                else
                {
                    if (System.IO.File.Exists(File.FullName))
                    {
                        try
                        {
                            System.IO.File.Delete(File.FullName);
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex));
                        }
                    }

                    _package.Save(_stream);
                    _package.Close();
                    if (Stream is MemoryStream)
                    {
                        var fi = new FileStream(File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (Encryption.IsEncrypted)
                        {
#if !MONO
                            byte[] file = ((MemoryStream)Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
#endif
#if MONO
                            throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                        }
                        else
                        {                            
                            fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
                        }
                        fi.Close();
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(File.FullName, GetAsByteArray(false));
                    }
                }
            }
            catch (Exception ex)
            {
                if (File == null)
                {
                    throw;
                }
                else
                {
                    throw (new InvalidOperationException(string.Format("Error saving file {0}", File.FullName), ex));
                }
            }
        }
示例#22
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)
                {
#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();
            }
        }
示例#23
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)
                {
#if !MONO
                    Stream encrStream = new MemoryStream();
                    CopyStream(input, ref encrStream);
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    Encryption.Password = Password;
                    ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption);
#endif
#if MONO
                    throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                }
                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)
                {
#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;
        }
示例#24
0
        internal byte[] GetAsByteArray(bool save)
        {
            if (save)
            {
                Workbook.Save();
                _package.Close();
                _stream = _package.Save();
            }
            Byte[] byRet = new byte[Stream.Length];
            long pos = Stream.Position;
            Stream.Seek(0, SeekOrigin.Begin);
            Stream.Read(byRet, 0, (int)Stream.Length);

            //Encrypt Workbook?
            if (Encryption.IsEncrypted)
            {
                EncryptedPackageHandler eph=new EncryptedPackageHandler();
                var ms = eph.EncryptPackage(byRet, Encryption);
                byRet = ms.ToArray();
            }

            Stream.Seek(pos, SeekOrigin.Begin);
            Stream.Close();
            return byRet;
        }
示例#25
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);
             _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;
            }
         }
     }
     else
     {
         //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite);
         _package = new Packaging.ZipPackage(_stream);
         CreateBlankWb();
     }
 }
示例#26
0
        /// <summary>
        /// Saves all the components back into the package.
        /// This method recursively calls the Save method on all sub-components.
        /// We close the package after the save is done.
        /// </summary>
        public void Save()
        {
            try
            {
                if (_stream is MemoryStream && _stream.Length > 0)
                {
                    //Close any open memorystream and "renew" then. This can occure if the package is saved twice.
                    //The stream is left open on save to enable the user to read the stream-property.
                    //Non-memorystream streams will leave the closing to the user before saving a second time.
                    CloseStream();
                }

                Workbook.Save();
                if (File == null)
                {
                    if(Encryption.IsEncrypted)
                    {
            #if !MONO
                        var ms = new MemoryStream();
                        _package.Save(ms);
                        byte[] file = ms.ToArray();
                        EncryptedPackageHandler eph = new EncryptedPackageHandler();
                        var msEnc = eph.EncryptPackage(file, Encryption);
                        CopyStream(msEnc, ref _stream);
            #endif
            #if MONO
                        throw new NotSupportedException("Encryption is not supported under Mono.");
            #endif
                    }
                    else
                    {
                        _package.Save(_stream);
                    }
                    _stream.Flush();
                    _package.Close();
                }
                else
                {
                    if (System.IO.File.Exists(File.FullName))
                    {
                        try
                        {
                            System.IO.File.Delete(File.FullName);
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex));
                        }
                    }

                    _package.Save(_stream);
                    _package.Close();
                    if (Stream is MemoryStream)
                    {
                        var fi = new FileStream(File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (Encryption.IsEncrypted)
                        {
            #if !MONO
                            byte[] file = ((MemoryStream)Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
            #endif
            #if MONO
                            throw new NotSupportedException("Encryption is not supported under Mono.");
            #endif
                        }
                        else
                        {
                            fi.Write(((MemoryStream)Stream).GetBuffer(), 0, (int)Stream.Length);
                        }
                        fi.Close();
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(File.FullName, GetAsByteArray(false));
                    }
                }
            }
            catch (Exception ex)
            {
                if (File == null)
                {
                    throw;
                }
                else
                {
                    throw (new InvalidOperationException(string.Format("Error saving file {0}", File.FullName), ex));
                }
            }
        }