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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
|
/* ====================================================================
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.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
/**
* High level representation of the style of a cell in a sheet of a workbook.
*
* @version 1.0-pre
*
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Jason Height (jheight at chariot dot net dot au)
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
* @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle)
*/
public class HSSFCellStyle implements CellStyle
{
private ExtendedFormatRecord format = null;
private short index = 0;
private Workbook workbook = null;
/**
* general (normal) horizontal alignment
*/
public final static short ALIGN_GENERAL = 0x0;
/**
* left-justified horizontal alignment
*/
public final static short ALIGN_LEFT = 0x1;
/**
* center horizontal alignment
*/
public final static short ALIGN_CENTER = 0x2;
/**
* right-justified horizontal alignment
*/
public final static short ALIGN_RIGHT = 0x3;
/**
* fill? horizontal alignment
*/
public final static short ALIGN_FILL = 0x4;
/**
* justified horizontal alignment
*/
public final static short ALIGN_JUSTIFY = 0x5;
/**
* center-selection? horizontal alignment
*/
public final static short ALIGN_CENTER_SELECTION = 0x6;
/**
* top-aligned vertical alignment
*/
public final static short VERTICAL_TOP = 0x0;
/**
* center-aligned vertical alignment
*/
public final static short VERTICAL_CENTER = 0x1;
/**
* bottom-aligned vertical alignment
*/
public final static short VERTICAL_BOTTOM = 0x2;
/**
* vertically justified vertical alignment
*/
public final static short VERTICAL_JUSTIFY = 0x3;
/**
* No border
*/
public final static short BORDER_NONE = 0x0;
/**
* Thin border
*/
public final static short BORDER_THIN = 0x1;
/**
* Medium border
*/
public final static short BORDER_MEDIUM = 0x2;
/**
* dash border
*/
public final static short BORDER_DASHED = 0x3;
/**
* dot border
*/
public final static short BORDER_HAIR = 0x4;
/**
* Thick border
*/
public final static short BORDER_THICK = 0x5;
/**
* double-line border
*/
public final static short BORDER_DOUBLE = 0x6;
/**
* hair-line border
*/
public final static short BORDER_DOTTED = 0x7;
/**
* Medium dashed border
*/
public final static short BORDER_MEDIUM_DASHED = 0x8;
/**
* dash-dot border
*/
public final static short BORDER_DASH_DOT = 0x9;
/**
* medium dash-dot border
*/
public final static short BORDER_MEDIUM_DASH_DOT = 0xA;
/**
* dash-dot-dot border
*/
public final static short BORDER_DASH_DOT_DOT = 0xB;
/**
* medium dash-dot-dot border
*/
public final static short BORDER_MEDIUM_DASH_DOT_DOT = 0xC;
/**
* slanted dash-dot border
*/
public final static short BORDER_SLANTED_DASH_DOT = 0xD;
/** No background */
public final static short NO_FILL = 0 ;
/** Solidly filled */
public final static short SOLID_FOREGROUND = 1 ;
/** Small fine dots */
public final static short FINE_DOTS = 2 ;
/** Wide dots */
public final static short ALT_BARS = 3 ;
/** Sparse dots */
public final static short SPARSE_DOTS = 4 ;
/** Thick horizontal bands */
public final static short THICK_HORZ_BANDS = 5 ;
/** Thick vertical bands */
public final static short THICK_VERT_BANDS = 6 ;
/** Thick backward facing diagonals */
public final static short THICK_BACKWARD_DIAG = 7 ;
/** Thick forward facing diagonals */
public final static short THICK_FORWARD_DIAG = 8 ;
/** Large spots */
public final static short BIG_SPOTS = 9 ;
/** Brick-like layout */
public final static short BRICKS = 10 ;
/** Thin horizontal bands */
public final static short THIN_HORZ_BANDS = 11 ;
/** Thin vertical bands */
public final static short THIN_VERT_BANDS = 12 ;
/** Thin backward diagonal */
public final static short THIN_BACKWARD_DIAG = 13 ;
/** Thin forward diagonal */
public final static short THIN_FORWARD_DIAG = 14 ;
/** Squares */
public final static short SQUARES = 15 ;
/** Diamonds */
public final static short DIAMONDS = 16 ;
/** Less Dots */
public final static short LESS_DOTS = 17 ;
/** Least Dots */
public final static short LEAST_DOTS = 18 ;
/** Creates new HSSFCellStyle why would you want to do this?? */
protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook)
{
this(index, rec, workbook.getWorkbook());
}
protected HSSFCellStyle(short index, ExtendedFormatRecord rec, Workbook workbook)
{
this.workbook = workbook;
this.index = index;
format = rec;
}
/**
* get the index within the HSSFWorkbook (sequence within the collection of ExtnededFormat objects)
* @return unique index number of the underlying record this style represents (probably you don't care
* unless you're comparing which one is which)
*/
public short getIndex()
{
return index;
}
/**
* set the data format (must be a valid format)
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
*/
public void setDataFormat(short fmt)
{
format.setFormatIndex(fmt);
}
/**
* get the index of the format
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
*/
public short getDataFormat()
{
return format.getFormatIndex();
}
/**
* Get the contents of the format string, by looking up
* the DataFormat against the bound workbook
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
* @return the format string or "General" if not found
*/
public String getDataFormatString() {
return getDataFormatString(workbook);
}
/**
* Get the contents of the format string, by looking up
* the DataFormat against the supplied workbook
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
*
* @return the format string or "General" if not found
*/
public String getDataFormatString(org.apache.poi.ss.usermodel.Workbook workbook) {
HSSFDataFormat format = new HSSFDataFormat( ((HSSFWorkbook)workbook).getWorkbook() );
int idx = getDataFormat();
return idx == -1 ? "General" : format.getFormat(getDataFormat());
}
/**
* Get the contents of the format string, by looking up
* the DataFormat against the supplied low level workbook
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
*/
public String getDataFormatString(org.apache.poi.hssf.model.Workbook workbook) {
HSSFDataFormat format = new HSSFDataFormat( workbook );
return format.getFormat(getDataFormat());
}
/**
* set the font for this style
* @param font a font object created or retreived from the HSSFWorkbook object
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/
public void setFont(Font font) {
setFont((HSSFFont)font);
}
public void setFont(HSSFFont font) {
format.setIndentNotParentFont(true);
short fontindex = font.getIndex();
format.setFontIndex(fontindex);
}
/**
* gets the index of the font for this style
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/
public short getFontIndex()
{
return format.getFontIndex();
}
/**
* gets the font for this style
* @param parentWorkbook The HSSFWorkbook that this style belongs to
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndex()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/
public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) {
return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex());
}
/**
* set the cell's using this style to be hidden
* @param hidden - whether the cell using this style should be hidden
*/
public void setHidden(boolean hidden)
{
format.setIndentNotParentCellOptions(true);
format.setHidden(hidden);
}
/**
* get whether the cell's using this style are to be hidden
* @return hidden - whether the cell using this style should be hidden
*/
public boolean getHidden()
{
return format.isHidden();
}
/**
* set the cell's using this style to be locked
* @param locked - whether the cell using this style should be locked
*/
public void setLocked(boolean locked)
{
format.setIndentNotParentCellOptions(true);
format.setLocked(locked);
}
/**
* get whether the cell's using this style are to be locked
* @return hidden - whether the cell using this style should be locked
*/
public boolean getLocked()
{
return format.isLocked();
}
/**
* set the type of horizontal alignment for the cell
* @param align - the type of alignment
* @see #ALIGN_GENERAL
* @see #ALIGN_LEFT
* @see #ALIGN_CENTER
* @see #ALIGN_RIGHT
* @see #ALIGN_FILL
* @see #ALIGN_JUSTIFY
* @see #ALIGN_CENTER_SELECTION
*/
public void setAlignment(short align)
{
format.setIndentNotParentAlignment(true);
format.setAlignment(align);
}
/**
* get the type of horizontal alignment for the cell
* @return align - the type of alignment
* @see #ALIGN_GENERAL
* @see #ALIGN_LEFT
* @see #ALIGN_CENTER
* @see #ALIGN_RIGHT
* @see #ALIGN_FILL
* @see #ALIGN_JUSTIFY
* @see #ALIGN_CENTER_SELECTION
*/
public short getAlignment()
{
return format.getAlignment();
}
/**
* get whether this cell is to be part of a merged block of cells
*
* @returns merged or not
*/
// public boolean getMergeCells()
// {
// return format.getMergeCells();
// }
/**
* set whether this cell is to be part of a merged block of cells
*
* @param merge merged or not
*/
// public void setMergeCells(boolean merge)
// {
// format.setMergeCells(merge);
// }
/**
* set whether the text should be wrapped
* @param wrapped wrap text or not
*/
public void setWrapText(boolean wrapped)
{
format.setIndentNotParentAlignment(true);
format.setWrapText(wrapped);
}
/**
* get whether the text should be wrapped
* @return wrap text or not
*/
public boolean getWrapText()
{
return format.getWrapText();
}
/**
* set the type of vertical alignment for the cell
* @param align the type of alignment
* @see #VERTICAL_TOP
* @see #VERTICAL_CENTER
* @see #VERTICAL_BOTTOM
* @see #VERTICAL_JUSTIFY
*/
public void setVerticalAlignment(short align)
{
format.setVerticalAlignment(align);
}
/**
* get the type of vertical alignment for the cell
* @return align the type of alignment
* @see #VERTICAL_TOP
* @see #VERTICAL_CENTER
* @see #VERTICAL_BOTTOM
* @see #VERTICAL_JUSTIFY
*/
public short getVerticalAlignment()
{
return format.getVerticalAlignment();
}
/**
* set the degree of rotation for the text in the cell
* @param rotation degrees (between -90 and 90 degrees)
*/
public void setRotation(short rotation)
{
if ((rotation < 0)&&(rotation >= -90)) {
//Take care of the funny 4th quadrant issue
//The 4th quadrant (-1 to -90) is stored as (91 to 180)
rotation = (short)(90 - rotation);
}
else if ((rotation < -90) ||(rotation > 90))
//Do not allow an incorrect rotation to be set
throw new IllegalArgumentException("The rotation must be between -90 and 90 degrees");
format.setRotation(rotation);
}
/**
* get the degree of rotation for the text in the cell
* @return rotation degrees (between -90 and 90 degrees)
*/
public short getRotation()
{
short rotation = format.getRotation();
if (rotation > 90)
//This is actually the 4th quadrant
rotation = (short)(90-rotation);
return rotation;
}
/**
* set the number of spaces to indent the text in the cell
* @param indent - number of spaces
*/
public void setIndention(short indent)
{
format.setIndent(indent);
}
/**
* get the number of spaces to indent the text in the cell
* @return indent - number of spaces
*/
public short getIndention()
{
return format.getIndent();
}
/**
* set the type of border to use for the left border of the cell
* @param border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public void setBorderLeft(short border)
{
format.setIndentNotParentBorder(true);
format.setBorderLeft(border);
}
/**
* get the type of border to use for the left border of the cell
* @return border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public short getBorderLeft()
{
return format.getBorderLeft();
}
/**
* set the type of border to use for the right border of the cell
* @param border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public void setBorderRight(short border)
{
format.setIndentNotParentBorder(true);
format.setBorderRight(border);
}
/**
* get the type of border to use for the right border of the cell
* @return border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public short getBorderRight()
{
return format.getBorderRight();
}
/**
* set the type of border to use for the top border of the cell
* @param border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public void setBorderTop(short border)
{
format.setIndentNotParentBorder(true);
format.setBorderTop(border);
}
/**
* get the type of border to use for the top border of the cell
* @return border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public short getBorderTop()
{
return format.getBorderTop();
}
/**
* set the type of border to use for the bottom border of the cell
* @param border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public void setBorderBottom(short border)
{
format.setIndentNotParentBorder(true);
format.setBorderBottom(border);
}
/**
* get the type of border to use for the bottom border of the cell
* @return border type
* @see #BORDER_NONE
* @see #BORDER_THIN
* @see #BORDER_MEDIUM
* @see #BORDER_DASHED
* @see #BORDER_DOTTED
* @see #BORDER_THICK
* @see #BORDER_DOUBLE
* @see #BORDER_HAIR
* @see #BORDER_MEDIUM_DASHED
* @see #BORDER_DASH_DOT
* @see #BORDER_MEDIUM_DASH_DOT
* @see #BORDER_DASH_DOT_DOT
* @see #BORDER_MEDIUM_DASH_DOT_DOT
* @see #BORDER_SLANTED_DASH_DOT
*/
public short getBorderBottom()
{
return format.getBorderBottom();
}
/**
* set the color to use for the left border
* @param color The index of the color definition
*/
public void setLeftBorderColor(short color)
{
format.setLeftBorderPaletteIdx(color);
}
/**
* get the color to use for the left border
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @param color The index of the color definition
*/
public short getLeftBorderColor()
{
return format.getLeftBorderPaletteIdx();
}
/**
* set the color to use for the right border
* @param color The index of the color definition
*/
public void setRightBorderColor(short color)
{
format.setRightBorderPaletteIdx(color);
}
/**
* get the color to use for the left border
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @param color The index of the color definition
*/
public short getRightBorderColor()
{
return format.getRightBorderPaletteIdx();
}
/**
* set the color to use for the top border
* @param color The index of the color definition
*/
public void setTopBorderColor(short color)
{
format.setTopBorderPaletteIdx(color);
}
/**
* get the color to use for the top border
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @param color The index of the color definition
*/
public short getTopBorderColor()
{
return format.getTopBorderPaletteIdx();
}
/**
* set the color to use for the bottom border
* @param color The index of the color definition
*/
public void setBottomBorderColor(short color)
{
format.setBottomBorderPaletteIdx(color);
}
/**
* get the color to use for the left border
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @param color The index of the color definition
*/
public short getBottomBorderColor()
{
return format.getBottomBorderPaletteIdx();
}
/**
* setting to one fills the cell with the foreground color... No idea about
* other values
*
* @see #NO_FILL
* @see #SOLID_FOREGROUND
* @see #FINE_DOTS
* @see #ALT_BARS
* @see #SPARSE_DOTS
* @see #THICK_HORZ_BANDS
* @see #THICK_VERT_BANDS
* @see #THICK_BACKWARD_DIAG
* @see #THICK_FORWARD_DIAG
* @see #BIG_SPOTS
* @see #BRICKS
* @see #THIN_HORZ_BANDS
* @see #THIN_VERT_BANDS
* @see #THIN_BACKWARD_DIAG
* @see #THIN_FORWARD_DIAG
* @see #SQUARES
* @see #DIAMONDS
*
* @param fp fill pattern (set to 1 to fill w/foreground color)
*/
public void setFillPattern(short fp)
{
format.setAdtlFillPattern(fp);
}
/**
* get the fill pattern (??) - set to 1 to fill with foreground color
* @return fill pattern
*/
public short getFillPattern()
{
return format.getAdtlFillPattern();
}
/**
* Checks if the background and foreground fills are set correctly when one
* or the other is set to the default color.
* <p>Works like the logic table below:</p>
* <p>BACKGROUND FOREGROUND</p>
* <p>NONE AUTOMATIC</p>
* <p>0x41 0x40</p>
* <p>NONE RED/ANYTHING</p>
* <p>0x40 0xSOMETHING</p>
*/
private void checkDefaultBackgroundFills() {
if (format.getFillForeground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) {
//JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt
//to me now.... But experience has shown that when the fore is set to AUTOMATIC then the
//background needs to be incremented......
if (format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1))
setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1));
} else if (format.getFillBackground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)
//Now if the forground changes to a non-AUTOMATIC color the background resets itself!!!
if (format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index)
setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index);
}
/**
* set the background fill color.
* <p>
* For example:
* <pre>
* cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
* cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
* </pre>
* optionally a Foreground and background fill can be applied:
* <i>Note: Ensure Foreground color is set prior to background</i>
* <pre>
* cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
* cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
* cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
* </pre>
* or, for the special case of SOLID_FILL:
* <pre>
* cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
* cs.setFillForegroundColor(new HSSFColor.RED().getIndex());
* </pre>
* It is necessary to set the fill style in order
* for the color to be shown in the cell.
*
* @param bg color
*/
public void setFillBackgroundColor(short bg)
{
format.setFillBackground(bg);
checkDefaultBackgroundFills();
}
/**
* get the background fill color
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @return fill color
*/
public short getFillBackgroundColor()
{
short result = format.getFillBackground();
//JMH: Do this ridiculous conversion, and let HSSFCellStyle
//internally migrate back and forth
if (result == (HSSFColor.AUTOMATIC.index+1))
return HSSFColor.AUTOMATIC.index;
else return result;
}
/**
* set the foreground fill color
* <i>Note: Ensure Foreground color is set prior to background color.</i>
* @param bg color
*/
public void setFillForegroundColor(short bg)
{
format.setFillForeground(bg);
checkDefaultBackgroundFills();
}
/**
* get the foreground fill color
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @return fill color
*/
public short getFillForegroundColor()
{
return format.getFillForeground();
}
/**
* Verifies that this style belongs to the supplied Workbook.
* 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 IllegalArgumentException if there's a workbook mis-match
*/
public void verifyBelongsToWorkbook(HSSFWorkbook wb) {
if(wb.getWorkbook() != workbook) {
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
}
}
/**
* Clones all the style information from another
* HSSFCellStyle, onto this one. This
* HSSFCellStyle will then have all the same
* properties as the source, but the two may
* be edited independently.
* Any stylings on this HSSFCellStyle will be lost!
*
* The source HSSFCellStyle could be from another
* HSSFWorkbook if you like. This allows you to
* copy styles from one HSSFWorkbook to another.
*/
public void cloneStyleFrom(CellStyle source) {
if(source instanceof HSSFCellStyle) {
this.cloneStyleFrom((HSSFCellStyle)source);
}
throw new IllegalArgumentException("Can only clone from one HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
}
public void cloneStyleFrom(HSSFCellStyle source) {
// First we need to clone the extended format
// record
format.cloneStyleFrom(source.format);
// Handle matching things if we cross workbooks
if(workbook != source.workbook) {
// Then we need to clone the format string,
// and update the format record for this
short fmt = workbook.createFormat(
source.getDataFormatString()
);
setDataFormat(fmt);
// Finally we need to clone the font,
// and update the format record for this
FontRecord fr = workbook.createNewFont();
fr.cloneStyleFrom(
source.workbook.getFontRecordAt(
source.getFontIndex()
)
);
HSSFFont font = new HSSFFont(
(short)workbook.getFontIndex(fr), fr
);
setFont(font);
}
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((format == null) ? 0 : format.hashCode());
result = prime * result + index;
return result;
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (obj instanceof HSSFCellStyle) {
final HSSFCellStyle other = (HSSFCellStyle) obj;
if (format == null) {
if (other.format != null)
return false;
} else if (!format.equals(other.format))
return false;
if (index != other.index)
return false;
return true;
}
return false;
}
}
|