【Java】常用POI生成Excel文档设置打印样式
package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPrintSetup; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelTest { public static void main(String[] args) throws IOException { //新建文件输出流对象 FileOutputStream out = new FileOutputStream("F:/poitest.xls"); //新建workbook HSSFWorkbook wb = new HSSFWorkbook(); //新建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"); } }
//以下为转载
HSSFSheet fromsheet = wb.getSheetAt(0); //模版页 for(int num=0;num<addSheetNum;num++)//新增 { String numStr = String.valueOf(num+2); HSSFSheet newsheet = wb.createSheet("第"+numStr+"页"); //设置打印参数 newsheet.setMargin(HSSFSheet.TopMargin,fromsheet.getMargin(HSSFSheet.TopMargin));// 页边距(上) newsheet.setMargin(HSSFSheet.BottomMargin,fromsheet.getMargin(HSSFSheet.BottomMargin));// 页边距(下) newsheet.setMargin(HSSFSheet.LeftMargin,fromsheet.getMargin(HSSFSheet.LeftMargin) );// 页边距(左) newsheet.setMargin(HSSFSheet.RightMargin,fromsheet.getMargin(HSSFSheet.RightMargin));// 页边距(右 HSSFPrintSetup ps = newsheet.getPrintSetup(); ps.setLandscape(false); // 打印方向,true:横向,false:纵向(默认) ps.setVResolution((short)600); ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型 SheetFunc.copyRows(wb, 0, num+1,0 , 46, 0);//复制 wb.getSheetAt(num+1).setColumnWidth((short)0, (short)2400);//256,31.38 }