POI - Excel 打印配置

转载链接: https://blog.csdn.net/qq_34357018/article/details/108620845

无模板时

PrintSetup ps = sheet.getPrintSetup();
// 印刷方向、true:横向、false:縦向
ps.setLandscape(true); //横向
ps.setLandscape(false); //纵向
ps.setOrientation(PrintOrientation.LANDSCAPE); // 横向 
ps.setOrientation(PrintOrientation.PORTRAIT);  // 纵向

//设置用紙
ps.setPaperSize(PrintSetup.A3_PAPERSIZE); 
//设置比率
ps.setScale(60);
//设置页边距
sheet.setMargin(Sheet.LeftMargin,( double ) 0.1 );//(左)
sheet.setMargin(Sheet.RightMargin,( double ) 0.1 );//(右)
sheet.setMargin(Sheet.BottomMargin,( double ) 0.5 );//(下)
sheet.setMargin(Sheet.TopMargin,( double ) 0.5 );//(上)
 
//启用“适合页面”打印选项的标志。(默认选择的是“将工作表调整为一页”)
sheet.setFitToPage(true);
//页眉与页脚设置
Header header = sheet.getHeader();
header.setCenter(HSSFHeader.page());//设置在中间
//header.setLeft(HSSFHeader.page());//设置在左边
//header.setRight(HSSFHeader.page());//设置在右边
 
Footer footer = sheet.getFooter();
footer.setCenter("第" + HSSFFooter.page() + "页,共 " + HSSFFooter.numPages()+"页");
 
//设置标题(实现打印时每一页都有同个头部标题)
sheet.setRepeatingColumns(CellRangeAddress columnRangeRef);
sheet.setRepeatingRows(CellRangeAddress rowRangeRef)

//设置 对齐方式
if (alignment != null){
    switch (alignment){
        case 0:
            sheet.setHorizontallyCenter(true);
            break;
        case 1:
            ps.setLeftMargin(0);
            break;
        case 2:
            ps.setRightMargin(0);
            break;
    }
    //sheet.setVerticallyCenter(true);
}

//将工作表适应为单页
//sheet.setFitToPage();

 //将所有列调整为一页
 printSetup.setFitHeight((short)0);
 sheet.setFitToPage(true);

 //将所有行调整为一页
 //printSetup.setFitWidth((short)0);
 //sheet.setFitToPage(true);

打印Page选项设置
POI - Excel 打印配置_第1张图片
有模板时

//sheet1:模板的sheet
//sheet2:要输出的sheet
public void setPrintSetupInfo(XSSFSheet sheet1, XSSFSheet sheet2) {
		XSSFPrintSetup psTemplete = sheet1.getPrintSetup();
		XSSFPrintSetup psOutput = sheet2.getPrintSetup();
 
        //页眉边距设置
		psOutput.setHeaderMargin(psTemplete.getHeaderMargin());
		psOutput.setFooterMargin(psTemplete.getFooterMargin());
        //设置宽、高页数以适合纸张
		psOutput.setFitWidth(psTemplete.getFitWidth());
		psOutput.setFitHeight(psTemplete.getFitHeight());
        //设置纸张尺寸
		psOutput.setPaperSize(psTemplete.getPaperSize());
        //设置页码开始
		psOutput.setPageStart(psTemplete.getPageStart());
        //设置是否横向打印
		psOutput.setLandscape(psTemplete.getLandscape());
        //设置是按顺序从左到右还是自上而下
		psOutput.setLeftToRight(psTemplete.getLeftToRight());
        //设置是否处于草稿模式
		psOutput.setDraft(psTemplete.getDraft());
        //设置比例
		psOutput.setScale(psTemplete.getScale());
        //页边距设置
		psOutput.setTopMargin(psTemplete.getTopMargin());
		psOutput.setBottomMargin(psTemplete.getBottomMargin());
		psOutput.setLeftMargin(psTemplete.getLeftMargin());
		psOutput.setRightMargin(psTemplete.getRightMargin());
 
		//页面打印选项设置(根据模板的选择来设置)
		outputSheet.setFitToPage(templeteSheet.getFitToPage());
		//页脚设置
		Footer psTempletefooter = templeteSheet.getFooter();
		Footer psOutputfooter = outputSheet.getFooter();
		psOutputfooter.setCenter(psTempletefooter.getCenter());
        //页眉设置省略。。。
 
        //设置标题(实现打印时每一页都有同个头部标题)
        sheet2.setRepeatingRows(sheet1.getRepeatingRows());
		sheet2.setRepeatingColumns(sheet1.getRepeatingColumns());
 
	}

官网API: https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/PrintSetup.html或
https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFPrintSetup.html

你可能感兴趣的:(java)