How to setup print excel files in jasper reports

2019-08-23 13:14发布

问题:

i have problem with my excel when i print preview the files after i export the report into excel. How to setup the files when i print the files, it will automatic fix the size according the A4 paper?.

回答1:

  HSSFPrintSetup printSetup = sheet.getPrintSetup();
    sheet.getPrintSetup().setFitWidth((short) 1);
    sheet.getPrintSetup().setFitHeight((short) 0);
    sheet.setAutobreaks(true);
    printSetup.setLandscape(true);

    HSSFFooter footer = sheet.getFooter();
    footer.setCenter("Page " + HSSFFooter.page() + " of "+ HSSFFooter.numPages());


回答2:

I had this problem as well: Exporting to Excel and opening a Print preview did not show A4 but Letter, despite the report page format being configured as A4 (that is 842 * 595 pixels).

During Excel export, Jasper calculates and sets the paper size in the private final short getSuitablePaperSize() method of net.sf.jasperreports.engine.export.JRXlsExporter (as of JasperReports 6.2.0).

The calculation did not work correctly because the JRParameter.IS_IGNORE_PAGINATION flag was turned on. With this flag, Jasper considers the entire report as one single page, so page size and page width are not anymore the same as defined in the report's page format.

Leaving JRParameter.IS_IGNORE_PAGINATION at false produced the correct print preview (in A4).