POI Excel 11 工作簿放大,设置页眉页脚

@author YHC

设置工作簿放大显示比例:

 设置工作簿的放大是用分数表示的,如下例子将显示比例调为75%,3是分子,4是分母;

             //创建Excel
		Workbook wb = new HSSFWorkbook();
		//创建工作簿
	    Sheet sheet1 = wb.createSheet("new sheet");
	    //设置工作簿放大
	    sheet1.setZoom(3,4);   // 75 percent magnification
	    //
	    // 写入文件
	    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
	    wb.write( fileOut );
	    fileOut.close(); 

效果图:


设置页眉页脚:

下面的事例是设置页眉的,但也可以直接用来设置页脚

                   //创建Excel
		   Workbook wb = new HSSFWorkbook();
		   //创建工作簿
		    Sheet sheet = wb.createSheet("new sheet");
                  //创建页眉
		    Header header = sheet.getHeader();
		    //自定义页眉,并设置页眉 左中右显示信息
		    header.setCenter("Center Header");
		    header.setLeft("Left Header");
		    header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
		                    HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
		    
                     //写入文件
		    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
		    wb.write(fileOut);
		    fileOut.close();
注意:运行代码之后,是空白的页面,那么如何看是否设置有效咧:

打开你的Excel文件->文件->页面设置->Tab选择页眉页脚->点击自定义页眉:




  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
poi-tl 是一个基于 Apache POI 库的 Java 模板引擎,可以用于生成 Word、Excel、PowerPoint 等文件。如果你想要在 Word 中添加页眉页脚,可以通过 poi-tl 实现。 首先,需要在 Word 模板中设置页眉页脚的样式。然后,在 Java 代码中使用 poi-tl 加载 Word 模板,替换模板中的变量,最后输出 Word 文件。 以下是一个示例代码,可以将页眉中的变量 `${header}` 替换为 "这是页眉",将页脚中的变量 `${footer}` 替换为 "这是页脚": ```java XWPFTemplate template = XWPFTemplate.compile("template.docx").render( new HashMap<String, Object>() {{ put("header", "这是页眉"); put("footer", "这是页脚"); }} ); // 设置页眉页脚 CTSectPr sectPr = template.getDocument().getDocument().getBody().getSectPr(); if (sectPr == null) { sectPr = template.getDocument().getDocument().getBody().addNewSectPr(); } CTHeaderFooterPolicy policy = sectPr.addNewPgMar().addNewHeaderFooter(); policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, new XWPFParagraph(new XWPFRun(), template.getDocument())); policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, new XWPFParagraph(new XWPFRun(), template.getDocument())); policy.getHeader().getParagraphs().get(0).createRun().setText("${header}"); policy.getFooter().getParagraphs().get(0).createRun().setText("${footer}"); template.write(new FileOutputStream("output.docx")); template.close(); ``` 注意,以上代码仅供参考,具体实现方式可能因版本变化而略有不同,建议查阅 poi-tl 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值