From b84c97c6f6838041f04fa58118a43ab04e5d0f18 Mon Sep 17 00:00:00 2001 From: Nan Chen Date: Fri, 16 Aug 2024 15:25:24 -0400 Subject: [PATCH 1/3] Add support for strikethrough --- .../main/java/org/dhatim/fastexcel/Font.java | 24 ++++++++++++------- .../dhatim/fastexcel/GenericStyleSetter.java | 21 ++++++++++++---- .../java/org/dhatim/fastexcel/Workbook.java | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java index cf6f91a7..52fb8bf1 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java @@ -30,7 +30,7 @@ class Font { /** * Default font. */ - public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000"); + public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000", false); /** * Bold flag. @@ -56,6 +56,10 @@ class Font { * RGB font color. */ private final String rgbColor; + /** + * Strikethrough flag. + */ + private final Boolean strikethrough; /** * Constructor. @@ -66,8 +70,9 @@ class Font { * @param name Font name. * @param size Font size, in points. * @param rgbColor RGB font color. + * @param strikethrough Strikethrough flag. */ - Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor) { + Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) { if (size.compareTo(BigDecimal.valueOf(409)) > 0 || size.compareTo(BigDecimal.valueOf(1)) < 0) { throw new IllegalStateException("Font size must be between 1 and 409 points: " + size); } @@ -77,6 +82,7 @@ class Font { this.name = name; this.size = size.setScale(2, RoundingMode.HALF_UP); this.rgbColor = rgbColor; + this.strikethrough = strikethrough; } /** @@ -88,15 +94,16 @@ class Font { * @param name Font name. Defaults to "Calibri". * @param size Font size, in points. Defaults to 11.0. * @param rgbColor RGB font color. Defaults to "FF000000". + * @param strikethrough Strikethrough flag. * @return New font object. */ - public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor) { - 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); + public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) { + 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); } @Override public int hashCode() { - return Objects.hash(bold, italic, underlined, name, size, rgbColor); + return Objects.hash(bold, italic, underlined, name, size, rgbColor, strikethrough); } @Override @@ -104,15 +111,15 @@ public boolean equals(Object obj) { boolean result; if (obj != null && obj.getClass() == this.getClass()) { Font other = (Font) obj; - 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); + 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); } else { result = false; } return result; } - public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor) { - 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); + public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor, Boolean strikethrough) { + 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); } /** @@ -123,6 +130,7 @@ public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underl */ void write(Writer w) throws IOException { w.append("").append(bold ? "" : "").append(italic ? "" : "").append(underlined ? "" : "").append(""); + w.append(strikethrough ? "" : ""); if (rgbColor != null) { w.append(""); } diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/GenericStyleSetter.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/GenericStyleSetter.java index 6d7b5ddc..6cb0776d 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/GenericStyleSetter.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/GenericStyleSetter.java @@ -87,6 +87,10 @@ abstract class GenericStyleSetter currentStyles, alignment = null; } Font font; - if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor)) { - font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor); + if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor, strikethrough)) { + font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough); } else { font = Font.DEFAULT; } @@ -513,8 +526,8 @@ public void set(ConditionalFormattingRule conditionalFormattingRule) { alignment = new Alignment(horizontalAlignment, verticalAlignment, wrapText, rotation, indent); } Font font = null; - if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null) { - font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor); + if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null || strikethrough != null && strikethrough) { + font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough); } Fill fill = null; if (fillColor != null) { diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java index c190eb4c..1e32d753 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java @@ -94,7 +94,7 @@ public void setActiveTab(int tabIndex) { } public void setGlobalDefaultFont(String fontName, double fontSize) { - this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null)); + this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null, null)); } public void setGlobalDefaultFont(Font font) { From 8e7dd9ade85d9bd39ba7f8a247637139060ba631 Mon Sep 17 00:00:00 2001 From: Nan Chen Date: Fri, 16 Aug 2024 15:25:39 -0400 Subject: [PATCH 2/3] Update test case --- .../org/dhatim/fastexcel/CorrectnessTest.java | 2 + .../fastexcel/PoiCompatibilityTest.java | 59 +++++++++++++------ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java index c9de178f..3e4573d0 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java @@ -351,6 +351,7 @@ void testForAllFeatures() throws Exception { .fontName("Cooper Black") .borderColor(Color.SEA_BLUE) .underlined() + .strikethrough() .rotation(90) .set(); //merge cells @@ -371,6 +372,7 @@ void testForAllFeatures() throws Exception { .fontName("Cooper Black") .borderColor(Color.SEA_BLUE) .underlined() + .strikethrough() .shadeAlternateRows(Color.SEA_BLUE) .shadeRows(Color.RED, 1) .set(); diff --git a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java index c3aae429..834813cc 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java @@ -211,10 +211,14 @@ void font() throws Exception { String bold = "Bold"; String italic = "Italic"; String underlined = "Underlined"; + String strikethrough = "Strikethrough"; String bold_italic = "Bold_italic"; String bold_underlined = "Bold_underlined"; + String bold_strikethrough = "Bold_strikethrough"; String italic_underlinded = "Italic_underlined"; - String all_three = "All_three"; + String italic_strikethrough = "Italic_strikethrough"; + String underlined_strikethrough = "Underlined_strikethrough"; + String all_four = "All_four"; byte[] data = writeWorkbook(wb -> { Worksheet ws = wb.newWorksheet(sheetName); ws.value(0, 0, bold); @@ -223,14 +227,22 @@ void font() throws Exception { ws.style(0, 1).italic().set(); ws.value(0, 2, underlined); ws.style(0, 2).underlined().set(); - ws.value(0, 3, bold_italic); - ws.style(0, 3).bold().italic().set(); - ws.value(0, 4, bold_underlined); - ws.style(0, 4).bold().underlined().set(); - ws.value(0, 5, italic_underlinded); - ws.style(0, 5).italic().underlined().set(); - ws.value(0, 6, all_three); - ws.style(0, 6).bold().italic().underlined().set(); + ws.value(0, 3, strikethrough); + ws.style(0, 3).strikethrough().set(); + ws.value(0, 4, bold_italic); + ws.style(0, 4).bold().italic().set(); + ws.value(0, 5, bold_underlined); + ws.style(0, 5).bold().underlined().set(); + ws.value(0, 6, bold_strikethrough); + ws.style(0, 6).bold().strikethrough().set(); + ws.value(0, 7, italic_underlinded); + ws.style(0, 7).italic().underlined().set(); + ws.value(0, 8, italic_strikethrough); + ws.style(0, 8).italic().strikethrough().set(); + ws.value(0, 9, underlined_strikethrough); + ws.style(0, 9).underlined().strikethrough().set(); + ws.value(0, 10, all_four); + ws.style(0, 10).bold().italic().underlined().strikethrough().set(); try { ws.close(); } catch (IOException ex) { @@ -244,15 +256,26 @@ void font() throws Exception { assertTrue(xws.getRow(0).getCell(0).getCellStyle().getFont().getBold()); assertTrue(xws.getRow(0).getCell(1).getCellStyle().getFont().getItalic()); assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(2).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); - assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getBold()); - assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getItalic()); + assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getStrikeout()); + assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getBold()); - assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(4).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); - assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getItalic()); + assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getItalic()); + assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getBold()); assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(5).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getBold()); - assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getItalic()); - assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(6).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); + assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getStrikeout()); + assertTrue(xws.getRow(0).getCell(7).getCellStyle().getFont().getItalic()); + assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(7).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); + assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getItalic()); + assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getStrikeout()); + assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(9).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); + assertTrue(xws.getRow(0).getCell(9).getCellStyle().getFont().getStrikeout()); + + assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getBold()); + assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getItalic()); + assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(10).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE); + assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getStrikeout()); + } @Test @@ -664,7 +687,7 @@ void hasValidNamedRange() throws Exception { // Fetch the XSSF Name object XSSFName name = xwb.getName("col names"); String formula = name.getRefersToFormula(); - + assertTrue(name != null); assertTrue(name.getNameName().equals("col names")); assertTrue(formula.equals("'Worksheet 1'!$A$1:$D$2")); @@ -688,7 +711,7 @@ void hasValidCellConditionalFormatting() throws Exception { XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0); int numRanges = condFmt.getFormattingRanges().length; CellRangeAddress cellRange = condFmt.getFormattingRanges()[0]; - + assertTrue(numCondFmts == 1); assertTrue(numRules == 1); assertTrue(numRanges == 1); @@ -715,7 +738,7 @@ void hasValidRangeConditionalFormatting() throws Exception { XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0); int numRanges = condFmt.getFormattingRanges().length; CellRangeAddress cellRange = condFmt.getFormattingRanges()[0]; - + assertTrue(numCondFmts == 1); assertTrue(numRules == 1); assertTrue(numRanges == 1); From 41965fa8e160b9b3c0ad7897038a68bca0979327 Mon Sep 17 00:00:00 2001 From: Nan Chen Date: Thu, 29 Aug 2024 11:12:04 -0400 Subject: [PATCH 3/3] Use primitive type boolean for strikethrough flag --- fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java index 52fb8bf1..bb104b69 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java @@ -59,7 +59,7 @@ class Font { /** * Strikethrough flag. */ - private final Boolean strikethrough; + private final boolean strikethrough; /** * Constructor. @@ -72,7 +72,7 @@ class Font { * @param rgbColor RGB font color. * @param strikethrough Strikethrough flag. */ - Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) { + Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor, boolean strikethrough) { if (size.compareTo(BigDecimal.valueOf(409)) > 0 || size.compareTo(BigDecimal.valueOf(1)) < 0) { throw new IllegalStateException("Font size must be between 1 and 409 points: " + size); }