I have a dynamic project witch is printed on some pages (number of pages depends on content from web (different amount)).
I have covered most of the code (have one header at the start of the sheet)
but I don't know how to multiple this header at the start of every new printed page page.
My header look's like this:

From your description Repeat specific rows or columns on every printed page could be what you wants. In current apache poi versions this is simply Sheet.setRepeatingRows- for repeating rows.
Following code shows a complete example which creates the kind of Excel sheet, your screen shot seems to show. It uses up to date methods from current apache poi 4.1.0. Since apache poi is highly in development, code samples are outdated very fast.
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.PropertyTemplate;
class CreateExcelRowsToRepeatAtTop {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
PropertyTemplate propertyTemplate = new PropertyTemplate(); // needed for drawing borders
Sheet sheet = workbook.createSheet();
// the header rows and cells
String[][] headers = new String[][] {
new String[] {"XRace", "Track Race", "Track 1", "Track 2", "Track 3", "Track 4"},
new String[] {"", "", "Best lap 1:23", "Best lap 2:02", "Best lap 3:30", "Best lap 6:22"},
};
int[] columnWidths = new int[] {15*256, 30*256, 15*256, 15*256, 15*256, 15*256};
for (int r = 0; r < headers.length; r++) {
Row row = sheet.createRow(r);
for (int c = 0; c < headers[0].length; c++) {
Cell cell = row.createCell(c);
cell.setCellValue(headers[r][c]);
}
}
for (int c = 0; c < headers[0].length; c++) {
sheet.setColumnWidth(c, columnWidths[c]);
}
// add merged regions as needed
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
// set alignments
for (int c = 0; c < headers[0].length; c++) {
CellUtil.setAlignment(CellUtil.getCell(CellUtil.getRow(0, sheet), c), HorizontalAlignment.CENTER);
CellUtil.setVerticalAlignment(CellUtil.getCell(CellUtil.getRow(0, sheet), c), VerticalAlignment.CENTER);
CellUtil.setAlignment(CellUtil.getCell(CellUtil.getRow(1, sheet), c), HorizontalAlignment.CENTER);
CellUtil.setVerticalAlignment(CellUtil.getCell(CellUtil.getRow(1, sheet), c), VerticalAlignment.CENTER);
}
// set higher row 2
CellUtil.getRow(1, sheet).setHeight((short)1000);
// draw borders
propertyTemplate.drawBorders(new CellRangeAddress(0, 1, 0, headers[0].length-1), BorderStyle.MEDIUM, BorderExtent.ALL);
sheet.createFreezePane(2,2); // freeze pane on screen
sheet.setRepeatingRows(CellRangeAddress.valueOf("1:2")); // set repeating rows for printing
// set print setup; fit all columns to one page width
sheet.setAutobreaks(true);
sheet.setFitToPage(true);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setFitHeight((short)0);
printSetup.setFitWidth((short)1);
// set table data rows
Object[][] data = new Object[1000][6];
for (int r = 0 ; r < data.length; r++) {
data[r] = new Object[] {r+1d, "Race " + (r+1), 12d*(r+1), 23d*(r+1), 34d*(r+1), 45d*(r+1)};
}
for (int r = 0; r < data.length; r++) {
Row row = sheet.createRow(r+2);
for (int c = 0; c < data[0].length; c++) {
Cell cell = row.createCell(c);
if (data[r][c] instanceof Double) {
cell.setCellValue((Double)data[r][c]);
} else if (data[r][c] instanceof String) {
cell.setCellValue((String)data[r][c]);
}
}
}
// draw borders
propertyTemplate.drawBorders(new CellRangeAddress(2, 2+data.length-1, 0, data[0].length-1), BorderStyle.THIN, BorderExtent.ALL);
// apply drawed borders to sheet
propertyTemplate.applyBorders(sheet);
workbook.write(fileout);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With