20
20
import io .github .biezhi .excel .plus .exception .ExcelException ;
21
21
import io .github .biezhi .excel .plus .utils .ExcelUtils ;
22
22
import io .github .biezhi .excel .plus .utils .Pair ;
23
-
24
23
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 .*;
31
25
import org .apache .poi .ss .util .CellRangeAddress ;
32
26
import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
33
27
34
28
import java .io .InputStream ;
35
29
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 ;
41
32
import java .util .stream .Collectors ;
42
33
43
34
/**
@@ -68,6 +59,8 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
68
59
CellStyle headerStyle ;
69
60
CellStyle columnStyle = null ;
70
61
CellStyle titleStyle ;
62
+ List <CellStyle > specialColumnStyles = new ArrayList <>();
63
+ List <Predicate <String >> specialConditions = new ArrayList <>();
71
64
72
65
T data0 = data .iterator ().next ();
73
66
// Set Excel header
@@ -110,6 +103,11 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
110
103
columnStyle = this .defaultColumnStyle (workbook );
111
104
}
112
105
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
+
113
111
String headerTitle = exporter .getHeaderTitle ();
114
112
int colIndex = 0 ;
115
113
if (null != headerTitle ) {
@@ -123,7 +121,7 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
123
121
124
122
}
125
123
126
- this .writeRows (sheet , columnStyle , null , iterator , startRow , columnIndexes );
124
+ this .writeRows (sheet , columnStyle , specialColumnStyles , null , specialConditions , iterator , startRow , columnIndexes );
127
125
128
126
workbook .write (outputStream );
129
127
outputStream .flush ();
@@ -171,24 +169,45 @@ default void writeColumnNames(int rowIndex, CellStyle headerStyle, Sheet sheet,
171
169
*
172
170
* @param sheet work sheet
173
171
* @param columnStyle each column style in the row.
172
+ * @param specialColumnStyles each special column style in the row.
174
173
* @param rowStyle row style
174
+ * @param specialConditions the judgment conditions for a special column
175
175
* @param iterator row data iterator
176
176
* @param startRow from the beginning of the line, the default is 1
177
177
* @param <T> Java Type
178
178
*/
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 ) {
180
180
for (int rowNum = startRow ; iterator .hasNext (); rowNum ++) {
181
181
T item = iterator .next ();
182
182
Row row = sheet .createRow (rowNum );
183
183
if (null != rowStyle ) {
184
184
row .setRowStyle (rowStyle );
185
185
}
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
+
186
203
Iterator <Integer > colIt = columnIndexes .iterator ();
187
204
while (colIt .hasNext ()) {
188
205
int col = colIt .next ();
189
206
Cell cell = row .createCell (col );
190
207
String value = ExcelUtils .getColumnValue (item , col );
191
- if (null != columnStyle ) {
208
+ if (isSpecialColumn ) {
209
+ cell .setCellStyle (specialColumnStyles .get (index ));
210
+ } else if (null != columnStyle ) {
192
211
cell .setCellStyle (columnStyle );
193
212
}
194
213
if (null != value ) {
0 commit comments