@@ -56,8 +56,11 @@ public ExcelOpenXmlSheetWriter(Stream stream, object value, string sheetName, IC
5656 this . _stream = stream ;
5757 // Why ZipArchiveMode.Update not ZipArchiveMode.Create?
5858 // R : Mode create - ZipArchiveEntry does not support seeking.'
59- this . _archive = new MiniExcelZipArchive ( _stream , ZipArchiveMode . Update , true , _utf8WithBom ) ;
6059 this . _configuration = configuration as OpenXmlConfiguration ?? OpenXmlConfiguration . DefaultConfig ;
60+ if ( _configuration . FastMode )
61+ this . _archive = new MiniExcelZipArchive ( _stream , ZipArchiveMode . Update , true , _utf8WithBom ) ;
62+ else
63+ this . _archive = new MiniExcelZipArchive ( _stream , ZipArchiveMode . Create , true , _utf8WithBom ) ;
6164 this . _printHeader = printHeader ;
6265 this . _value = value ;
6366 _sheets . Add ( new SheetDto { Name = sheetName , SheetIdx = 1 } ) ; //TODO:remove
@@ -120,7 +123,7 @@ private void CreateSheetXml(object value, string sheetPath)
120123 {
121124 ZipArchiveEntry entry = _archive . CreateEntry ( sheetPath , CompressionLevel . Fastest ) ;
122125 using ( var zipStream = entry . Open ( ) )
123- using ( MiniExcelStreamWriter writer = new MiniExcelStreamWriter ( zipStream , _utf8WithBom , _configuration . BufferSize ) )
126+ using ( MiniExcelStreamWriter writer = new MiniExcelStreamWriter ( zipStream , _utf8WithBom , _configuration . BufferSize ) )
124127 {
125128 if ( value == null )
126129 {
@@ -166,14 +169,14 @@ private void CreateSheetXml(object value, string sheetPath)
166169 {
167170 mode = "IDictionary<string, object>" ;
168171 var dic = item as IDictionary < string , object > ;
169- props = GetDictionaryColumnInfo ( dic , null ) ;
172+ props = GetDictionaryColumnInfo ( dic , null ) ;
170173 maxColumnIndex = props . Count ;
171174 }
172175 else if ( item is IDictionary )
173176 {
174177 var dic = item as IDictionary ;
175178 mode = "IDictionary" ;
176- props = GetDictionaryColumnInfo ( null , dic ) ;
179+ props = GetDictionaryColumnInfo ( null , dic ) ;
177180 //maxColumnIndex = dic.Keys.Count;
178181 maxColumnIndex = props . Count ; // why not using keys, because ignore attribute 
179182 }
@@ -253,7 +256,7 @@ private void CreateSheetXml(object value, string sheetPath)
253256 WriteC ( writer , r , columnName : p . ExcelColumnName ) ;
254257 cellIndex ++ ;
255258 }
256-
259+
257260 writer . Write ( $ "</x:row>") ;
258261 yIndex ++ ;
259262 }
@@ -353,7 +356,7 @@ private void WriteEmptySheet(MiniExcelStreamWriter writer)
353356 private void GenerateSheetByColumnInfo < T > ( MiniExcelStreamWriter writer , IEnumerable value , List < ExcelColumnInfo > props , int xIndex = 1 , int yIndex = 1 )
354357 {
355358 var isDic = typeof ( T ) == typeof ( IDictionary ) ;
356- var isDapperRow = typeof ( T ) == typeof ( IDictionary < string , object > ) ;
359+ var isDapperRow = typeof ( T ) == typeof ( IDictionary < string , object > ) ;
357360 foreach ( T v in value )
358361 {
359362 writer . Write ( $ "<x:row r=\" { yIndex } \" >") ;
@@ -375,14 +378,14 @@ private void GenerateSheetByColumnInfo<T>(MiniExcelStreamWriter writer, IEnumera
375378 else if ( isDapperRow )
376379 {
377380 cellValue = ( ( IDictionary < string , object > ) v ) [ p . Key . ToString ( ) ] ;
378- }
381+ }
379382 else
380383 {
381384 cellValue = p . Property . GetValue ( v ) ;
382385 }
383386 WriteCell ( writer , yIndex , cellIndex , cellValue , p ) ;
384-
385-
387+
388+
386389 cellIndex ++ ;
387390 }
388391 writer . Write ( $ "</x:row>") ;
@@ -403,15 +406,15 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
403406 {
404407 v = ExcelOpenXmlUtils . EncodeXML ( str ) ;
405408 }
406- else if ( p ? . ExcelFormat != null && value is IFormattable formattableValue )
409+ else if ( p ? . ExcelFormat != null && value is IFormattable formattableValue )
407410 {
408411 var formattedStr = formattableValue . ToString ( p . ExcelFormat , _configuration . Culture ) ;
409412 v = ExcelOpenXmlUtils . EncodeXML ( formattedStr ) ;
410413 }
411414 else
412415 {
413416 Type type = null ;
414- if ( p == null || p . Key != null )
417+ if ( p == null || p . Key != null )
415418 {
416419 // TODO: need to optimize
417420 // Dictionary need to check type every time, so it's slow..
@@ -503,7 +506,7 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
503506 }
504507 else if ( type == typeof ( DateTime ) )
505508 {
506- if ( _configuration . Culture != CultureInfo . InvariantCulture )
509+ if ( _configuration . Culture != CultureInfo . InvariantCulture )
507510 {
508511 t = "str" ;
509512 v = ( ( DateTime ) value ) . ToString ( _configuration . Culture ) ;
@@ -518,18 +521,18 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
518521 {
519522 // TODO: now it'll lose date type information
520523 t = "str" ;
521- v = ( ( DateTime ) value ) . ToString ( p . ExcelFormat , _configuration . Culture ) ;
524+ v = ( ( DateTime ) value ) . ToString ( p . ExcelFormat , _configuration . Culture ) ;
522525 }
523526 }
524527 else
525528 {
526529 //TODO: _configuration.Culture
527- v = ExcelOpenXmlUtils . EncodeXML ( value . ToString ( ) ) ;
530+ v = ExcelOpenXmlUtils . EncodeXML ( value . ToString ( ) ) ;
528531 }
529532 }
530533
531534 var columname = ExcelOpenXmlUtils . ConvertXyToCell ( cellIndex , rowIndex ) ;
532- if ( v != null && ( v . StartsWith ( " " , StringComparison . Ordinal ) || v . EndsWith ( " " , StringComparison . Ordinal ) ) ) /*Prefix and suffix blank space will lost after SaveAs #294*/
535+ if ( v != null && ( v . StartsWith ( " " , StringComparison . Ordinal ) || v . EndsWith ( " " , StringComparison . Ordinal ) ) ) /*Prefix and suffix blank space will lost after SaveAs #294*/
533536 writer . Write ( $ "<x:c r=\" { columname } \" { ( t == null ? "" : $ "t =\" { t } \" ") } s=\" { s } \" xml:space=\" preserve\" ><x:v>{ v } </x:v></x:c>") ;
534537 else
535538 //t check avoid format error 
@@ -590,8 +593,13 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
590593 var yIndex = xy . Item2 ;
591594 var xIndex = 0 ;
592595 {
593- dimensionWritePosition = writer . WriteAndFlush ( $@ "<x:dimension ref=""") ;
594- writer . Write ( $@ " />") ; // end of code will be replaced
596+
597+ if ( _configuration . FastMode )
598+ {
599+ dimensionWritePosition = writer . WriteAndFlush ( $@ "<x:dimension ref=""") ;
600+ writer . Write ( " />" ) ; // end of code will be replaced
601+ }
602+
595603 writer . Write ( "<x:sheetData>" ) ;
596604 int fieldCount = reader . FieldCount ;
597605 if ( _printHeader )
@@ -626,11 +634,14 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
626634 }
627635 writer . Write ( "</x:sheetData>" ) ;
628636 if ( _configuration . AutoFilter )
629- writer . Write ( $ "<x:autoFilter ref=\" A1:{ ExcelOpenXmlUtils . ConvertXyToCell ( ( xIndex - 1 ) /*TODO:code smell*/ , yIndex - 1 ) } \" />") ;
637+ writer . Write ( $ "<x:autoFilter ref=\" A1:{ ExcelOpenXmlUtils . ConvertXyToCell ( ( xIndex - 1 ) /*TODO:code smell*/ , yIndex - 1 ) } \" />") ;
630638 writer . WriteAndFlush ( "</x:worksheet>" ) ;
631639
632- writer . SetPosition ( dimensionWritePosition ) ;
633- writer . WriteAndFlush ( $@ "A1:{ ExcelOpenXmlUtils . ConvertXyToCell ( ( xIndex - 1 ) /*TODO:code smell*/ , yIndex - 1 ) } """);
640+ if ( _configuration . FastMode )
641+ {
642+ writer . SetPosition ( dimensionWritePosition ) ;
643+ writer . WriteAndFlush ( $@ "A1:{ ExcelOpenXmlUtils . ConvertXyToCell ( ( xIndex - 1 ) /*TODO:code smell*/ , yIndex - 1 ) } """);
644+ }
634645 }
635646
636647 private static void WriteC(MiniExcelStreamWriter writer, string r, string columnName)
@@ -779,7 +790,7 @@ private string GetDimensionRef(int maxRowIndex, int maxColumnIndex)
779790
780791 public async Task SaveAsAsync ( CancellationToken cancellationToken = default ( CancellationToken ) )
781792 {
782- await Task . Run ( ( ) => SaveAs ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
793+ await Task . Run ( ( ) => SaveAs ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
783794 }
784795
785796 public void Insert ( )
0 commit comments