Skip to content

Commit 2bf3402

Browse files
weihubeatstisonkun
andauthored
refactor: optimize the StyleUtil code (#466)
Co-authored-by: tison <wander4096@gmail.com>
1 parent d547c4f commit 2bf3402

File tree

3 files changed

+290
-99
lines changed

3 files changed

+290
-99
lines changed

fastexcel/src/main/java/cn/idev/excel/util/StyleUtil.java

Lines changed: 38 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cn.idev.excel.write.metadata.style.WriteCellStyle;
1010
import cn.idev.excel.write.metadata.style.WriteFont;
1111
import java.util.Optional;
12+
import java.util.function.Consumer;
1213
import lombok.extern.slf4j.Slf4j;
1314
import org.apache.commons.collections4.CollectionUtils;
1415
import org.apache.poi.common.usermodel.HyperlinkType;
@@ -54,66 +55,26 @@ public static CellStyle buildCellStyle(
5455
}
5556

5657
private static void buildCellStyle(CellStyle cellStyle, WriteCellStyle writeCellStyle) {
57-
if (writeCellStyle.getHidden() != null) {
58-
cellStyle.setHidden(writeCellStyle.getHidden());
59-
}
60-
if (writeCellStyle.getLocked() != null) {
61-
cellStyle.setLocked(writeCellStyle.getLocked());
62-
}
63-
if (writeCellStyle.getQuotePrefix() != null) {
64-
cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
65-
}
66-
if (writeCellStyle.getHorizontalAlignment() != null) {
67-
cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
68-
}
69-
if (writeCellStyle.getWrapped() != null) {
70-
cellStyle.setWrapText(writeCellStyle.getWrapped());
71-
}
72-
if (writeCellStyle.getVerticalAlignment() != null) {
73-
cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
74-
}
75-
if (writeCellStyle.getRotation() != null) {
76-
cellStyle.setRotation(writeCellStyle.getRotation());
77-
}
78-
if (writeCellStyle.getIndent() != null) {
79-
cellStyle.setIndention(writeCellStyle.getIndent());
80-
}
81-
if (writeCellStyle.getBorderLeft() != null) {
82-
cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
83-
}
84-
if (writeCellStyle.getBorderRight() != null) {
85-
cellStyle.setBorderRight(writeCellStyle.getBorderRight());
86-
}
87-
if (writeCellStyle.getBorderTop() != null) {
88-
cellStyle.setBorderTop(writeCellStyle.getBorderTop());
89-
}
90-
if (writeCellStyle.getBorderBottom() != null) {
91-
cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
92-
}
93-
if (writeCellStyle.getLeftBorderColor() != null) {
94-
cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
95-
}
96-
if (writeCellStyle.getRightBorderColor() != null) {
97-
cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
98-
}
99-
if (writeCellStyle.getTopBorderColor() != null) {
100-
cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
101-
}
102-
if (writeCellStyle.getBottomBorderColor() != null) {
103-
cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
104-
}
105-
if (writeCellStyle.getFillPatternType() != null) {
106-
cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
107-
}
108-
if (writeCellStyle.getFillBackgroundColor() != null) {
109-
cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
110-
}
111-
if (writeCellStyle.getFillForegroundColor() != null) {
112-
cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
113-
}
114-
if (writeCellStyle.getShrinkToFit() != null) {
115-
cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
116-
}
58+
setIfNotNull(cellStyle::setHidden, writeCellStyle.getHidden());
59+
setIfNotNull(cellStyle::setLocked, writeCellStyle.getLocked());
60+
setIfNotNull(cellStyle::setQuotePrefixed, writeCellStyle.getQuotePrefix());
61+
setIfNotNull(cellStyle::setAlignment, writeCellStyle.getHorizontalAlignment());
62+
setIfNotNull(cellStyle::setWrapText, writeCellStyle.getWrapped());
63+
setIfNotNull(cellStyle::setVerticalAlignment, writeCellStyle.getVerticalAlignment());
64+
setIfNotNull(cellStyle::setRotation, writeCellStyle.getRotation());
65+
setIfNotNull(cellStyle::setIndention, writeCellStyle.getIndent());
66+
setIfNotNull(cellStyle::setBorderLeft, writeCellStyle.getBorderLeft());
67+
setIfNotNull(cellStyle::setBorderRight, writeCellStyle.getBorderRight());
68+
setIfNotNull(cellStyle::setBorderTop, writeCellStyle.getBorderTop());
69+
setIfNotNull(cellStyle::setBorderBottom, writeCellStyle.getBorderBottom());
70+
setIfNotNull(cellStyle::setLeftBorderColor, writeCellStyle.getLeftBorderColor());
71+
setIfNotNull(cellStyle::setRightBorderColor, writeCellStyle.getRightBorderColor());
72+
setIfNotNull(cellStyle::setTopBorderColor, writeCellStyle.getTopBorderColor());
73+
setIfNotNull(cellStyle::setBottomBorderColor, writeCellStyle.getBottomBorderColor());
74+
setIfNotNull(cellStyle::setFillPattern, writeCellStyle.getFillPatternType());
75+
setIfNotNull(cellStyle::setFillBackgroundColor, writeCellStyle.getFillBackgroundColor());
76+
setIfNotNull(cellStyle::setFillForegroundColor, writeCellStyle.getFillForegroundColor());
77+
setIfNotNull(cellStyle::setShrinkToFit, writeCellStyle.getShrinkToFit());
11778
}
11879

11980
public static short buildDataFormat(Workbook workbook, DataFormatData dataFormatData) {
@@ -125,7 +86,7 @@ public static short buildDataFormat(Workbook workbook, DataFormatData dataFormat
12586
}
12687
if (StringUtils.isNotBlank(dataFormatData.getFormat())) {
12788
if (log.isDebugEnabled()) {
128-
log.info("create new data format:{}", dataFormatData);
89+
log.debug("create new data format:{}", dataFormatData);
12990
}
13091
DataFormat dataFormatCreate = workbook.createDataFormat();
13192
return dataFormatCreate.getFormat(dataFormatData.getFormat());
@@ -135,7 +96,7 @@ public static short buildDataFormat(Workbook workbook, DataFormatData dataFormat
13596

13697
public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) {
13798
if (log.isDebugEnabled()) {
138-
log.info("create new font:{},{}", writeFont, originFont);
99+
log.debug("create new font:{},{}", writeFont, originFont);
139100
}
140101
if (writeFont == null && originFont == null) {
141102
return null;
@@ -144,33 +105,15 @@ public static Font buildFont(Workbook workbook, Font originFont, WriteFont write
144105
if (writeFont == null || font == null) {
145106
return font;
146107
}
147-
if (writeFont.getFontName() != null) {
148-
font.setFontName(writeFont.getFontName());
149-
}
150-
if (writeFont.getFontHeightInPoints() != null) {
151-
font.setFontHeightInPoints(writeFont.getFontHeightInPoints());
152-
}
153-
if (writeFont.getItalic() != null) {
154-
font.setItalic(writeFont.getItalic());
155-
}
156-
if (writeFont.getStrikeout() != null) {
157-
font.setStrikeout(writeFont.getStrikeout());
158-
}
159-
if (writeFont.getColor() != null) {
160-
font.setColor(writeFont.getColor());
161-
}
162-
if (writeFont.getTypeOffset() != null) {
163-
font.setTypeOffset(writeFont.getTypeOffset());
164-
}
165-
if (writeFont.getUnderline() != null) {
166-
font.setUnderline(writeFont.getUnderline());
167-
}
168-
if (writeFont.getCharset() != null) {
169-
font.setCharSet(writeFont.getCharset());
170-
}
171-
if (writeFont.getBold() != null) {
172-
font.setBold(writeFont.getBold());
173-
}
108+
setIfNotNull(font::setFontName, writeFont.getFontName());
109+
setIfNotNull(font::setFontHeightInPoints, writeFont.getFontHeightInPoints());
110+
setIfNotNull(font::setItalic, writeFont.getItalic());
111+
setIfNotNull(font::setStrikeout, writeFont.getStrikeout());
112+
setIfNotNull(font::setColor, writeFont.getColor());
113+
setIfNotNull(font::setTypeOffset, writeFont.getTypeOffset());
114+
setIfNotNull(font::setUnderline, writeFont.getUnderline());
115+
setIfNotNull(font::setCharSet, writeFont.getCharset());
116+
setIfNotNull(font::setBold, writeFont.getBold());
174117
return font;
175118
}
176119

@@ -265,4 +208,10 @@ public static int getCellCoordinate(
265208
}
266209
return currentCoordinate;
267210
}
211+
212+
public static <T> void setIfNotNull(Consumer<T> setter, T value) {
213+
if (value != null) {
214+
setter.accept(value);
215+
}
216+
}
268217
}

0 commit comments

Comments
 (0)