8
8
import org .apache .poi .ss .usermodel .*;
9
9
import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
10
10
11
- import java .io .File ;
11
+ import java .io .InputStream ;
12
12
import java .io .OutputStream ;
13
13
import java .util .Collection ;
14
14
import java .util .Iterator ;
@@ -36,58 +36,45 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
36
36
throw new ExcelException ("Export excel data is empty." );
37
37
}
38
38
try {
39
+
40
+ Sheet sheet ;
39
41
Workbook workbook ;
40
42
CellStyle headerStyle ;
41
- CellStyle columnStyle ;
42
-
43
- if (null != exporter .getTemplatePath ()) {
44
- workbook = WorkbookFactory .create (new File (exporter .getTemplatePath ()));
45
- } else {
46
- workbook = exporter .getExcelType ().equals (ExcelType .XLSX ) ? new XSSFWorkbook () : new HSSFWorkbook ();
47
- }
48
-
49
- T data0 = data .iterator ().next ();
50
- Sheet sheet = workbook .createSheet (ExcelUtils .getSheetName (data0 ));
51
-
52
- Row rowHead = sheet .createRow (0 );
43
+ CellStyle columnStyle = null ;
44
+ CellStyle rowStyle = null ;
53
45
46
+ T data0 = data .iterator ().next ();
54
47
// Set Excel header
55
48
Iterator <T > iterator = data .iterator ();
56
49
List <String > fieldNames = ExcelUtils .getWriteFieldNames (data0 .getClass ());
57
- int rows = data .size ();
58
50
int cols = fieldNames .size ();
51
+ int startRow = exporter .startRow ();
59
52
60
- if (null != exporter .getHeaderStyle ()) {
61
- headerStyle = exporter .getHeaderStyle ().apply (workbook );
62
- } else {
63
- headerStyle = defaultHeaderStyle (workbook );
64
- }
65
-
66
- if (null != exporter .getColumnStyle ()) {
67
- columnStyle = exporter .getColumnStyle ().apply (workbook );
53
+ if (null != exporter .getTemplatePath ()) {
54
+ InputStream in = ExcelWriter .class .getClassLoader ().getResourceAsStream (exporter .getTemplatePath ());
55
+ workbook = WorkbookFactory .create (in );
56
+ sheet = workbook .getSheetAt (0 );
68
57
} else {
69
- columnStyle = defaultColumnStyle ( workbook );
70
- }
58
+ workbook = exporter . getExcelType (). equals ( ExcelType . XLSX ) ? new XSSFWorkbook () : new HSSFWorkbook ( );
59
+ sheet = workbook . createSheet ( ExcelUtils . getSheetName ( data0 ));
71
60
72
- for (int col = 0 ; col < cols ; col ++) {
73
- Cell cell = rowHead .createCell (col );
74
- cell .setCellStyle (headerStyle );
75
- cell .setCellValue (fieldNames .get (col ));
76
- sheet .autoSizeColumn (col );
77
- }
61
+ if (null != exporter .getHeaderStyle ()) {
62
+ headerStyle = exporter .getHeaderStyle ().apply (workbook );
63
+ } else {
64
+ headerStyle = defaultHeaderStyle (workbook );
65
+ }
78
66
79
- for (int rowNum = 1 ; iterator .hasNext () && rowNum <= rows ; rowNum ++) {
80
- T item = iterator .next ();
81
- Row row = sheet .createRow (rowNum );
82
- for (int col = 0 ; col < cols ; col ++) {
83
- Cell cell = row .createCell (col );
84
- String value = ExcelUtils .getColumnValue (item , col );
85
- cell .setCellStyle (columnStyle );
86
- cell .setCellValue (value );
87
- sheet .autoSizeColumn (col );
67
+ if (null != exporter .getColumnStyle ()) {
68
+ columnStyle = exporter .getColumnStyle ().apply (workbook );
69
+ } else {
70
+ columnStyle = defaultColumnStyle (workbook );
88
71
}
72
+
73
+ this .writeRowHead (headerStyle , sheet , fieldNames , cols );
89
74
}
90
75
76
+ this .writeRows (sheet , columnStyle , rowStyle , iterator , cols , startRow );
77
+
91
78
workbook .write (outputStream );
92
79
outputStream .flush ();
93
80
outputStream .close ();
@@ -96,6 +83,37 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
96
83
}
97
84
}
98
85
86
+ default void writeRowHead (CellStyle headerStyle , Sheet sheet , List <String > fieldNames , int cols ) {
87
+ Row rowHead = sheet .createRow (0 );
88
+ for (int col = 0 ; col < cols ; col ++) {
89
+ Cell cell = rowHead .createCell (col );
90
+ if (null != headerStyle ) {
91
+ cell .setCellStyle (headerStyle );
92
+ }
93
+ cell .setCellValue (fieldNames .get (col ));
94
+ sheet .autoSizeColumn (col );
95
+ }
96
+ }
97
+
98
+ default <T > void writeRows (Sheet sheet , CellStyle columnStyle , CellStyle rowStyle , Iterator <T > iterator , int cols , int startRow ) {
99
+ for (int rowNum = startRow ; iterator .hasNext (); rowNum ++) {
100
+ T item = iterator .next ();
101
+ Row row = sheet .createRow (rowNum );
102
+ if (null != rowStyle ) {
103
+ row .setRowStyle (rowStyle );
104
+ }
105
+ for (int col = 0 ; col < cols ; col ++) {
106
+ Cell cell = row .createCell (col );
107
+ String value = ExcelUtils .getColumnValue (item , col );
108
+ if (null != columnStyle ) {
109
+ cell .setCellStyle (columnStyle );
110
+ }
111
+ cell .setCellValue (value );
112
+ sheet .autoSizeColumn (col );
113
+ }
114
+ }
115
+ }
116
+
99
117
/**
100
118
* The default Excel header style.
101
119
*
0 commit comments