-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathExcelReaderFactory.cs
More file actions
235 lines (200 loc) · 9.32 KB
/
Copy pathExcelReaderFactory.cs
File metadata and controls
235 lines (200 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System.Diagnostics.CodeAnalysis;
using ExcelDataReader.Core.BinaryFormat;
using ExcelDataReader.Core.CompoundFormat;
using ExcelDataReader.Core.OfficeCrypto;
using ExcelDataReader.Core.XmlSpreadsheetFormat;
using ExcelDataReader.Exceptions;
using ExcelDataReader.Misc;
namespace ExcelDataReader;
/// <summary>
/// The ExcelReader Factory.
/// </summary>
public static class ExcelReaderFactory
{
private const string DirectoryEntryWorkbook = "Workbook";
private const string DirectoryEntryBook = "Book";
private const string DirectoryEntryEncryptedPackage = "EncryptedPackage";
private const string DirectoryEntryEncryptionInfo = "EncryptionInfo";
/// <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)
{
configuration ??= new ExcelReaderConfiguration();
fileStream = PrepareInputStream(fileStream, configuration);
var probe = new byte[8];
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.ReadAtLeast(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, configuration.SinglePassMode);
}
if (TryGetEncryptedPackage(fileStream, document, configuration.Password, out stream))
{
return new ExcelOpenXmlReader(stream, configuration.SinglePassMode);
}
throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound);
}
if (XlsWorkbook.IsRawBiffStream(probe))
{
return new ExcelBinaryReader(fileStream, configuration.Password, configuration.FallbackEncoding, configuration.SinglePassMode);
}
if (probe[0] == 0x50 && probe[1] == 0x4B)
{
// zip files start with 'PK'
return new ExcelOpenXmlReader(fileStream, configuration.SinglePassMode);
}
if (SpreadsheetXmlWorkbook.IsSpreadsheetXmlStream(fileStream))
{
return new ExcelSpreadsheetXmlReader(fileStream, configuration.SinglePassMode);
}
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)
{
configuration ??= new ExcelReaderConfiguration();
fileStream = PrepareInputStream(fileStream, configuration);
var probe = new byte[8];
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.ReadAtLeast(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, configuration.SinglePassMode);
}
else
{
throw new ExcelReaderException(Errors.ErrorStreamWorkbookNotFound);
}
}
else if (XlsWorkbook.IsRawBiffStream(probe))
{
return new ExcelBinaryReader(fileStream, configuration.Password, configuration.FallbackEncoding, configuration.SinglePassMode);
}
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)
{
configuration ??= new ExcelReaderConfiguration();
fileStream = PrepareInputStream(fileStream, configuration);
var probe = new byte[8];
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.ReadAtLeast(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.SinglePassMode);
}
throw new ExcelReaderException(Errors.ErrorCompoundNoOpenXml);
}
if (probe[0] == 0x50 && probe[1] == 0x4B)
{
// Zip files start with 'PK'
return new ExcelOpenXmlReader(fileStream, configuration.SinglePassMode);
}
throw new HeaderException(Errors.ErrorHeaderSignature);
}
/// <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)
{
configuration ??= new ExcelReaderConfiguration();
fileStream = PrepareInputStream(fileStream, configuration);
return new ExcelCsvReader(fileStream, configuration.FallbackEncoding, configuration.AutodetectSeparators, configuration.AnalyzeInitialCsvRows, configuration.QuoteChar, configuration.TrimWhiteSpace, configuration.EscapeChar);
}
private static bool TryGetWorkbook(Stream fileStream, CompoundDocument document, [NotNullWhen(true)] out Stream? stream)
{
var workbookEntry = document.FindEntry(DirectoryEntryWorkbook, DirectoryEntryBook);
if (workbookEntry != null)
{
if (workbookEntry.EntryType != STGTY.STGTY_STREAM)
{
throw new ExcelReaderException(Errors.ErrorWorkbookIsNotStream);
}
stream = new CompoundStream(document, fileStream, workbookEntry.StreamFirstSector, (int)workbookEntry.StreamSize, workbookEntry.IsEntryMiniStream, false);
return true;
}
stream = null;
return false;
}
private static bool TryGetEncryptedPackage(Stream fileStream, CompoundDocument document, string? password, [NotNullWhen(true)] out Stream? stream)
{
var encryptedPackage = document.FindEntry(DirectoryEntryEncryptedPackage);
var encryptionInfo = document.FindEntry(DirectoryEntryEncryptionInfo);
if (encryptedPackage == null || encryptionInfo == null)
{
stream = null;
return false;
}
var infoBytes = document.ReadStream(fileStream, encryptionInfo.StreamFirstSector, (int)encryptionInfo.StreamSize, encryptionInfo.IsEntryMiniStream);
var encryption = EncryptionInfo.Create(infoBytes);
if (encryption.VerifyPassword("VelvetSweatshop"))
{
// Magic password used for write-protected workbooks
password = "VelvetSweatshop";
}
else if (password == null || !encryption.VerifyPassword(password))
{
throw new InvalidPasswordException(Errors.ErrorInvalidPassword);
}
var secretKey = encryption.GenerateSecretKey(password);
var packageStream = new CompoundStream(document, fileStream, encryptedPackage.StreamFirstSector, (int)encryptedPackage.StreamSize, encryptedPackage.IsEntryMiniStream, false);
stream = encryption.CreateEncryptedPackageStream(packageStream, secretKey);
return true;
}
private static Stream GetSeekableStream(Stream fileStream)
{
if (fileStream.CanSeek)
return fileStream;
using (fileStream)
{
// LeaveOpen semantics are handled by wrapping the caller stream before this call.
var seekableStream = new MemoryStream();
fileStream.CopyTo(seekableStream);
seekableStream.Seek(0, SeekOrigin.Begin);
return seekableStream;
}
}
private static Stream PrepareInputStream(Stream fileStream, ExcelReaderConfiguration configuration)
{
if (configuration.LeaveOpen)
{
fileStream = new LeaveOpenStream(fileStream);
}
return GetSeekableStream(fileStream);
}
}