1
1
package io .github .biezhi .excel .plus .writer ;
2
2
3
+ import io .github .biezhi .excel .plus .Constant ;
3
4
import io .github .biezhi .excel .plus .enums .ExcelType ;
4
5
import io .github .biezhi .excel .plus .exception .ExcelException ;
5
6
import io .github .biezhi .excel .plus .utils .ExcelUtils ;
6
7
import io .github .biezhi .excel .plus .utils .Pair ;
7
8
import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
8
- import org .apache .poi .hssf .util .HSSFColor ;
9
9
import org .apache .poi .ss .usermodel .*;
10
+ import org .apache .poi .ss .util .CellRangeAddress ;
10
11
import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
11
12
12
13
import java .io .InputStream ;
13
14
import java .io .OutputStream ;
14
15
import java .util .Collection ;
16
+ import java .util .Comparator ;
15
17
import java .util .Iterator ;
16
18
import java .util .List ;
17
19
import java .util .stream .Collectors ;
22
24
* @author biezhi
23
25
* @date 2018/2/4
24
26
*/
25
- public interface ExcelWriter {
27
+ public interface ExcelWriter extends Constant {
26
28
27
29
/**
28
30
* Default Export method
@@ -43,38 +45,56 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
43
45
Workbook workbook ;
44
46
CellStyle headerStyle ;
45
47
CellStyle columnStyle = null ;
48
+ CellStyle titleStyle = null ;
46
49
47
50
T data0 = data .iterator ().next ();
48
51
// Set Excel header
49
52
Iterator <T > iterator = data .iterator ();
50
53
51
54
List <Pair <Integer , String >> writeFieldNames = ExcelUtils .getWriteFieldNames (data0 .getClass ());
52
55
56
+ List <Integer > columnIndexes = writeFieldNames .stream ().map (Pair ::getK ).collect (Collectors .toList ());
57
+
53
58
int startRow = exporter .startRow ();
54
59
55
60
if (null != exporter .getTemplatePath ()) {
56
61
InputStream in = ExcelWriter .class .getClassLoader ().getResourceAsStream (exporter .getTemplatePath ());
57
62
workbook = WorkbookFactory .create (in );
58
63
sheet = workbook .getSheetAt (0 );
64
+
59
65
} else {
60
66
workbook = exporter .getExcelType ().equals (ExcelType .XLSX ) ? new XSSFWorkbook () : new HSSFWorkbook ();
61
67
sheet = workbook .createSheet (ExcelUtils .getSheetName (data0 ));
62
68
69
+ if (null != exporter .getTitleStyle ()) {
70
+ titleStyle = exporter .getTitleStyle ().apply (workbook );
71
+ } else {
72
+ titleStyle = this .defaultTitleStyle (workbook );
73
+ }
74
+
63
75
if (null != exporter .getHeaderStyle ()) {
64
76
headerStyle = exporter .getHeaderStyle ().apply (workbook );
65
77
} else {
66
- headerStyle = defaultHeaderStyle (workbook );
78
+ headerStyle = this . defaultHeaderStyle (workbook );
67
79
}
80
+
68
81
if (null != exporter .getColumnStyle ()) {
69
82
columnStyle = exporter .getColumnStyle ().apply (workbook );
70
83
} else {
71
- columnStyle = defaultColumnStyle (workbook );
84
+ columnStyle = this . defaultColumnStyle (workbook );
72
85
}
73
86
74
- this .writeRowHead (headerStyle , sheet , writeFieldNames );
75
- }
87
+ String headerTitle = exporter .getHeaderTitle ();
88
+ int colIndex = 0 ;
89
+ if (null != headerTitle ) {
90
+ colIndex = 1 ;
91
+ int maxColIndex = columnIndexes .stream ().max (Comparator .comparingInt (Integer ::intValue )).get ();
92
+ this .writeTitleRow (titleStyle , sheet , headerTitle , maxColIndex );
93
+ }
94
+ this .writeColumnNames (colIndex , headerStyle , sheet , writeFieldNames );
95
+ startRow += colIndex ;
76
96
77
- List < Integer > columnIndexes = writeFieldNames . stream (). map ( Pair :: getK ). collect ( Collectors . toList ());
97
+ }
78
98
79
99
this .writeRows (sheet , columnStyle , null , iterator , startRow , columnIndexes );
80
100
@@ -86,15 +106,28 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
86
106
}
87
107
}
88
108
109
+ default void writeTitleRow (CellStyle cellStyle , Sheet sheet , String title , int maxColIndex ) {
110
+ Row titleRow = sheet .createRow (0 );
111
+ for (int i = 0 ; i <= maxColIndex ; i ++) {
112
+ Cell cell = titleRow .createCell (i );
113
+ if (i == 0 ) {
114
+ cell .setCellValue (title );
115
+ }
116
+ cell .setCellStyle (cellStyle );
117
+ }
118
+ sheet .addMergedRegion (new CellRangeAddress (0 , 0 , 0 , maxColIndex ));
119
+ }
120
+
89
121
/**
90
122
* Write the header row to Sheet.
91
123
*
124
+ * @param rowIndex start row index
92
125
* @param headerStyle header row cell style
93
126
* @param sheet work sheet
94
127
* @param columnNames column names
95
128
*/
96
- default void writeRowHead ( CellStyle headerStyle , Sheet sheet , List <Pair <Integer , String >> columnNames ) {
97
- Row rowHead = sheet .createRow (0 );
129
+ default void writeColumnNames ( int rowIndex , CellStyle headerStyle , Sheet sheet , List <Pair <Integer , String >> columnNames ) {
130
+ Row rowHead = sheet .createRow (rowIndex );
98
131
columnNames .forEach (pair -> {
99
132
Integer colIndex = pair .getK ();
100
133
String columnName = pair .getV ();
@@ -137,53 +170,6 @@ default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyl
137
170
}
138
171
}
139
172
140
- /**
141
- * The default Excel header style.
142
- *
143
- * @param workbook Excel workbook
144
- * @return header row cell style
145
- */
146
- default CellStyle defaultHeaderStyle (Workbook workbook ) {
147
- CellStyle headerStyle = workbook .createCellStyle ();
148
-
149
- headerStyle .setAlignment (HorizontalAlignment .CENTER );
150
- headerStyle .setVerticalAlignment (VerticalAlignment .CENTER );
151
- headerStyle .setBorderTop (BorderStyle .THIN );
152
- headerStyle .setBorderRight (BorderStyle .THIN );
153
- headerStyle .setBorderBottom (BorderStyle .THIN );
154
- headerStyle .setBorderLeft (BorderStyle .THIN );
155
-
156
- headerStyle .setFillForegroundColor (HSSFColor .HSSFColorPredefined .WHITE .getIndex ());
157
- headerStyle .setFillPattern (FillPatternType .SOLID_FOREGROUND );
158
-
159
- Font headerFont = workbook .createFont ();
160
- headerFont .setFontHeightInPoints ((short ) 12 );
161
- headerFont .setBold (true );
162
- headerStyle .setFont (headerFont );
163
- return headerStyle ;
164
- }
165
-
166
- /**
167
- * The default Excel column style.
168
- *
169
- * @param workbook Excel workbook
170
- * @return row column cell style
171
- */
172
- default CellStyle defaultColumnStyle (Workbook workbook ) {
173
- CellStyle cellStyle = workbook .createCellStyle ();
174
- cellStyle .setAlignment (HorizontalAlignment .CENTER );
175
- cellStyle .setVerticalAlignment (VerticalAlignment .CENTER );
176
- cellStyle .setBorderTop (BorderStyle .THIN );
177
- cellStyle .setBorderRight (BorderStyle .THIN );
178
- cellStyle .setBorderBottom (BorderStyle .THIN );
179
- cellStyle .setBorderLeft (BorderStyle .THIN );
180
- cellStyle .setWrapText (true );
181
-
182
- Font cellFont = workbook .createFont ();
183
- cellStyle .setFont (cellFont );
184
- return cellStyle ;
185
- }
186
-
187
173
/**
188
174
* Export excel
189
175
*
0 commit comments