/// <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 IExcel 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 IExcel 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)); } if (TryGetEncryptedPackage(fileStream, document, configuration.Password, out stream)) { return(new ExcelOpenXmlReader(stream)); } throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound); } if (XlsWorkbook.IsRawBiffStream(probe)) { return(new ExcelBinaryReader(fileStream, configuration.Password, configuration.FallbackEncoding)); } if (probe[0] == 0x50 && probe[1] == 0x4B) { // zip files start with 'PK' return(new ExcelOpenXmlReader(fileStream)); } 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 IExcel 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); } }
/// <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 IExcel 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)); } throw new ExcelReaderException(Errors.ErrorCompoundNoOpenXml); } if (probe[0] == 0x50 && probe[1] == 0x4B) { // Zip files start with 'PK' return(new ExcelOpenXmlReader(fileStream)); } throw new HeaderException(Errors.ErrorHeaderSignature); }