Java – Apache Poi How do I add a header to the beginning of every printed page?

Apache Poi How do I add a header to the beginning of every printed page?… here is a solution to the problem.

Apache Poi How do I add a header to the beginning of every printed page?

I have a dynamic project witch printed on some pages (the number of pages depends on the web content (different amounts)).

I’ve covered most of the code (with a title at the beginning of the worksheet).

But I don’t know how to add this title at the beginning of every new printed page.

My title looks like this :

enter image description here

Solution

According to your description< a href="https://support.office.com/en-us/article/repeat-specific-rows-or-columns-on-every-printed-page-0d6dac43-7ee7-4f34-8b08-ffcc8b022409" rel=" noreferrer noopener nofollow">Repeat specific rows or columns on every printed page may be what you want. In the current Apache Poi version, this is just Sheet.setRepeatingRows – Used for repeating rows.

The following code shows a complete example that creates the kind of Excel worksheet that your screenshot seems to show. It uses the latest methods from the current Apache POI 4.1.0. Because Apache Poi is in a highly developed stage, code examples quickly become obsolete.

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);
  }

}
}

Related Problems and Solutions