3
3
import io .github .biezhi .excel .plus .enums .ExcelType ;
4
4
import io .github .biezhi .excel .plus .exception .ExcelException ;
5
5
import io .github .biezhi .excel .plus .utils .ExcelUtils ;
6
+ import io .github .biezhi .excel .plus .utils .Pair ;
6
7
import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
7
8
import org .apache .poi .hssf .util .HSSFColor ;
8
9
import org .apache .poi .ss .usermodel .*;
13
14
import java .util .Collection ;
14
15
import java .util .Iterator ;
15
16
import java .util .List ;
17
+ import java .util .stream .Collectors ;
16
18
17
19
/**
18
20
* Exporter interface
@@ -25,10 +27,10 @@ public interface ExcelWriter {
25
27
/**
26
28
* Default Export method
27
29
*
28
- * @param exporter
29
- * @param outputStream
30
- * @param <T>
31
- * @throws ExcelException
30
+ * @param exporter Exporter Object
31
+ * @param outputStream OutputStream
32
+ * @param <T> Java Type
33
+ * @throws ExcelException thrown when exporting Excel to an exception
32
34
*/
33
35
default <T > void export (Exporter <T > exporter , OutputStream outputStream ) throws ExcelException {
34
36
Collection <T > data = exporter .getData ();
@@ -41,14 +43,14 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
41
43
Workbook workbook ;
42
44
CellStyle headerStyle ;
43
45
CellStyle columnStyle = null ;
44
- CellStyle rowStyle = null ;
45
46
46
47
T data0 = data .iterator ().next ();
47
48
// Set Excel header
48
- Iterator <T > iterator = data .iterator ();
49
- List <String > fieldNames = ExcelUtils .getWriteFieldNames (data0 .getClass ());
50
- int cols = fieldNames .size ();
51
- int startRow = exporter .startRow ();
49
+ Iterator <T > iterator = data .iterator ();
50
+
51
+ List <Pair <Integer , String >> writeFieldNames = ExcelUtils .getWriteFieldNames (data0 .getClass ());
52
+
53
+ int startRow = exporter .startRow ();
52
54
53
55
if (null != exporter .getTemplatePath ()) {
54
56
InputStream in = ExcelWriter .class .getClassLoader ().getResourceAsStream (exporter .getTemplatePath ());
@@ -63,17 +65,18 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
63
65
} else {
64
66
headerStyle = defaultHeaderStyle (workbook );
65
67
}
66
-
67
68
if (null != exporter .getColumnStyle ()) {
68
69
columnStyle = exporter .getColumnStyle ().apply (workbook );
69
70
} else {
70
71
columnStyle = defaultColumnStyle (workbook );
71
72
}
72
73
73
- this .writeRowHead (headerStyle , sheet , fieldNames , cols );
74
+ this .writeRowHead (headerStyle , sheet , writeFieldNames );
74
75
}
75
76
76
- this .writeRows (sheet , columnStyle , rowStyle , iterator , cols , startRow );
77
+ List <Integer > columnIndexes = writeFieldNames .stream ().map (Pair ::getK ).collect (Collectors .toList ());
78
+
79
+ this .writeRows (sheet , columnStyle , null , iterator , startRow , columnIndexes );
77
80
78
81
workbook .write (outputStream );
79
82
outputStream .flush ();
@@ -83,26 +86,46 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
83
86
}
84
87
}
85
88
86
- default void writeRowHead (CellStyle headerStyle , Sheet sheet , List <String > fieldNames , int cols ) {
89
+ /**
90
+ * Write the header row to Sheet.
91
+ *
92
+ * @param headerStyle header row cell style
93
+ * @param sheet work sheet
94
+ * @param columnNames column names
95
+ */
96
+ default void writeRowHead (CellStyle headerStyle , Sheet sheet , List <Pair <Integer , String >> columnNames ) {
87
97
Row rowHead = sheet .createRow (0 );
88
- for (int col = 0 ; col < cols ; col ++) {
89
- Cell cell = rowHead .createCell (col );
98
+ columnNames .forEach (pair -> {
99
+ Integer colIndex = pair .getK ();
100
+ String columnName = pair .getV ();
101
+ Cell cell = rowHead .createCell (colIndex );
90
102
if (null != headerStyle ) {
91
103
cell .setCellStyle (headerStyle );
92
104
}
93
- cell .setCellValue (fieldNames .get (col ));
94
- sheet .autoSizeColumn (col );
95
- }
105
+ cell .setCellValue (columnName );
106
+ });
96
107
}
97
108
98
- default <T > void writeRows (Sheet sheet , CellStyle columnStyle , CellStyle rowStyle , Iterator <T > iterator , int cols , int startRow ) {
109
+ /**
110
+ * Write line data
111
+ *
112
+ * @param sheet work sheet
113
+ * @param columnStyle each column style in the row.
114
+ * @param rowStyle row style
115
+ * @param iterator row data iterator
116
+ * @param startRow from the beginning of the line, the default is 1
117
+ * @param <T> Java Type
118
+ */
119
+ default <T > void writeRows (Sheet sheet , CellStyle columnStyle , CellStyle rowStyle , Iterator <T > iterator , int startRow , List <Integer > columnIndexes ) {
99
120
for (int rowNum = startRow ; iterator .hasNext (); rowNum ++) {
100
121
T item = iterator .next ();
101
122
Row row = sheet .createRow (rowNum );
102
123
if (null != rowStyle ) {
103
124
row .setRowStyle (rowStyle );
104
125
}
105
- for (int col = 0 ; col < cols ; col ++) {
126
+ Iterator <Integer > colIt = columnIndexes .iterator ();
127
+ while (colIt .hasNext ()) {
128
+ int col = colIt .next ();
106
129
Cell cell = row .createCell (col );
107
130
String value = ExcelUtils .getColumnValue (item , col );
108
131
if (null != columnStyle ) {
@@ -117,8 +140,8 @@ default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyl
117
140
/**
118
141
* The default Excel header style.
119
142
*
120
- * @param workbook
121
- * @return
143
+ * @param workbook Excel workbook
144
+ * @return header row cell style
122
145
*/
123
146
default CellStyle defaultHeaderStyle (Workbook workbook ) {
124
147
CellStyle headerStyle = workbook .createCellStyle ();
@@ -143,8 +166,8 @@ default CellStyle defaultHeaderStyle(Workbook workbook) {
143
166
/**
144
167
* The default Excel column style.
145
168
*
146
- * @param workbook
147
- * @return
169
+ * @param workbook Excel workbook
170
+ * @return row column cell style
148
171
*/
149
172
default CellStyle defaultColumnStyle (Workbook workbook ) {
150
173
CellStyle cellStyle = workbook .createCellStyle ();
@@ -164,9 +187,9 @@ default CellStyle defaultColumnStyle(Workbook workbook) {
164
187
/**
165
188
* Export excel
166
189
*
167
- * @param exporter
168
- * @param <T>
169
- * @throws ExcelException
190
+ * @param exporter Exporter Object
191
+ * @param <T> Java Type
192
+ * @throws ExcelException thrown when exporting Excel to an exception
170
193
*/
171
194
<T > void export (Exporter <T > exporter ) throws ExcelException ;
172
195
0 commit comments