Search code examples
javajasper-reportsexport-to-excel

Set paper size in Jasper to be recognized in Excel


I have an Excel report that I generate with Jasper 3.7.5. If I preview the file the last column of the report is not shown. enter image description here

If I change the paper size in Page Setup from Letter to A4 then the last column is shown. enter image description here

When I try to print it, in the Print dialog the first option for paper size is Letter. Is it possible to set the paper size from Jasper in such a way that the Excel would recognize that it is A4 and would set the Paper Size to A4? Or is this only possible in Excel and not in Jasper? It would be nice to have the Paper Size selected to A4 both in the Print dialog and in the preview.

The report width and height (that are set up in the jrxml inside the jasperReport tag) are calculated like this:

// A4 size is 210 mm X 297 mm
int quality = 236; // 236 dpmm = 600 dpi
int width = 210 * quality / 98;
int height = 297 * quality / 98;

I do not know why the formula needs to be divided with 98. However I have noticed that setting the value above 100 has no visible effects on the report.

UPDATE: The report is in landscape mode.


Solution

  • I could not solve this in Jasper. I have solved it by manipulating the report after it is generated and before it is sent back to browser using the Apache POI API:

    Sheet sheet = workbook.getSheetAt(0);
    sheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);