/// <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));
        }
        /// <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);
            }
        }
        /// <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)
        {
            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);

            // 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));
                }
                else
                {
                    throw new ExcelReaderException(Errors.ErrorCompoundNoOpenXml);
                }
            }
            else if (probe[0] == 0x50 && probe[1] == 0x4B)
            {
                // Zip files start with 'PK'
                return(new ExcelOpenXmlReader(fileStream));
            }
            else
            {
                throw new HeaderException(Errors.ErrorHeaderSignature);
            }
        }
        /// <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)
        {
            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))
            {
                var document = new CompoundDocument(fileStream);
                if (TryGetWorkbook(fileStream, document, out var stream))
                {
                    return(new ExcelBinaryReader(stream, configuration.Password, configuration.FallbackEncoding));
                }
                else
                {
                    throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound);
                }
            }
            else if (XlsWorkbook.IsRawBiffStream(probe))
            {
                return(new ExcelBinaryReader(fileStream, configuration.Password, configuration.FallbackEncoding));
            }
            else
            {
                throw new HeaderException(Errors.ErrorHeaderSignature);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Saves this icon to the specified stream.
        /// </summary>
        /// <param name="size">The size of the generated icon in pixels.</param>
        /// <param name="stream">The stream to which the icon is written.</param>
        /// <param name="format">The image format of the generated icon.</param>
        public void Save(int size, Stream stream, ExportImageFormat format)
        {
            if (size < 30)
            {
                throw new ArgumentOutOfRangeException("size", size, "The size was too small. Only sizes greater than or equal to 30 pixels are supported.");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (format == ExportImageFormat.Png)
            {
                using (var img = ToBitmap(size))
                {
                    img.Save(stream, ImageFormat.Png);
                }
            }
            else if (format == ExportImageFormat.Emf)
            {
                ToMetafile(stream, size);
            }
            else
            {
                // The LeaveOpenStream wrapper is needed for .NET 4.0 compatibility, since the
                // .NET 4.0 version of StreamWriter does not have an option for keeping the
                // stream open when the writer itself is disposed.
                using (var leaveOpenStream = new LeaveOpenStream(stream))
                {
                    using (var writer = new StreamWriter(leaveOpenStream, Encoding.UTF8))
                    {
                        GenerateSvg(size, writer, format == ExportImageFormat.SvgFragment);
                    }
                }
            }
        }