@@ -24,8 +24,6 @@ namespace FileHelpers.ExcelNPOIStorage
24
24
public sealed class ExcelNPOIStorage : ExcelStorageBase
25
25
#pragma warning restore 618
26
26
{
27
- //private readonly Missing mv = Missing.Value;
28
-
29
27
#region " Constructors "
30
28
31
29
/// <summary>Create a new ExcelStorage to work with the specified type</summary>
@@ -85,7 +83,7 @@ private void CloseAndCleanUp()
85
83
86
84
#endregion
87
85
88
- #region " OpenWorkbook "
86
+ #region " OpenWorkbookFromStream "
89
87
90
88
private void OpenWorkbook ( string filename )
91
89
{
@@ -95,17 +93,19 @@ private void OpenWorkbook(string filename)
95
93
96
94
using ( FileStream file = new FileStream ( filename , FileMode . Open , FileAccess . Read ) )
97
95
{
98
- var extension = Path . GetExtension ( filename ) ;
99
- OpenWorkbook ( file , extension ) ;
96
+ OpenWorkbookFromStream ( file ) ;
100
97
}
101
98
}
102
99
103
- private void OpenWorkbook ( Stream stream , string knownFileExtension = null )
100
+ private void OpenWorkbookFromStream ( Stream stream )
104
101
{
105
102
mWorkbook = WorkbookFactory . Create ( stream ) ;
103
+ mWorkbook . MissingCellPolicy = MissingCellPolicy . CREATE_NULL_AS_BLANK ;
106
104
107
- if ( String . IsNullOrEmpty ( SheetName ) )
105
+ if ( string . IsNullOrEmpty ( SheetName ) )
106
+ {
108
107
mSheet = mWorkbook . GetSheetAt ( mWorkbook . ActiveSheetIndex ) ;
108
+ }
109
109
else
110
110
{
111
111
try
@@ -150,7 +150,7 @@ private void CreateWorkbook(string filename)
150
150
mWorkbook = new XSSFWorkbook ( ) ;
151
151
else if ( extension . ToLowerInvariant ( ) == ".xls" )
152
152
mWorkbook = new HSSFWorkbook ( ) ;
153
- mSheet = mSheet = String . IsNullOrEmpty ( SheetName ) ? mWorkbook . CreateSheet ( ) : mWorkbook . CreateSheet ( SheetName ) ;
153
+ mSheet = mSheet = string . IsNullOrEmpty ( SheetName ) ? mWorkbook . CreateSheet ( ) : mWorkbook . CreateSheet ( SheetName ) ;
154
154
mWorkbook . SetActiveSheet ( 0 ) ;
155
155
}
156
156
@@ -336,12 +336,12 @@ public override void InsertRecords(object[] records)
336
336
if ( OverrideFile && File . Exists ( FileName ) )
337
337
File . Delete ( FileName ) ;
338
338
339
- if ( ! String . IsNullOrEmpty ( TemplateFile ) )
339
+ if ( ! string . IsNullOrEmpty ( TemplateFile ) )
340
340
{
341
341
if ( File . Exists ( TemplateFile ) == false )
342
342
throw new ExcelBadUsageException ( string . Concat ( "Template file not found: '" , TemplateFile , "'" ) ) ;
343
343
344
- if ( String . Compare ( TemplateFile , FileName , StringComparison . OrdinalIgnoreCase ) != 0 )
344
+ if ( string . Compare ( TemplateFile , FileName , StringComparison . OrdinalIgnoreCase ) != 0 )
345
345
File . Copy ( TemplateFile , FileName , true ) ;
346
346
}
347
347
@@ -359,10 +359,6 @@ public override void InsertRecords(object[] records)
359
359
360
360
SaveWorkbook ( FileName ) ;
361
361
}
362
- catch
363
- {
364
- throw ;
365
- }
366
362
finally
367
363
{
368
364
CloseAndCleanUp ( ) ;
@@ -378,7 +374,7 @@ public override void InsertRecords(object[] records)
378
374
/// <returns>The extracted records.</returns>
379
375
public override object [ ] ExtractRecords ( )
380
376
{
381
- if ( String . IsNullOrEmpty ( FileName ) )
377
+ if ( string . IsNullOrEmpty ( FileName ) )
382
378
throw new ExcelBadUsageException ( "You need to specify the WorkBookFile of the ExcelDataLink." ) ;
383
379
384
380
return TryGetRecordsFromWorkbook ( ( ) => OpenWorkbook ( FileName ) ) ;
@@ -393,7 +389,7 @@ public object[] ExtractRecords(Stream stream)
393
389
throw new ArgumentNullException ( "stream" ) ;
394
390
}
395
391
396
- return TryGetRecordsFromWorkbook ( ( ) => OpenWorkbook ( stream ) ) ;
392
+ return TryGetRecordsFromWorkbook ( ( ) => OpenWorkbookFromStream ( stream ) ) ;
397
393
}
398
394
399
395
private object [ ] TryGetRecordsFromWorkbook ( Action workbookOpenerProvider )
@@ -448,10 +444,6 @@ private object[] TryGetRecordsFromWorkbook(Action workbookOpenerProvider)
448
444
}
449
445
}
450
446
}
451
- catch
452
- {
453
- throw ;
454
- }
455
447
finally
456
448
{
457
449
CloseAndCleanUp ( ) ;
@@ -476,7 +468,7 @@ private static string ColumnsToValues(object[] values)
476
468
for ( int i = 1 ; i < values . Length ; i ++ )
477
469
{
478
470
res += "," + ( values [ i ] == null
479
- ? String . Empty
471
+ ? string . Empty
480
472
: values [ i ] . ToString ( ) ) ;
481
473
}
482
474
@@ -485,7 +477,7 @@ private static string ColumnsToValues(object[] values)
485
477
486
478
private class CellExtractor : ICellHandler
487
479
{
488
- private List < object > _cells ;
480
+ private readonly List < object > _cells ;
489
481
490
482
/// <summary>
491
483
/// Initializes a new instance of the CellExtractor class.
@@ -512,7 +504,7 @@ public void OnCell(ICell cell, ICellWalkContext ctx)
512
504
513
505
private class CellInserter : ICellHandler
514
506
{
515
- private List < object > _cells = null ;
507
+ private readonly List < object > _cells ;
516
508
private List < object > . Enumerator _valuesEnumerator ;
517
509
518
510
/// <summary>
0 commit comments