forked from nissl-lab/npoi
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathXSSFCellStyle.cs
1496 lines (1351 loc) · 48 KB
/
XSSFCellStyle.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
using NPOI.OpenXmlFormats.Spreadsheet;
using System;
using System.Xml;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel.Extensions;
using NPOI.XSSF.Model;
namespace NPOI.XSSF.UserModel
{
/**
*
* High level representation of the the possible formatting information for the contents of the cells on a sheet in a
* SpreadsheetML document.
*
* @see NPOI.xssf.usermodel.XSSFWorkbook#CreateCellStyle()
* @see NPOI.xssf.usermodel.XSSFWorkbook#getCellStyleAt(short)
* @see NPOI.xssf.usermodel.XSSFCell#setCellStyle(NPOI.ss.usermodel.CellStyle)
*/
public class XSSFCellStyle : ICellStyle
{
private int _cellXfId;
private StylesTable _stylesSource;
private CT_Xf _cellXf;
private CT_Xf _cellStyleXf;
private XSSFFont _font;
private XSSFCellAlignment _cellAlignment;
private ThemesTable _theme;
/**
* Creates a Cell Style from the supplied parts
* @param cellXfId The main XF for the cell. Must be a valid 0-based index into the XF table
* @param cellStyleXfId Optional, style xf. A value of <code>-1</code> means no xf.
* @param stylesSource Styles Source to work off
*/
public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource, ThemesTable theme)
{
_cellXfId = cellXfId;
_stylesSource = stylesSource;
_cellXf = stylesSource.GetCellXfAt(this._cellXfId);
_cellStyleXf = cellStyleXfId == -1 ? null : stylesSource.GetCellStyleXfAt(cellStyleXfId);
_theme = theme;
}
/**
* Used so that StylesSource can figure out our location
*/
public CT_Xf GetCoreXf()
{
return _cellXf;
}
/**
* Used so that StylesSource can figure out our location
*/
public CT_Xf GetStyleXf()
{
return _cellStyleXf;
}
/// <summary>
/// Creates an empty Cell Style
/// </summary>
/// <param name="stylesSource"></param>
public XSSFCellStyle(StylesTable stylesSource)
{
_stylesSource = stylesSource;
// We need a new CT_Xf for the main styles
// TODO decide on a style ctxf
_cellXf = new CT_Xf();
_cellStyleXf = null;
}
/**
* Verifies that this style belongs to the supplied Workbook
* Styles Source.
* Will throw an exception if it belongs to a different one.
* This is normally called when trying to assign a style to a
* cell, to ensure the cell and the style are from the same
* workbook (if they're not, it won't work)
* @throws ArgumentException if there's a workbook mis-match
*/
public void VerifyBelongsToStylesSource(StylesTable src)
{
if (this._stylesSource != src)
{
throw new ArgumentException("This Style does not belong to the supplied Workbook Styles Source. Are you trying to assign a style from one workbook to the cell of a different workbook?");
}
}
/**
* Clones all the style information from another
* XSSFCellStyle, onto this one. This
* XSSFCellStyle will then have all the same
* properties as the source, but the two may
* be edited independently.
* Any stylings on this XSSFCellStyle will be lost!
*
* The source XSSFCellStyle could be from another
* XSSFWorkbook if you like. This allows you to
* copy styles from one XSSFWorkbook to another.
*/
public void CloneStyleFrom(ICellStyle source)
{
if (source is XSSFCellStyle)
{
XSSFCellStyle src = (XSSFCellStyle)source;
// Is it on our Workbook?
if (src._stylesSource == _stylesSource)
{
// Nice and easy
src.GetCoreXf().CopyTo(_cellXf);
// There is no need to copy _cellStyleXf couse XSSFCellStyle does not modify any _cellStyleXf properties.
// Just update _cellStyleXf reference
if (_cellXf.xfIdSpecified)
{
_cellStyleXf = _stylesSource.GetCellStyleXfAt((int)_cellXf.xfId);
}
else
{
_cellStyleXf = null;
}
}
else
{
// Copy the style
try
{
// Remove any children off the current style, to
// avoid orphaned nodes
if (_cellXf.IsSetAlignment())
_cellXf.UnsetAlignment();
if (_cellXf.IsSetExtLst())
_cellXf.UnsetExtLst();
// Create a new Xf with the same contents
_cellXf = src.GetCoreXf().Copy();
// bug 56295: ensure that the fills is available and set correctly
CT_Fill fill = CT_Fill.Parse(src.GetCTFill().ToString());
AddFill(fill);
// bug 58084: set borders correctly
CT_Border border = CT_Border.Parse(src.GetCTBorder().ToString());
AddBorder(border);
// Swap it over
_stylesSource.ReplaceCellXfAt(_cellXfId, _cellXf);
}
catch (XmlException e)
{
throw new POIXMLException(e);
}
// Copy the format
String fmt = src.GetDataFormatString();
DataFormat = (
(new XSSFDataFormat(_stylesSource)).GetFormat(fmt)
);
// Copy the font
try
{
CT_Font ctFont =
src.GetFont().GetCTFont().Clone();
XSSFFont font = new XSSFFont(ctFont);
font.RegisterTo(_stylesSource);
SetFont(font);
}
catch (XmlException e)
{
throw new POIXMLException(e);
}
}
// Clear out cached details
_font = null;
_cellAlignment = null;
}
else
{
throw new ArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
}
}
private void AddFill(CT_Fill fill)
{
int idx = _stylesSource.PutFill(new XSSFCellFill(fill));
_cellXf.fillId = (uint)(idx);
_cellXf.applyFill = (true);
}
private void AddBorder(CT_Border border)
{
int idx = _stylesSource.PutBorder(new XSSFCellBorder(border, _theme));
_cellXf.borderId = (uint)(idx);
_cellXf.applyBorder = (true);
}
private uint FindAddBorder(CT_Border border)
{
//Find an existing border that matches this one, if not add a copy to the current source and update the reference.
int findThis = border.ToString().GetHashCode();
uint index = 0;
foreach (XSSFCellBorder existing in _stylesSource.GetBorders())
{
if (findThis == existing.GetCTBorder().ToString().GetHashCode())
{
return index;
}
index++;
}
//404 border not found. Add it
return (uint)_stylesSource.PutBorder(new XSSFCellBorder(border.Copy()));
}
public HorizontalAlignment Alignment
{
get
{
return GetAlignmentEnum();
}
set
{
GetCellAlignment().Horizontal = value;
}
}
/// <summary>
/// Get the type of horizontal alignment for the cell
/// </summary>
/// <returns>the type of alignment</returns>
internal HorizontalAlignment GetAlignmentEnum()
{
CT_CellAlignment align = _cellXf.alignment;
if (align != null && align.IsSetHorizontal())
{
return (HorizontalAlignment)align.horizontal;
}
return HorizontalAlignment.General;
}
/// <summary>
/// Get or set the type of border to use for the bottom border of the cell
/// </summary>
public BorderStyle BorderBottom
{
get
{
if (!_cellXf.applyBorder) return BorderStyle.None;
int idx = (int)_cellXf.borderId;
CT_Border ct = _stylesSource.GetBorderAt(idx).GetCTBorder();
if (!ct.IsSetBottom())
{
return BorderStyle.None;
}
else
{
return (BorderStyle)ct.bottom.style;
}
}
set
{
CT_Border ct = GetCTBorder(copy: true);
CT_BorderPr pr = ct.IsSetBottom() ? ct.bottom : ct.AddNewBottom();
if (value == BorderStyle.None)
ct.UnsetBottom();
else
pr.style = (ST_BorderStyle)value;
int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));
_cellXf.borderId = (uint)idx;
_cellXf.applyBorder = (true);
}
}
/// <summary>
/// Get or set the type of border to use for the left border of the cell
/// </summary>
public BorderStyle BorderLeft
{
get
{
if (!_cellXf.applyBorder) return BorderStyle.None;
int idx = (int)_cellXf.borderId;
CT_Border ct = _stylesSource.GetBorderAt(idx).GetCTBorder();
if (!ct.IsSetLeft())
{
return BorderStyle.None;
}
else
{
return (BorderStyle)ct.left.style;
}
}
set
{
CT_Border ct = GetCTBorder(copy: true);
CT_BorderPr pr = ct.IsSetLeft() ? ct.left : ct.AddNewLeft();
if (value == BorderStyle.None) ct.unsetLeft();
else pr.style = (ST_BorderStyle)value;
int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));
_cellXf.borderId = (uint)idx;
_cellXf.applyBorder = (true);
}
}
/// <summary>
/// Get or set the type of border to use for the right border of the cell
/// </summary>
public BorderStyle BorderRight
{
get
{
if (!_cellXf.applyBorder) return BorderStyle.None;
int idx = (int)_cellXf.borderId;
CT_Border ct = _stylesSource.GetBorderAt(idx).GetCTBorder();
if (!ct.IsSetRight())
{
return BorderStyle.None;
}
else
{
return (BorderStyle)ct.right.style;
}
}
set
{
CT_Border ct = GetCTBorder(copy: true);
CT_BorderPr pr = ct.IsSetRight() ? ct.right : ct.AddNewRight();
if (value == BorderStyle.None) ct.unsetRight();
else pr.style = (ST_BorderStyle)value;
int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));
_cellXf.borderId = (uint)idx;
_cellXf.applyBorder = (true);
}
}
/// <summary>
/// Get or set the type of border to use for the top border of the cell
/// </summary>
public BorderStyle BorderTop
{
get
{
if (!_cellXf.applyBorder) return BorderStyle.None;
int idx = (int)_cellXf.borderId;
CT_Border ct = _stylesSource.GetBorderAt(idx).GetCTBorder();
if (!ct.IsSetTop())
{
return BorderStyle.None;
}
else
{
return (BorderStyle)ct.top.style;
}
}
set
{
CT_Border ct = GetCTBorder(copy: true);
CT_BorderPr pr = ct.IsSetTop() ? ct.top : ct.AddNewTop();
if (value == BorderStyle.None) ct.unsetTop();
else pr.style = (ST_BorderStyle)value;
int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));
_cellXf.borderId = (uint)idx;
_cellXf.applyBorder = (true);
}
}
/**
* Get the color to use for the bottom border
* Color is optional. When missing, IndexedColors.Automatic is implied.
* @return the index of the color defInition, default value is {@link NPOI.ss.usermodel.IndexedColors#AUTOMATIC}
* @see NPOI.ss.usermodel.IndexedColors
*/
public short BottomBorderColor
{
get
{
XSSFColor clr = BottomBorderXSSFColor;
return clr == null ? IndexedColors.Black.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = value;
SetBottomBorderColor(clr);
}
}
/**
* Get the color to use for the bottom border as a {@link XSSFColor}
*
* @return the used color or <code>null</code> if not Set
*/
public XSSFColor BottomBorderXSSFColor
{
get
{
if (!_cellXf.applyBorder) return null;
int idx = (int)_cellXf.borderId;
XSSFCellBorder border = _stylesSource.GetBorderAt(idx);
return border.GetBorderColor(BorderSide.BOTTOM);
}
}
/**
* Get the index of the number format (numFmt) record used by this cell format.
*
* @return the index of the number format
*/
public short DataFormat
{
get
{
return (short)_cellXf.numFmtId;
}
set
{
// XSSF supports >32,767 formats
SetDataFormat(value & 0xFFFF);
}
}
/**
* Set the index of a data format
*
* @param fmt the index of a data format
*/
public void SetDataFormat(int fmt)
{
_cellXf.applyNumberFormat = (true);
_cellXf.numFmtId = (uint)(fmt);
}
/**
* Get the contents of the format string, by looking up
* the StylesSource
*
* @return the number format string
*/
public String GetDataFormatString()
{
int idx = DataFormat;
return new XSSFDataFormat(_stylesSource).GetFormat((short)idx);
}
/// <summary>
/// Get the background fill color.
/// Note - many cells are actually filled with a foreground fill, not a background fill
/// </summary>
public short FillBackgroundColor
{
get
{
XSSFColor clr = (XSSFColor)this.FillBackgroundColorColor;
return clr == null ? IndexedColors.Automatic.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = (value);
SetFillBackgroundColor(clr);
}
}
/**
* Get the background fill color.
* <p>
* Note - many cells are actually Filled with a foreground
* Fill, not a background fill - see {@link #getFillForegroundColor()}
* </p>
* @see NPOI.xssf.usermodel.XSSFColor#getRgb()
* @return XSSFColor - fill color or <code>null</code> if not Set
*/
public IColor FillBackgroundColorColor
{
get
{
return this.FillBackgroundXSSFColor;
}
set
{
this.FillBackgroundXSSFColor = (XSSFColor)value;
}
}
public XSSFColor FillBackgroundXSSFColor
{
get
{
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if (_cellXf.IsSetApplyFill() && !_cellXf.applyFill) return null;
int fillIndex = (int)_cellXf.fillId;
XSSFCellFill fg = _stylesSource.GetFillAt(fillIndex);
XSSFColor fillBackgroundColor = fg.GetFillBackgroundColor();
if (fillBackgroundColor != null && _theme != null)
{
_theme.InheritFromThemeAsRequired(fillBackgroundColor);
}
return fillBackgroundColor;
}
set
{
CT_Fill ct = GetCTFill();
CT_PatternFill ptrn = ct.patternFill;
if (value == null)
{
if (ptrn != null && ptrn.IsSetBgColor()) ptrn.UnsetBgColor();
}
else
{
if (ptrn == null) ptrn = ct.AddNewPatternFill();
ptrn.bgColor = (value.GetCTColor());
}
AddFill(ct);
}
}
/**
* Get the foreground fill color.
* <p>
* Many cells are Filled with this, instead of a
* background color ({@link #getFillBackgroundColor()})
* </p>
* @see IndexedColors
* @return fill color, default value is {@link NPOI.ss.usermodel.IndexedColors#AUTOMATIC}
*/
public short FillForegroundColor
{
get
{
XSSFColor clr = (XSSFColor)this.FillForegroundColorColor;
return clr == null ? IndexedColors.Automatic.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = (value);
SetFillForegroundColor(clr);
}
}
/// <summary>
/// Get the foreground fill color.
/// </summary>
public IColor FillForegroundColorColor
{
get
{
return this.FillForegroundXSSFColor;
}
set
{
this.FillForegroundXSSFColor = (XSSFColor)value;
}
}
/// <summary>
/// Get the foreground fill color.
/// </summary>
public XSSFColor FillForegroundXSSFColor
{
get
{
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if (_cellXf.IsSetApplyFill() && !_cellXf.applyFill) return null;
int fillIndex = (int)_cellXf.fillId;
XSSFCellFill fg = _stylesSource.GetFillAt(fillIndex);
XSSFColor fillForegroundColor = fg.GetFillForegroundColor();
if (fillForegroundColor != null && _theme != null)
{
_theme.InheritFromThemeAsRequired(fillForegroundColor);
}
return fillForegroundColor;
}
set
{
CT_Fill ct = GetCTFill();
CT_PatternFill ptrn = ct.patternFill;
if (value == null)
{
if (ptrn != null && ptrn.IsSetFgColor()) ptrn.UnsetFgColor();
}
else
{
if (ptrn == null) ptrn = ct.AddNewPatternFill();
ptrn.fgColor = (value.GetCTColor());
}
AddFill(ct);
}
}
public FillPattern FillPattern
{
get
{
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if (_cellXf.IsSetApplyFill() && !_cellXf.applyFill) return FillPattern.NoFill;
int FillIndex = (int)_cellXf.fillId;
XSSFCellFill fill = _stylesSource.GetFillAt(FillIndex);
ST_PatternType ptrn = fill.GetPatternType();
if(ptrn == ST_PatternType.none) return FillPattern.NoFill;
return (FillPattern)((int)ptrn);
//return FillPattern.forInt(ptrn.intValue() - 1); minus one in poi, why???
}
set
{
CT_Fill ct = GetCTFill();
CT_PatternFill ptrn = ct.IsSetPatternFill() ? ct.GetPatternFill() : ct.AddNewPatternFill();
if (value == FillPattern.NoFill && ptrn.IsSetPatternType())
ptrn.UnsetPatternType();
else
ptrn.patternType = (ST_PatternType)(value);
// ctptrn.setPatternType(STPatternType.Enum.forInt(pattern.getCode() + 1)); plus one in poi, why???
AddFill(ct);
}
}
/**
* Gets the font for this style
* @return Font - font
*/
public XSSFFont GetFont()
{
if (_font == null)
{
_font = _stylesSource.GetFontAt(FontId);
}
return _font;
}
/**
* Gets the index of the font for this style
*
* @return short - font index
* @see NPOI.xssf.usermodel.XSSFWorkbook#getFontAt(short)
*/
public short FontIndex
{
get
{
return (short)FontId;
}
}
/**
* Get whether the cell's using this style are to be hidden
*
* @return bool - whether the cell using this style is hidden
*/
public bool IsHidden
{
get
{
if (!_cellXf.IsSetProtection() || !_cellXf.protection.IsSetHidden())
{
return false;
}
return _cellXf.protection.hidden;
}
set
{
if (!_cellXf.IsSetProtection())
{
_cellXf.AddNewProtection();
}
_cellXf.protection.hidden = (value);
}
}
/**
* Get the number of spaces to indent the text in the cell
*
* @return indent - number of spaces
*/
public short Indention
{
get
{
CT_CellAlignment align = _cellXf.alignment;
return (short)(align == null ? 0 : align.indent);
}
set
{
GetCellAlignment().Indent = value;
}
}
/**
* Get the index within the StylesTable (sequence within the collection of CT_Xf elements)
*
* @return unique index number of the underlying record this style represents
*/
public short Index
{
get
{
return (short)this._cellXfId;
}
}
protected internal int UIndex
{
get
{
return this._cellXfId;
}
}
/**
* Get the color to use for the left border
*
* @return the index of the color defInition, default value is {@link NPOI.ss.usermodel.IndexedColors#BLACK}
* @see NPOI.ss.usermodel.IndexedColors
*/
public short LeftBorderColor
{
get
{
XSSFColor clr = LeftBorderXSSFColor;
return clr == null ? IndexedColors.Black.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = (value);
SetLeftBorderColor(clr);
}
}
public XSSFColor DiagonalBorderXSSFColor
{
get
{
if (!_cellXf.applyBorder) return null;
int idx = (int)_cellXf.borderId;
XSSFCellBorder border = _stylesSource.GetBorderAt(idx);
return border.GetBorderColor(BorderSide.DIAGONAL);
}
}
/**
* Get the color to use for the left border
*
* @return the index of the color defInition or <code>null</code> if not Set
* @see NPOI.ss.usermodel.IndexedColors
*/
public XSSFColor LeftBorderXSSFColor
{
get
{
if (!_cellXf.applyBorder) return null;
int idx = (int)_cellXf.borderId;
XSSFCellBorder border = _stylesSource.GetBorderAt(idx);
return border.GetBorderColor(BorderSide.LEFT);
}
}
/// <summary>
/// Get whether the cell's using this style are locked
/// </summary>
public bool IsLocked
{
get
{
if (!_cellXf.IsSetProtection())
{
return true;
}
return _cellXf.protection.locked;
}
set
{
if (!_cellXf.IsSetProtection())
{
_cellXf.AddNewProtection();
}
_cellXf.protection.locked = value;
}
}
/// <summary>
/// Turn on or off "Quote Prefix" or "123 Prefix" for the style,
/// which is used to tell Excel that the thing which looks like
/// a number or a formula shouldn't be treated as on.
/// </summary>
/// <value>Is "Quote Prefix" or "123 Prefix" enabled for the cell?</value>
public bool IsQuotePrefixed
{
get
{
return _cellXf.quotePrefix;
}
set
{
_cellXf.quotePrefix = value;
}
}
/// <summary>
/// Get the color to use for the right border
/// </summary>
public short RightBorderColor
{
get
{
XSSFColor clr = RightBorderXSSFColor;
return clr == null ? IndexedColors.Black.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = (value);
SetRightBorderColor(clr);
}
}
/// <summary>
/// Get the color to use for the right border
/// </summary>
/// <returns></returns>
public XSSFColor RightBorderXSSFColor
{
get
{
if (!_cellXf.applyBorder) return null;
int idx = (int)_cellXf.borderId;
XSSFCellBorder border = _stylesSource.GetBorderAt(idx);
return border.GetBorderColor(BorderSide.RIGHT);
}
}
/// <summary>
/// Get the degree of rotation (between 0 and 180 degrees) for the text in the cell
///
/// Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
/// uses values from 0 to 180 degrees.The implementations of this method will map between these two value-ranges
/// accordingly, however the corresponding getter is returning values in the range mandated by the current type
/// of Excel file-format that this CellStyle is applied to.
/// </summary>
/// <example>
/// Expressed in degrees. Values range from 0 to 180. The first letter of
/// the text is considered the center-point of the arc.
/// For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the horizon is calculated as:
/// <code>[degrees below horizon] = 90 - textRotation.</code>
/// </example>
public short Rotation
{
get
{
CT_CellAlignment align = _cellXf.alignment;
return (short)(align == null ? 0 : align.textRotation);
}
set
{
GetCellAlignment().TextRotation = value;
}
}
/**
* Get the color to use for the top border
*
* @return the index of the color defInition, default value is {@link NPOI.ss.usermodel.IndexedColors#BLACK}
* @see NPOI.ss.usermodel.IndexedColors
*/
public short TopBorderColor
{
get
{
XSSFColor clr = TopBorderXSSFColor;
return clr == null ? IndexedColors.Black.Index : clr.Indexed;
}
set
{
XSSFColor clr = new XSSFColor();
clr.Indexed = (value);
SetTopBorderColor(clr);
}
}
/// <summary>
/// Get the color to use for the top border
/// </summary>
/// <returns></returns>
public XSSFColor TopBorderXSSFColor
{
get
{
if (!_cellXf.applyBorder) return null;
int idx = (int)_cellXf.borderId;
XSSFCellBorder border = _stylesSource.GetBorderAt(idx);
return border.GetBorderColor(BorderSide.TOP);
}
}
/// <summary>
/// Get the type of vertical alignment for the cell
/// </summary>
public VerticalAlignment VerticalAlignment
{
get
{
return GetVerticalAlignmentEnum();
}
set
{
GetCellAlignment().Vertical = value;
}
}
/// <summary>
/// Get the type of vertical alignment for the cell
/// </summary>
/// <returns></returns>
internal VerticalAlignment GetVerticalAlignmentEnum()
{
CT_CellAlignment align = _cellXf.alignment;
if (align != null && align.IsSetVertical())
{
return (VerticalAlignment)align.vertical;
}
return VerticalAlignment.Bottom;
}
/// <summary>
/// Whether the text in a cell should be line-wrapped within the cell.
/// </summary>
public bool WrapText
{
get
{
CT_CellAlignment align = _cellXf.alignment;
return align != null && align.wrapText;
}
set
{
GetCellAlignment().WrapText = value;
}
}
/**
* Set the color to use for the bottom border
*
* @param color the color to use, null means no color
*/
public void SetBottomBorderColor(XSSFColor color)
{
CT_Border ct = GetCTBorder(copy: true);
if (color == null && !ct.IsSetBottom()) return;
CT_BorderPr pr = ct.IsSetBottom() ? ct.bottom : ct.AddNewBottom();
if (color != null) pr.SetColor(color.GetCTColor());
else pr.UnsetColor();
int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));
_cellXf.borderId = (uint)idx;