java poi打印_【Java】常用POI生成Excel文档设置打印样式

packagepoi_test;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFFont;importorg.apache.poi.hssf.usermodel.HSSFPrintSetup;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;public classExcelTest {public static void main(String[] args) throwsIOException {//新建文件输出流对象

FileOutputStream out = new FileOutputStream("F:/poitest.xls");//新建workbook

HSSFWorkbook wb = newHSSFWorkbook();//新建sheet

HSSFSheet sheet =wb.createSheet();//新建行

HSSFRow row = sheet.createRow(2);//设置行高

row.setHeightInPoints(20);//新建单元格

HSSFCell cell = row.createCell(2);//创建整个文本的字体对象,workbook创建

HSSFFont cnFont =wb.createFont();//设置字体行高,字体名字

cnFont.setFontHeightInPoints((short)10);

cnFont.setFontName("隶书");//将文本字面格式用到单元格上,新建单元格风格

HSSFCellStyle cnStyle =wb.createCellStyle();

cnStyle.setFont(cnFont);

cell.setCellStyle(cnStyle);//单元格内文本对象新建,HSSFRichTextString的应用

HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");

cell.setCellValue(richText);//再建一个单元格,重复上面的设置

HSSFCell enCell = row.createCell(3);

HSSFFont enFont=wb.createFont();

enFont.setFontHeightInPoints((short) 10);

enFont.setFontName("Arial Black");

HSSFCellStyle enStyle=wb.createCellStyle();

enStyle.setFont(enFont);

enCell.setCellStyle(enStyle);

enCell.setCellValue(new HSSFRichTextString("English font test"));

sheet.setColumnWidth(2, 4000);

sheet.setColumnWidth(3, 4000);//输出//设置边框

sheet.setDisplayGridlines(false);//设置打印的边框

sheet.setPrintGridlines(false);//设置打印对象

HSSFPrintSetup printSetup =sheet.getPrintSetup();//设置页边距

printSetup.setHeaderMargin((double) 0.44); //页眉

printSetup.setFooterMargin((double) 0.2);//页脚//设置页宽

printSetup.setFitWidth((short)1);

printSetup.setFitHeight((short)1000);//设置打印方向,横向就是true

printSetup.setLandscape(true);//设置A4纸

printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//打印,关闭流

wb.write(out);

out.close();

System.out.println("ok");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值