Пример #1
0
        /// <summary>
        /// Creates an instance of <see cref="ExcelBinaryReader"/>
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="configuration">The configuration object.</param>
        /// <returns>The excel data reader.</returns>
        public static IExcelDataReader CreateBinaryReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
        {
            var probe = new byte[8];

            fileStream.Seek(0, SeekOrigin.Begin);
            fileStream.Read(probe, 0, probe.Length);
            fileStream.Seek(0, SeekOrigin.Begin);

            if (CompoundDocument.IsCompoundDocument(probe))
            {
                var document = new CompoundDocument(fileStream);
                if (TryGetWorkbook(fileStream, document, out var stream))
                {
                    return(new ExcelBinaryReader(stream, configuration));
                }
                else
                {
                    throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound);
                }
            }
            else if (XlsWorkbook.IsRawBiffStream(probe))
            {
                return(new ExcelBinaryReader(fileStream, configuration));
            }
            else
            {
                throw new HeaderException(Errors.ErrorHeaderSignature);
            }
        }
Пример #2
0
        /// <summary>
        /// Creates an instance of <see cref="ExcelOpenXmlReader"/>
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="configuration">The reader configuration -or- <see langword="null"/> to use the default configuration.</param>
        /// <returns>The excel data reader.</returns>
        public static IExcelDataReader CreateOpenXmlReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
        {
            var probe = new byte[8];

            fileStream.Seek(0, SeekOrigin.Begin);
            fileStream.Read(probe, 0, probe.Length);
            fileStream.Seek(0, SeekOrigin.Begin);

            // Probe for password protected compound document or zip file
            if (CompoundDocument.IsCompoundDocument(probe))
            {
                var document = new CompoundDocument(fileStream);
                if (TryGetEncryptedPackage(fileStream, document, configuration?.Password, out var stream))
                {
                    return(new ExcelOpenXmlReader(stream, configuration));
                }
                else
                {
                    throw new ExcelReaderException(Errors.ErrorCompoundNoOpenXml);
                }
            }
            else if (probe[0] == 0x50 && probe[1] == 0x4B)
            {
                // Zip files start with 'PK'
                return(new ExcelOpenXmlReader(fileStream, configuration));
            }
            else
            {
                throw new HeaderException(Errors.ErrorHeaderSignature);
            }
        }
Пример #3
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;
        }
Пример #4
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>
        /// Creates an instance of <see cref="ExcelBinaryReader"/> or <see cref="ExcelOpenXmlReader"/>
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="configuration">The configuration object.</param>
        /// <returns>The excel data reader.</returns>
        public static IExcelDataReader CreateReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
        {
            if (configuration == null)
            {
                configuration = new ExcelReaderConfiguration();
            }

            if (configuration.LeaveOpen)
            {
                fileStream = new LeaveOpenStream(fileStream);
            }

            var probe = new byte[8];

            fileStream.Seek(0, SeekOrigin.Begin);
            fileStream.Read(probe, 0, probe.Length);
            fileStream.Seek(0, SeekOrigin.Begin);

            if (CompoundDocument.IsCompoundDocument(probe))
            {
                // Can be BIFF5-8 or password protected OpenXml
                var document = new CompoundDocument(fileStream);
                if (TryGetWorkbook(fileStream, document, out var stream))
                {
                    return(new ExcelBinaryReader(stream, configuration.Password, configuration.FallbackEncoding));
                }
                else if (TryGetEncryptedPackage(fileStream, document, configuration.Password, out stream))
                {
                    return(new ExcelOpenXmlReader(stream));
                }
                else
                {
                    throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound);
                }
            }
            else if (XlsWorkbook.IsRawBiffStream(probe))
            {
                return(new ExcelBinaryReader(fileStream, configuration.Password, configuration.FallbackEncoding));
            }
            else if (probe[0] == 0x50 && probe[1] == 0x4B)
            {
                // zip files start with 'PK'
                return(new ExcelOpenXmlReader(fileStream));
            }
            else
            {
                throw new HeaderException(Errors.ErrorHeaderSignature);
            }
        }
Пример #6
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)
        {
            if (CompoundDocument.IsCompoundDocument(fi))
            {
                CompoundDocument doc = new CompoundDocument(fi);

                MemoryStream ret = null;
                ret = GetStreamFromPackage(doc, encryption);
                return(ret);
            }
            else
            {
                throw (new InvalidDataException(string.Format("File {0} is not an encrypted package", fi.FullName)));
            }
        }
Пример #7
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 void DecryptPackage(FileInfo fi, ExcelEncryption encryption, Stream outputStream)
        {
            if (CompoundDocument.IsCompoundDocument(fi))
            {
                CompoundDocument doc = new CompoundDocument(fi, this.tempFolder);

                //Stream ret = null;
                GetStreamFromPackage(doc, encryption, outputStream);
            }
            else
            {
                throw (new InvalidDataException(string.Format("File {0} is not an encrypted package", fi.FullName)));
            }
            //return ret;
        }
Пример #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)
     {
         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;
 }
Пример #9
0
 //Helpmethod to output the streams in the storage
 //private void WriteDoc(CompoundDocument.StoragePart storagePart, string p)
 //{
 //    foreach (var store in storagePart.SubStorage)
 //    {
 //        string sdir=p + store.Key.Replace((char)6,'x') + "\\";
 //        Directory.CreateDirectory(sdir);
 //        WriteDoc(store.Value, sdir);
 //    }
 //    foreach (var str in storagePart.DataStreams)
 //    {
 //        File.WriteAllBytes(p + str.Key.Replace((char)6, 'x') + ".bin", str.Value);
 //    }
 //}
 /// <summary>
 /// Read the package from the OLE document and decrypt it using the supplied password
 /// </summary>
 /// <param name="stream">The memory stream. </param>
 /// <param name="encryption">The encryption object from the Package</param>
 /// <returns></returns>
 internal MemoryStream DecryptPackage(MemoryStream stream, ExcelEncryption encryption)
 {
     try
     {
         if (CompoundDocument.IsCompoundDocument(stream))
         {
             var doc = new CompoundDocument(stream);
             return(GetStreamFromPackage(doc, encryption));
         }
         else
         {
             throw (new InvalidDataException("The stream is not an valid/supported encrypted document."));
         }
     }
     catch// (Exception ex)
     {
         throw;
     }
 }
Пример #10
0
 //Helpmethod to output the streams in the storage
 //private void WriteDoc(CompoundDocument.StoragePart storagePart, string p)
 //{
 //    foreach (var store in storagePart.SubStorage)
 //    {
 //        string sdir=p + store.Key.Replace((char)6,'x') + "\\";
 //        Directory.CreateDirectory(sdir);
 //        WriteDoc(store.Value, sdir);
 //    }
 //    foreach (var str in storagePart.DataStreams)
 //    {
 //        File.WriteAllBytes(p + str.Key.Replace((char)6, 'x') + ".bin", str.Value);
 //    }
 //}
 /// <summary>
 /// Read the package from the OLE document and decrypt it using the supplied password
 /// </summary>
 /// <param name="stream">The memory stream. </param>
 /// <param name="encryption">The encryption object from the Package</param>
 /// <returns></returns>
 internal void DecryptPackage(MemoryStream stream, ExcelEncryption encryption, Stream outputStream)
 {
     try
     {
         if (CompoundDocument.IsCompoundDocument(stream))
         {
             var doc = new CompoundDocument(stream, this.tempFolder);
             GetStreamFromPackage(doc, encryption, outputStream);
         }
         else
         {
             throw (new InvalidDataException("The stream is not an valid/supported encrypted document."));
         }
     }
     catch// (Exception ex)
     {
         throw;
     }
 }