Detect needed print orientation with Apache POI

冷暖自知 提交于 2020-01-22 18:23:53

问题


I'm using Apache POI to create xls spreadsheets. Is there a way to detect if the data fits in portrait mode or if I have to set the sheet to landscape mode? I know how to set the modes, but I don't know how to find out if the data fits the current print orientation.


回答1:


I've tried but cant see a way to get this working.

When you create the workbook, poi defaults the Fit Height and Width to 1 each.

Fit Height is the number of pages high to fit sheet in

and

Fit Width is the number of pages wide to fit sheet in

Unless you set the sheet print height and width to a higher value like so,

sheet.getPrintSetup().setFitHeight((short)10);

System.out.println (sheet.getPrintSetup().getFitWidth());
System.out.println (sheet.getPrintSetup().getFitHeight());

always return 1 and 1

The problem is Excel will always compress the data (in zoom size) down to 10% to fit the 1 x 1 page layout. [In MS_Excel, this shows up as Print Preview > Page Setup > Scaling > Down to X% of actual size ]

Once the zoom is at 10%, it then overflows the data onto page 2 and so on.

I've tried a sheet with lots of data and even sent a large PrintArea

workBook.setPrintArea(
                    0, //sheet index
                    0, //start column
                    50, //end column
                    0, //start row
                    520  //end row
            );

on a variety of print sizes.

sheet.getPrintSetup().setPaperSize((short)11); // A5 

So the default printable area / orientation are not changed unless you override them, so I dont think the data can be detected to be larger than the printable area - which is what you're trying to obtain.

This might be one for the POI mailing lists.

Updated to include link to this discussion on POI mailing lists as already asked by OP.

http://mail-archives.apache.org/mod_mbox/poi-user/201010.mbox/%3c4CBDD258.80407@openforce.com%3e




回答2:


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

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


来源:https://stackoverflow.com/questions/3976341/detect-needed-print-orientation-with-apache-poi

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