@@ -29,7 +29,6 @@ type StreamWriter struct {
29
29
Sheet string
30
30
SheetID int
31
31
sheetWritten bool
32
- cols strings.Builder
33
32
worksheet * xlsxWorksheet
34
33
rawData bufferedWriter
35
34
rows int
@@ -413,16 +412,18 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt
413
412
if err != nil {
414
413
return err
415
414
}
416
- c := xlsxC {R : ref , S : options .StyleID }
415
+ c := xlsxC {R : ref , S : sw .worksheet .prepareCellStyle (col , row , options .StyleID )}
416
+ var s int
417
417
if v , ok := val .(Cell ); ok {
418
- c .S = v .StyleID
419
- val = v .Value
418
+ s , val = v .StyleID , v .Value
420
419
setCellFormula (& c , v .Formula )
421
420
} else if v , ok := val .(* Cell ); ok && v != nil {
422
- c .S = v .StyleID
423
- val = v .Value
421
+ s , val = v .StyleID , v .Value
424
422
setCellFormula (& c , v .Formula )
425
423
}
424
+ if s > 0 {
425
+ c .S = s
426
+ }
426
427
if err = sw .setCellValFunc (& c , val ); err != nil {
427
428
_ , _ = sw .rawData .WriteString (`</row>` )
428
429
return err
@@ -433,6 +434,33 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt
433
434
return sw .rawData .Sync ()
434
435
}
435
436
437
+ // SetColStyle provides a function to set the style of a single column or
438
+ // multiple columns for the StreamWriter. Note that you must call
439
+ // the 'SetColStyle' function before the 'SetRow' function. For example set
440
+ // style of column H on Sheet1:
441
+ //
442
+ // err := sw.SetColStyle(8, 8, style)
443
+ func (sw * StreamWriter ) SetColStyle (minVal , maxVal , styleID int ) error {
444
+ if sw .sheetWritten {
445
+ return ErrStreamSetColStyle
446
+ }
447
+ if minVal < MinColumns || minVal > MaxColumns || maxVal < MinColumns || maxVal > MaxColumns {
448
+ return ErrColumnNumber
449
+ }
450
+ if maxVal < minVal {
451
+ minVal , maxVal = maxVal , minVal
452
+ }
453
+ s , err := sw .file .stylesReader ()
454
+ if err != nil {
455
+ return err
456
+ }
457
+ if styleID < 0 || s .CellXfs == nil || len (s .CellXfs .Xf ) <= styleID {
458
+ return newInvalidStyleID (styleID )
459
+ }
460
+ sw .worksheet .setColStyle (minVal , maxVal , styleID )
461
+ return nil
462
+ }
463
+
436
464
// SetColWidth provides a function to set the width of a single column or
437
465
// multiple columns for the StreamWriter. Note that you must call
438
466
// the 'SetColWidth' function before the 'SetRow' function. For example set
@@ -452,14 +480,7 @@ func (sw *StreamWriter) SetColWidth(minVal, maxVal int, width float64) error {
452
480
if minVal > maxVal {
453
481
minVal , maxVal = maxVal , minVal
454
482
}
455
-
456
- sw .cols .WriteString (`<col min="` )
457
- sw .cols .WriteString (strconv .Itoa (minVal ))
458
- sw .cols .WriteString (`" max="` )
459
- sw .cols .WriteString (strconv .Itoa (maxVal ))
460
- sw .cols .WriteString (`" width="` )
461
- sw .cols .WriteString (strconv .FormatFloat (width , 'f' , - 1 , 64 ))
462
- sw .cols .WriteString (`" customWidth="1"/>` )
483
+ sw .worksheet .setColWidth (minVal , maxVal , width )
463
484
return nil
464
485
}
465
486
@@ -642,10 +663,27 @@ func writeCell(buf *bufferedWriter, c xlsxC) {
642
663
func (sw * StreamWriter ) writeSheetData () {
643
664
if ! sw .sheetWritten {
644
665
bulkAppendFields (& sw .rawData , sw .worksheet , 4 , 5 )
645
- if sw .cols .Len () > 0 {
646
- _ , _ = sw .rawData .WriteString ("<cols>" )
647
- _ , _ = sw .rawData .WriteString (sw .cols .String ())
648
- _ , _ = sw .rawData .WriteString ("</cols>" )
666
+ if sw .worksheet .Cols != nil {
667
+ for _ , col := range sw .worksheet .Cols .Col {
668
+ _ , _ = sw .rawData .WriteString ("<cols>" )
669
+ sw .rawData .WriteString (`<col min="` )
670
+ sw .rawData .WriteString (strconv .Itoa (col .Min ))
671
+ sw .rawData .WriteString (`" max="` )
672
+ sw .rawData .WriteString (strconv .Itoa (col .Max ))
673
+ sw .rawData .WriteString (`"` )
674
+ if col .Width != nil {
675
+ sw .rawData .WriteString (` width="` )
676
+ sw .rawData .WriteString (strconv .FormatFloat (* col .Width , 'f' , - 1 , 64 ))
677
+ sw .rawData .WriteString (`" customWidth="1"` )
678
+ }
679
+ if col .Style != 0 {
680
+ sw .rawData .WriteString (` style="` )
681
+ sw .rawData .WriteString (strconv .Itoa (col .Style ))
682
+ sw .rawData .WriteString (`"` )
683
+ }
684
+ sw .rawData .WriteString (`/>` )
685
+ _ , _ = sw .rawData .WriteString ("</cols>" )
686
+ }
649
687
}
650
688
_ , _ = sw .rawData .WriteString (`<sheetData>` )
651
689
sw .sheetWritten = true
0 commit comments