Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
0
        public ExcelBinaryReader(Stream stream, ExcelReaderConfiguration configuration)
            : base(configuration)
        {
            Workbook = new XlsWorkbook(stream, Configuration.Password, Configuration.FallbackEncoding);

            // By default, the data reader is positioned on the first result.
            Reset();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates an instance of ExcelCsvReader
        /// </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 CreateCsvReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
        {
            if (configuration == null)
            {
                configuration = new ExcelReaderConfiguration();
            }

            return(new ExcelCsvReader(fileStream, configuration.FallbackEncoding, configuration.AutodetectSeparators));
        }
Exemplo n.º 5
0
        public ExcelOpenXmlReader(Stream stream, ExcelReaderConfiguration configuration)
            : base(configuration)
        {
            Document = new ZipWorker(stream);
            Workbook = new XlsxWorkbook(Document);

            // By default, the data reader is positioned on the first result.
            Reset();
        }
        /// <summary>
        /// Creates an instance of ExcelCsvReader
        /// </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 CreateCsvReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
        {
            if (configuration == null)
            {
                configuration = new ExcelReaderConfiguration();
            }

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

            return(new ExcelCsvReader(fileStream, configuration.FallbackEncoding, configuration.AutodetectSeparators, configuration.AnalyzeInitialCsvRows));
        }
        protected ExcelDataReader(ExcelReaderConfiguration configuration)
        {
            if (configuration == null)
            {
                // Use the defaults
                configuration = new ExcelReaderConfiguration();
            }

            // Copy the configuration to prevent external changes
            Configuration = new ExcelReaderConfiguration()
            {
                FallbackEncoding = configuration.FallbackEncoding
            };
        }
        /// <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);
            }
        }
Exemplo n.º 9
0
        /// <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)
        {
            var probe = new byte[8];

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

            if (XlsWorkbook.IsCompoundDocument(probe) || XlsWorkbook.IsRawBiffStream(probe))
            {
                return(CreateBinaryReader(fileStream, configuration));
            }

            // zip files start with 'PK'
            if (probe[0] == 0x50 && probe[1] == 0x4B)
            {
                return(CreateOpenXmlReader(fileStream, configuration));
            }

            throw new NotSupportedException("Unknown file format");
        }
Exemplo n.º 10
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)
 {
     return(new ExcelOpenXmlReader(fileStream, configuration));
 }