Apache poi - print layout, more than one print area in the same sheet

纵然是瞬间 提交于 2019-12-08 02:47:23

问题


I'm trying to develop a complex report, and I need to set up the print areas for the excel file. I must divide the xls file in 3 part, but if I do setPrintArea(..) the new area subscribe the old and the result is that I have in the print preview only the last page. How can I set more than one print area? This is the code:

protected void createCustomerSheet(String label) {
    createSheet(label);
    getCurrentSheet().getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);
    getCurrentSheet().getPrintSetup().setFitHeight((short)1);
    getCurrentSheet().getPrintSetup().setFitWidth((short)1);
    getCurrentSheet().setAutobreaks(true);
    getCurrentSheet().setFitToPage(true);
}

then I call 3 times

 wb.setPrintArea(activeSheetIndex, startColumn, endColumn, startRow, endRow);

I also tried to add break rows, but it doesn't work..

Any ideas?


回答1:


Excel maintains only one print area for a spreadsheet. So Apache POI's Excel API provides the ability to set one print area.

It sounds like you might be trying to define different pages of a report. If so, you'll need to set row and/or column breaks in each Sheet in which you want this done. Use the following methods of Sheet, assuming sheet is your Sheet instance:

sheet.setAutobreaks(false);
sheet.setRowBreak(rowIndex);
sheet.setColumnBreak(columnIndex);

You may call each of those last 2 methods multiple times to establish multiple breaks.




回答2:


You can set multiple print ranges like this:

try (final Workbook wb = new HSSFWorkbook(new FileInputStream("in.xls"))) {
    wb.setPrintArea(0,  "$E$6:$F$12,$H$16:$I$25,$J$18:$L$26");
    wb.write(new FileOutputStream("out.xls"));
}



回答3:


This is how I set the print area for my sheets.

// set print area
workbook.setPrintArea(
    workbook.getSheetIndex(sheet.getSheetName()), //sheet index
    0, //start column
    x, //end column
    0, //start row
    i  //end row
);


来源:https://stackoverflow.com/questions/14899293/apache-poi-print-layout-more-than-one-print-area-in-the-same-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!