Skip to content

Commit e16b68a

Browse files
authored
Merge pull request #461 from nchen000/feat/strikethrough
Feat/strikethrough
2 parents 7f37afa + 41965fa commit e16b68a

File tree

5 files changed

+77
-31
lines changed

5 files changed

+77
-31
lines changed

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Font {
3030
/**
3131
* Default font.
3232
*/
33-
public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000");
33+
public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000", false);
3434

3535
/**
3636
* Bold flag.
@@ -56,6 +56,10 @@ class Font {
5656
* RGB font color.
5757
*/
5858
private final String rgbColor;
59+
/**
60+
* Strikethrough flag.
61+
*/
62+
private final boolean strikethrough;
5963

6064
/**
6165
* Constructor.
@@ -66,8 +70,9 @@ class Font {
6670
* @param name Font name.
6771
* @param size Font size, in points.
6872
* @param rgbColor RGB font color.
73+
* @param strikethrough Strikethrough flag.
6974
*/
70-
Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor) {
75+
Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor, boolean strikethrough) {
7176
if (size.compareTo(BigDecimal.valueOf(409)) > 0 || size.compareTo(BigDecimal.valueOf(1)) < 0) {
7277
throw new IllegalStateException("Font size must be between 1 and 409 points: " + size);
7378
}
@@ -77,6 +82,7 @@ class Font {
7782
this.name = name;
7883
this.size = size.setScale(2, RoundingMode.HALF_UP);
7984
this.rgbColor = rgbColor;
85+
this.strikethrough = strikethrough;
8086
}
8187

8288
/**
@@ -88,31 +94,32 @@ class Font {
8894
* @param name Font name. Defaults to "Calibri".
8995
* @param size Font size, in points. Defaults to 11.0.
9096
* @param rgbColor RGB font color. Defaults to "FF000000".
97+
* @param strikethrough Strikethrough flag.
9198
* @return New font object.
9299
*/
93-
public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor) {
94-
return new Font(bold != null? bold : DEFAULT.bold, italic != null ? italic : DEFAULT.italic , underlined != null ? underlined : DEFAULT.underlined, name != null ? name : DEFAULT.name, size != null ? size:DEFAULT.size, rgbColor != null ? rgbColor: DEFAULT.rgbColor);
100+
public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) {
101+
return new Font(bold != null? bold : DEFAULT.bold, italic != null ? italic : DEFAULT.italic , underlined != null ? underlined : DEFAULT.underlined, name != null ? name : DEFAULT.name, size != null ? size:DEFAULT.size, rgbColor != null ? rgbColor: DEFAULT.rgbColor, strikethrough != null ? strikethrough : DEFAULT.strikethrough);
95102
}
96103

97104
@Override
98105
public int hashCode() {
99-
return Objects.hash(bold, italic, underlined, name, size, rgbColor);
106+
return Objects.hash(bold, italic, underlined, name, size, rgbColor, strikethrough);
100107
}
101108

102109
@Override
103110
public boolean equals(Object obj) {
104111
boolean result;
105112
if (obj != null && obj.getClass() == this.getClass()) {
106113
Font other = (Font) obj;
107-
result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(underlined, other.underlined) && Objects.equals(name, other.name) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor);
114+
result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(underlined, other.underlined) && Objects.equals(name, other.name) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor) && Objects.equals(strikethrough, other.strikethrough);
108115
} else {
109116
result = false;
110117
}
111118
return result;
112119
}
113120

114-
public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor) {
115-
return Objects.equals(bold, DEFAULT.bold) && Objects.equals(italic, DEFAULT.italic) && Objects.equals(underlined, DEFAULT.underlined) && Objects.equals(fontName, DEFAULT.name) && Objects.equals(fontSize, DEFAULT.size) && Objects.equals(fontColor, DEFAULT.rgbColor);
121+
public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor, Boolean strikethrough) {
122+
return Objects.equals(bold, DEFAULT.bold) && Objects.equals(italic, DEFAULT.italic) && Objects.equals(underlined, DEFAULT.underlined) && Objects.equals(fontName, DEFAULT.name) && Objects.equals(fontSize, DEFAULT.size) && Objects.equals(fontColor, DEFAULT.rgbColor) && Objects.equals(strikethrough, DEFAULT.strikethrough);
116123
}
117124

118125
/**
@@ -123,6 +130,7 @@ public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underl
123130
*/
124131
void write(Writer w) throws IOException {
125132
w.append("<font>").append(bold ? "<b/>" : "").append(italic ? "<i/>" : "").append(underlined ? "<u/>" : "").append("<sz val=\"").append(size.toString()).append("\"/>");
133+
w.append(strikethrough ? "<strike/>" : "");
126134
if (rgbColor != null) {
127135
w.append("<color rgb=\"").append(rgbColor).append("\"/>");
128136
}

fastexcel-writer/src/main/java/org/dhatim/fastexcel/GenericStyleSetter.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ abstract class GenericStyleSetter<STYLE_SETTER extends GenericStyleSetter<STYLE_
8787
* RGB font color.
8888
*/
8989
private String fontColor;
90+
/**
91+
* Strikethrough flag.
92+
*/
93+
private Boolean strikethrough;
9094
/**
9195
* Horizontal alignment.
9296
*/
@@ -250,6 +254,15 @@ public STYLE_SETTER underlined() {
250254
return getThis();
251255
}
252256

257+
/**
258+
* Use strikethrough text.
259+
* @return This style setter.
260+
*/
261+
public STYLE_SETTER strikethrough() {
262+
this.strikethrough = true;
263+
return getThis();
264+
}
265+
253266
/**
254267
* Define horizontal alignment.
255268
*
@@ -463,8 +476,8 @@ protected void setStyle(boolean shadingEnabled, Set<Integer> currentStyles,
463476
alignment = null;
464477
}
465478
Font font;
466-
if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor)) {
467-
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor);
479+
if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor, strikethrough)) {
480+
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough);
468481
} else {
469482
font = Font.DEFAULT;
470483
}
@@ -513,8 +526,8 @@ public void set(ConditionalFormattingRule conditionalFormattingRule) {
513526
alignment = new Alignment(horizontalAlignment, verticalAlignment, wrapText, rotation, indent);
514527
}
515528
Font font = null;
516-
if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null) {
517-
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor);
529+
if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null || strikethrough != null && strikethrough) {
530+
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough);
518531
}
519532
Fill fill = null;
520533
if (fillColor != null) {

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void setActiveTab(int tabIndex) {
9494
}
9595

9696
public void setGlobalDefaultFont(String fontName, double fontSize) {
97-
this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null));
97+
this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null, null));
9898
}
9999

100100
public void setGlobalDefaultFont(Font font) {

fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ void testForAllFeatures() throws Exception {
351351
.fontName("Cooper Black")
352352
.borderColor(Color.SEA_BLUE)
353353
.underlined()
354+
.strikethrough()
354355
.rotation(90)
355356
.set();
356357
//merge cells
@@ -371,6 +372,7 @@ void testForAllFeatures() throws Exception {
371372
.fontName("Cooper Black")
372373
.borderColor(Color.SEA_BLUE)
373374
.underlined()
375+
.strikethrough()
374376
.shadeAlternateRows(Color.SEA_BLUE)
375377
.shadeRows(Color.RED, 1)
376378
.set();

fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java

+41-18
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ void font() throws Exception {
211211
String bold = "Bold";
212212
String italic = "Italic";
213213
String underlined = "Underlined";
214+
String strikethrough = "Strikethrough";
214215
String bold_italic = "Bold_italic";
215216
String bold_underlined = "Bold_underlined";
217+
String bold_strikethrough = "Bold_strikethrough";
216218
String italic_underlinded = "Italic_underlined";
217-
String all_three = "All_three";
219+
String italic_strikethrough = "Italic_strikethrough";
220+
String underlined_strikethrough = "Underlined_strikethrough";
221+
String all_four = "All_four";
218222
byte[] data = writeWorkbook(wb -> {
219223
Worksheet ws = wb.newWorksheet(sheetName);
220224
ws.value(0, 0, bold);
@@ -223,14 +227,22 @@ void font() throws Exception {
223227
ws.style(0, 1).italic().set();
224228
ws.value(0, 2, underlined);
225229
ws.style(0, 2).underlined().set();
226-
ws.value(0, 3, bold_italic);
227-
ws.style(0, 3).bold().italic().set();
228-
ws.value(0, 4, bold_underlined);
229-
ws.style(0, 4).bold().underlined().set();
230-
ws.value(0, 5, italic_underlinded);
231-
ws.style(0, 5).italic().underlined().set();
232-
ws.value(0, 6, all_three);
233-
ws.style(0, 6).bold().italic().underlined().set();
230+
ws.value(0, 3, strikethrough);
231+
ws.style(0, 3).strikethrough().set();
232+
ws.value(0, 4, bold_italic);
233+
ws.style(0, 4).bold().italic().set();
234+
ws.value(0, 5, bold_underlined);
235+
ws.style(0, 5).bold().underlined().set();
236+
ws.value(0, 6, bold_strikethrough);
237+
ws.style(0, 6).bold().strikethrough().set();
238+
ws.value(0, 7, italic_underlinded);
239+
ws.style(0, 7).italic().underlined().set();
240+
ws.value(0, 8, italic_strikethrough);
241+
ws.style(0, 8).italic().strikethrough().set();
242+
ws.value(0, 9, underlined_strikethrough);
243+
ws.style(0, 9).underlined().strikethrough().set();
244+
ws.value(0, 10, all_four);
245+
ws.style(0, 10).bold().italic().underlined().strikethrough().set();
234246
try {
235247
ws.close();
236248
} catch (IOException ex) {
@@ -244,15 +256,26 @@ void font() throws Exception {
244256
assertTrue(xws.getRow(0).getCell(0).getCellStyle().getFont().getBold());
245257
assertTrue(xws.getRow(0).getCell(1).getCellStyle().getFont().getItalic());
246258
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(2).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
247-
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getBold());
248-
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getItalic());
259+
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getStrikeout());
260+
249261
assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getBold());
250-
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(4).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
251-
assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getItalic());
262+
assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getItalic());
263+
assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getBold());
252264
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(5).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
253265
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getBold());
254-
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getItalic());
255-
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(6).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
266+
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getStrikeout());
267+
assertTrue(xws.getRow(0).getCell(7).getCellStyle().getFont().getItalic());
268+
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(7).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
269+
assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getItalic());
270+
assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getStrikeout());
271+
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(9).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
272+
assertTrue(xws.getRow(0).getCell(9).getCellStyle().getFont().getStrikeout());
273+
274+
assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getBold());
275+
assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getItalic());
276+
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(10).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
277+
assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getStrikeout());
278+
256279
}
257280

258281
@Test
@@ -664,7 +687,7 @@ void hasValidNamedRange() throws Exception {
664687
// Fetch the XSSF Name object
665688
XSSFName name = xwb.getName("col names");
666689
String formula = name.getRefersToFormula();
667-
690+
668691
assertTrue(name != null);
669692
assertTrue(name.getNameName().equals("col names"));
670693
assertTrue(formula.equals("'Worksheet 1'!$A$1:$D$2"));
@@ -688,7 +711,7 @@ void hasValidCellConditionalFormatting() throws Exception {
688711
XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0);
689712
int numRanges = condFmt.getFormattingRanges().length;
690713
CellRangeAddress cellRange = condFmt.getFormattingRanges()[0];
691-
714+
692715
assertTrue(numCondFmts == 1);
693716
assertTrue(numRules == 1);
694717
assertTrue(numRanges == 1);
@@ -715,7 +738,7 @@ void hasValidRangeConditionalFormatting() throws Exception {
715738
XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0);
716739
int numRanges = condFmt.getFormattingRanges().length;
717740
CellRangeAddress cellRange = condFmt.getFormattingRanges()[0];
718-
741+
719742
assertTrue(numCondFmts == 1);
720743
assertTrue(numRules == 1);
721744
assertTrue(numRanges == 1);

0 commit comments

Comments
 (0)