20
20
/** */
21
21
package com .flowingcode .vaadin .addons .gridexporter ;
22
22
23
- import com .vaadin .flow .component .ComponentUtil ;
24
- import com .vaadin .flow .component .grid .ColumnTextAlign ;
25
- import com .vaadin .flow .component .grid .Grid .Column ;
26
- import com .vaadin .flow .data .binder .BeanPropertySet ;
27
- import com .vaadin .flow .data .binder .PropertySet ;
28
- import com .vaadin .flow .data .provider .DataProvider ;
29
23
import java .io .IOException ;
30
24
import java .io .InputStream ;
31
25
import java .io .PipedInputStream ;
39
33
import java .util .List ;
40
34
import java .util .stream .Collectors ;
41
35
import java .util .stream .Stream ;
42
-
43
36
import org .apache .commons .lang3 .ArrayUtils ;
44
37
import org .apache .commons .lang3 .StringUtils ;
45
38
import org .apache .commons .lang3 .tuple .Pair ;
58
51
import org .apache .poi .ss .util .CellRangeAddress ;
59
52
import org .slf4j .Logger ;
60
53
import org .slf4j .LoggerFactory ;
54
+ import com .vaadin .flow .component .ComponentUtil ;
55
+ import com .vaadin .flow .component .grid .ColumnTextAlign ;
56
+ import com .vaadin .flow .component .grid .Grid .Column ;
57
+ import com .vaadin .flow .data .binder .BeanPropertySet ;
58
+ import com .vaadin .flow .data .binder .PropertySet ;
59
+ import com .vaadin .flow .data .provider .DataProvider ;
61
60
62
61
/**
63
62
* @author mlopez
@@ -107,10 +106,17 @@ public InputStream createInputStream() {
107
106
108
107
// initialize the data range with tne coordinates of tha data placeholder cell
109
108
CellRangeAddress dataRange = new CellRangeAddress (cell .getRowIndex (), cell .getRowIndex (), cell .getColumnIndex (), cell .getColumnIndex ());
110
- fillData (sheet , cell , exporter .grid .getDataProvider (), dataRange , titleCell != null );
109
+
110
+ Sheet tempSheet = wb .cloneSheet (exporter .sheetNumber );
111
+
112
+ int lastRow = fillData (sheet , cell , exporter .grid .getDataProvider (), dataRange , titleCell != null );
111
113
112
114
applyConditionalFormattings (sheet , dataRange );
113
115
116
+ copyBottomOfSheetStartingOnRow (wb , tempSheet , sheet , cell .getRowIndex ()+1 , lastRow );
117
+
118
+ wb .removeSheetAt (exporter .sheetNumber + 1 );
119
+
114
120
cell = findCellWithPlaceHolder (sheet , exporter .footersPlaceHolder );
115
121
List <Pair <String , Column <T >>> footers = getGridFooters (exporter .grid );
116
122
if (cell != null ) {
@@ -166,6 +172,54 @@ public void run() {
166
172
return in ;
167
173
}
168
174
175
+ private void copyBottomOfSheetStartingOnRow (Workbook workbook , Sheet sourceSheet ,
176
+ Sheet targetSheet , int rowIndex , int targetRow ) {
177
+ int fRow = rowIndex ;
178
+ int lRow = sourceSheet .getLastRowNum ();
179
+ for (int iRow = fRow ; iRow <= lRow ; iRow ++) {
180
+ Row row = sourceSheet .getRow (iRow );
181
+ Row myRow = targetSheet .createRow (targetRow ++);
182
+ if (row != null ) {
183
+ short fCell = row .getFirstCellNum ();
184
+ short lCell = row .getLastCellNum ();
185
+ for (int iCell = fCell ; iCell < lCell ; iCell ++) {
186
+ Cell cell = row .getCell (iCell );
187
+ Cell newCell = myRow .createCell (iCell );
188
+ newCell .setCellStyle (cell .getCellStyle ());
189
+ if (cell != null ) {
190
+ switch (cell .getCellType ()) {
191
+ case BLANK :
192
+ newCell .setCellValue ("" );
193
+ break ;
194
+
195
+ case BOOLEAN :
196
+ newCell .setCellValue (cell .getBooleanCellValue ());
197
+ break ;
198
+
199
+ case ERROR :
200
+ newCell .setCellErrorValue (cell .getErrorCellValue ());
201
+ break ;
202
+
203
+ case FORMULA :
204
+ newCell .setCellFormula (cell .getCellFormula ());
205
+ break ;
206
+
207
+ case NUMERIC :
208
+ newCell .setCellValue (cell .getNumericCellValue ());
209
+ break ;
210
+
211
+ case STRING :
212
+ newCell .setCellValue (cell .getStringCellValue ());
213
+ break ;
214
+ default :
215
+ newCell .setCellFormula (cell .getCellFormula ());
216
+ }
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+
169
223
private void applyConditionalFormattings (Sheet sheet , CellRangeAddress targetCellRange ) {
170
224
SheetConditionalFormatting sheetCondFormatting = sheet .getSheetConditionalFormatting ();
171
225
@@ -177,7 +231,7 @@ private void applyConditionalFormattings(Sheet sheet, CellRangeAddress targetCel
177
231
178
232
}
179
233
180
- private void fillData (
234
+ private int fillData (
181
235
Sheet sheet , Cell dataCell , DataProvider <T , ?> dataProvider , CellRangeAddress dataRange , boolean titleExists ) {
182
236
Stream <T > dataStream = obtainDataStream (dataProvider );
183
237
@@ -188,11 +242,6 @@ private void fillData(
188
242
t -> {
189
243
if (notFirstRow [0 ]) {
190
244
CellStyle cellStyle = startingCell [0 ].getCellStyle ();
191
- int lastRow = sheet .getLastRowNum ();
192
- sheet .shiftRows (
193
- startingCell [0 ].getRowIndex () + (titleExists ? 1 : 0 ),
194
- lastRow ,
195
- (titleExists ? 1 : 0 ));
196
245
Row newRow = sheet .createRow (startingCell [0 ].getRowIndex () + 1 );
197
246
startingCell [0 ] = newRow .createCell (startingCell [0 ].getColumnIndex ());
198
247
startingCell [0 ].setCellStyle (cellStyle );
@@ -205,6 +254,7 @@ private void fillData(
205
254
// since we initialized the cell range with the data placeholder cell, we use
206
255
// the existing 'getLastColumn' to keep the offset of the data range
207
256
dataRange .setLastColumn (dataRange .getLastColumn () + exporter .getColumns ().size () - 1 );
257
+ return startingCell [0 ].getRowIndex ();
208
258
}
209
259
210
260
@ SuppressWarnings ("unchecked" )
0 commit comments