Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 1727c18

Browse files
authored
Merge pull request #16 from vpday/master
✨ to specify the style of a column by condition.
2 parents c065d13 + 5642187 commit 1727c18

File tree

3 files changed

+98
-20
lines changed

3 files changed

+98
-20
lines changed

src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,15 @@
2020
import io.github.biezhi.excel.plus.exception.ExcelException;
2121
import io.github.biezhi.excel.plus.utils.ExcelUtils;
2222
import io.github.biezhi.excel.plus.utils.Pair;
23-
2423
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25-
import org.apache.poi.ss.usermodel.Cell;
26-
import org.apache.poi.ss.usermodel.CellStyle;
27-
import org.apache.poi.ss.usermodel.Row;
28-
import org.apache.poi.ss.usermodel.Sheet;
29-
import org.apache.poi.ss.usermodel.Workbook;
30-
import org.apache.poi.ss.usermodel.WorkbookFactory;
24+
import org.apache.poi.ss.usermodel.*;
3125
import org.apache.poi.ss.util.CellRangeAddress;
3226
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
3327

3428
import java.io.InputStream;
3529
import java.io.OutputStream;
36-
import java.math.BigDecimal;
37-
import java.util.Collection;
38-
import java.util.Comparator;
39-
import java.util.Iterator;
40-
import java.util.List;
30+
import java.util.*;
31+
import java.util.function.Predicate;
4132
import java.util.stream.Collectors;
4233

4334
/**
@@ -68,6 +59,8 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
6859
CellStyle headerStyle;
6960
CellStyle columnStyle = null;
7061
CellStyle titleStyle;
62+
List<CellStyle> specialColumnStyles = new ArrayList<>();
63+
List<Predicate<String>> specialConditions = new ArrayList<>();
7164

7265
T data0 = data.iterator().next();
7366
// Set Excel header
@@ -110,6 +103,11 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
110103
columnStyle = this.defaultColumnStyle(workbook);
111104
}
112105

106+
if (null != exporter.getSpecialColumn()) {
107+
exporter.getSpecialColumn().values().forEach(var -> specialColumnStyles.add(var.apply(workbook)));
108+
specialConditions = new ArrayList<>(exporter.getSpecialColumn().keySet());
109+
}
110+
113111
String headerTitle = exporter.getHeaderTitle();
114112
int colIndex = 0;
115113
if (null != headerTitle) {
@@ -123,7 +121,7 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
123121

124122
}
125123

126-
this.writeRows(sheet, columnStyle, null, iterator, startRow, columnIndexes);
124+
this.writeRows(sheet, columnStyle, specialColumnStyles, null, specialConditions, iterator, startRow, columnIndexes);
127125

128126
workbook.write(outputStream);
129127
outputStream.flush();
@@ -171,24 +169,45 @@ default void writeColumnNames(int rowIndex, CellStyle headerStyle, Sheet sheet,
171169
*
172170
* @param sheet work sheet
173171
* @param columnStyle each column style in the row.
172+
* @param specialColumnStyles each special column style in the row.
174173
* @param rowStyle row style
174+
* @param specialConditions the judgment conditions for a special column
175175
* @param iterator row data iterator
176176
* @param startRow from the beginning of the line, the default is 1
177177
* @param <T> Java Type
178178
*/
179-
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyle, Iterator<T> iterator, int startRow, List<Integer> columnIndexes) {
179+
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, List<CellStyle> specialColumnStyles, CellStyle rowStyle, List<Predicate<String>> specialConditions, Iterator<T> iterator, int startRow, List<Integer> columnIndexes) {
180180
for (int rowNum = startRow; iterator.hasNext(); rowNum++) {
181181
T item = iterator.next();
182182
Row row = sheet.createRow(rowNum);
183183
if (null != rowStyle) {
184184
row.setRowStyle(rowStyle);
185185
}
186+
187+
boolean isSpecialColumn = false;
188+
int index = -1;
189+
if (null != specialColumnStyles && null != specialConditions) {
190+
here:
191+
for (Integer col : columnIndexes) {
192+
String value = ExcelUtils.getColumnValue(item, col);
193+
for (int i = 0, length = specialConditions.size(); i < length; i++) {
194+
index = i;
195+
isSpecialColumn = specialConditions.get(i).test(value);
196+
if (isSpecialColumn) {
197+
break here;
198+
}
199+
}
200+
}
201+
}
202+
186203
Iterator<Integer> colIt = columnIndexes.iterator();
187204
while (colIt.hasNext()) {
188205
int col = colIt.next();
189206
Cell cell = row.createCell(col);
190207
String value = ExcelUtils.getColumnValue(item, col);
191-
if (null != columnStyle) {
208+
if (isSpecialColumn) {
209+
cell.setCellStyle(specialColumnStyles.get(index));
210+
} else if (null != columnStyle) {
192211
cell.setCellStyle(columnStyle);
193212
}
194213
if (null != value) {

src/main/java/io/github/biezhi/excel/plus/writer/Exporter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2424

2525
import java.util.Collection;
26+
import java.util.Map;
27+
import java.util.concurrent.ConcurrentHashMap;
2628
import java.util.function.Function;
29+
import java.util.function.Predicate;
2730

2831
/**
2932
* Excel exporter
@@ -45,6 +48,7 @@ public class Exporter<T> {
4548
private Function<Workbook, CellStyle> titleStyle;
4649
private Function<Workbook, CellStyle> headerStyle;
4750
private Function<Workbook, CellStyle> columnStyle;
51+
private Map<Predicate<String>,Function<Workbook, CellStyle>> specialColumn = new ConcurrentHashMap<>(16);
4852

4953
public static <T> Exporter<T> create(Collection<T> data) {
5054
Exporter<T> exporter = new Exporter<>();
@@ -77,6 +81,11 @@ public Exporter<T> columnStyle(Function<Workbook, CellStyle> function) {
7781
return this;
7882
}
7983

84+
public Exporter<T> specialColumn(Map<Predicate<String>,Function<Workbook, CellStyle>> specialColumn) {
85+
this.specialColumn.putAll(specialColumn);
86+
return this;
87+
}
88+
8089
public Exporter<T> byTemplate(String templatePath) {
8190
this.templatePath = templatePath;
8291
return this;
@@ -103,6 +112,10 @@ public Function<Workbook, CellStyle> getColumnStyle() {
103112
return columnStyle;
104113
}
105114

115+
public Map<Predicate<String>, Function<Workbook, CellStyle>> getSpecialColumn() {
116+
return specialColumn;
117+
}
118+
106119
public String getTemplatePath() {
107120
return templatePath;
108121
}

src/test/java/io/github/biezhi/excel/plus/ExcelPlusTest.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package io.github.biezhi.excel.plus;
22

3+
import io.github.biezhi.excel.plus.enums.ExcelType;
34
import io.github.biezhi.excel.plus.enums.ParseType;
45
import io.github.biezhi.excel.plus.exception.ExcelException;
5-
import io.github.biezhi.excel.plus.exception.ParseException;
66
import io.github.biezhi.excel.plus.model.CardSecret;
77
import io.github.biezhi.excel.plus.reader.Reader;
88
import io.github.biezhi.excel.plus.writer.Exporter;
99
import io.github.biezhi.excel.plus.writer.ResponseWrapper;
1010
import org.apache.poi.hssf.util.HSSFColor;
11-
import org.apache.poi.ss.usermodel.CellStyle;
12-
import org.apache.poi.ss.usermodel.FillPatternType;
13-
import org.apache.poi.ss.usermodel.Font;
14-
import org.apache.poi.ss.usermodel.HorizontalAlignment;
11+
import org.apache.poi.ss.usermodel.*;
12+
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
13+
import org.apache.poi.xssf.usermodel.XSSFColor;
1514
import org.junit.Assert;
1615
import org.junit.Test;
1716

@@ -21,7 +20,11 @@
2120
import java.net.URLDecoder;
2221
import java.util.ArrayList;
2322
import java.util.List;
23+
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.concurrent.ConcurrentHashMap;
26+
import java.util.function.Function;
27+
import java.util.function.Predicate;
2528
import java.util.stream.Collectors;
2629

2730
/**
@@ -144,6 +147,49 @@ public void testExportByTpl() throws ExcelException {
144147
).writeAsFile(new File("template_rows.xls"));
145148
}
146149

150+
@Test
151+
public void testExportBySpecials() throws ExcelException {
152+
List<CardSecret> cardSecrets = this.buildCardSecrets();
153+
154+
Map<Predicate<String>, Function<Workbook, CellStyle>> specialColumn = new ConcurrentHashMap<>(3);
155+
specialColumn.put(var -> var.equals("vlfdzepjmlz2y43z7er4"), webHook -> {
156+
XSSFCellStyle cellStyle = (XSSFCellStyle) webHook.createCellStyle();
157+
cellStyle.setAlignment(HorizontalAlignment.LEFT);
158+
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
159+
cellStyle.setWrapText(true);
160+
XSSFColor xssfColor = new XSSFColor();
161+
xssfColor.setRGB(new byte[]{(byte) 252, (byte) 228, (byte) 236});
162+
cellStyle.setFillForegroundColor(xssfColor);
163+
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
164+
Font font = webHook.createFont();
165+
font.setFontName("Arial");
166+
font.setFontHeightInPoints((short) 12);
167+
cellStyle.setFont(font);
168+
return cellStyle;
169+
});
170+
specialColumn.put(var -> var.equals("rasefq2rzotsmx526z6g"), webHook -> {
171+
XSSFCellStyle cellStyle = (XSSFCellStyle) webHook.createCellStyle();
172+
cellStyle.setAlignment(HorizontalAlignment.LEFT);
173+
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
174+
cellStyle.setWrapText(true);
175+
XSSFColor xssfColor = new XSSFColor();
176+
xssfColor.setRGB(new byte[]{(byte) 255, (byte) 235, (byte) 238});
177+
cellStyle.setFillForegroundColor(xssfColor);
178+
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
179+
Font font = webHook.createFont();
180+
font.setFontName("Arial");
181+
font.setFontHeightInPoints((short) 13);
182+
cellStyle.setFont(font);
183+
return cellStyle;
184+
});
185+
186+
Exporter<CardSecret> exporter = Exporter.create(cardSecrets)
187+
.sheetName("卡密列表第一季数据").specialColumn(specialColumn);
188+
exporter.setExcelType(ExcelType.XLSX);
189+
190+
excelPlus.export(exporter).writeAsFile(new File("export.xlsx"));
191+
}
192+
147193
// @Test
148194
public void testDownload() throws ExcelException {
149195
List<CardSecret> cardSecrets = this.buildCardSecrets();

0 commit comments

Comments
 (0)