您的位置:首页 > 编程语言 > Java开发

使用ITEXT与JAVA生成PDF报表

2008-10-30 10:22 597 查看
        /** * 生成PDF文件并调用相应的阅读器实现打印预览 * @param listAll 待生成文件的记录 * @param response */ public void createtable(List listAll, HttpServletResponse response) throws IOException { SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm"); Date today = new Date(); String dateToday = "企业未付费订单_" + dataFormat.format(today); //定义纸张类型及方向,页边距
Document document = new Document(PageSize.A4.rotate());
                ByteArrayOutputStream ba = new ByteArrayOutputStream();  try {                   //方法1:向客户端缓存中写入临时PDF文件                                      PdfWriter.getInstance(document,ba);
                   //方法2:在服务器端的D盘生成PDF文件                    
  PdfWriter.getInstance(document, new FileOutputStream(     "d://" + dateToday +".pdf"));   document.open();   // 解决中文问题   BaseFont bfChinese = BaseFont.createFont("STSong-Light",     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   Paragraph Title = new Paragraph();   // 设置页面格式    Title.setSpacingBefore(8);    Title.setSpacingAfter(2);    Title.setAlignment(1);    // 设置PDF标题    Title.add(new Chunk("企业订单管理---企业未付费订单列表",      new com.lowagie.text.Font(bfChinese, 16,        com.lowagie.text.Font.BOLD)));
   document.add(new Paragraph("51SoftJob招聘网", new com.lowagie.text.Font(      bfChinese, 12, com.lowagie.text.Font.BOLD)));    //将标题输出到PDF文档
  document.add(Title) ;
  // ------------------------下一页是从数据库中查询的数据显示到表格中--------------------------  // document.newPage(); //新的一页
  List l = listAll;   //设置表格的列行
  Table mytable = new Table(10);   //   int WidthE[] = { 1, 2, 6, 3, 3, 4, 4, 2, 3, 2 };   //设置格子的颜色
  mytable.setWidth(100);   mytable.setWidths(WidthE);   mytable.setSpacing(0);   mytable.setDefaultHorizontalAlignment(1);   mytable.setDefaultCellBorderColor(Color.blue);   mytable.setBorderColor(Color.blue);      // 文本内容   Font FonttextChinese = new Font(bfChinese, 8, Font.NORMAL);   // 表头   Font FontheadChinese = new Font(bfChinese, 10, Font.BOLD);
  // 设置表头   Cell h1 = new Cell(new Phrase("编号", FontheadChinese ));   h1.setHorizontalAlignment(Element.ALIGN_CENTER);   h1.setBackgroundColor(Color.lightGray);      Cell h2 = new Cell(new Phrase("订单号", FontheadChinese));   h2.setHorizontalAlignment(Element.ALIGN_CENTER);   h2.setBackgroundColor(Color.lightGray);      Cell h3 = new Cell(new Phrase("企业名称", FontheadChinese));   h3.setHorizontalAlignment(Element.ALIGN_CENTER);   h3.setBackgroundColor(Color.lightGray);   Cell h4 = new Cell(new Phrase("订单类型", FontheadChinese));   h4.setHorizontalAlignment(Element.ALIGN_CENTER);   h4.setBackgroundColor(Color.lightGray);
  Cell h5 = new Cell(new Phrase("联系人", FontheadChinese));   h5.setHorizontalAlignment(Element.ALIGN_CENTER);   h5.setBackgroundColor(Color.lightGray);
  Cell h6 = new Cell(new Phrase("联系电话", FontheadChinese));   h6.setHorizontalAlignment(Element.ALIGN_CENTER);   h6.setBackgroundColor(Color.lightGray);      Cell h7 = new Cell(new Phrase("付款方式", FontheadChinese));   h7.setHorizontalAlignment(Element.ALIGN_CENTER);   h7.setBackgroundColor(Color.lightGray);      Cell h8 = new Cell(new Phrase("金额", FontheadChinese));   h8.setHorizontalAlignment(Element.ALIGN_CENTER);   h8.setBackgroundColor(Color.lightGray);      Cell h9 = new Cell(new Phrase("提交日期", FontheadChinese));   h9.setHorizontalAlignment(Element.ALIGN_CENTER);   h9.setBackgroundColor(Color.lightGray);      Cell h10 = new Cell(new Phrase("审核状态", FontheadChinese));   h10.setHorizontalAlignment(Element.ALIGN_CENTER);   h10.setBackgroundColor(Color.lightGray);
  mytable.addCell(h1);   mytable.addCell(h2);   mytable.addCell(h3);   mytable.addCell(h4);   mytable.addCell(h5);   mytable.addCell(h6);   mytable.addCell(h7);   mytable.addCell(h8);   mytable.addCell(h9);   mytable.addCell(h10);
  for (int i = 1; i <= l.size(); i++) { Map recordMap = new HashMap(); recordMap = (Map) listAll.get(i - 1);    Cell cell1 = new Cell(new Phrase(String.valueOf(i), FonttextChinese));    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);    Cell cell2 = new Cell(new Phrase(String.valueOf(recordMap.get("ORDER_ID")),      FonttextChinese));    cell2.setHorizontalAlignment(Element.ALIGN_CENTER);    Cell cell3 = new Cell(new Phrase(String.valueOf(recordMap.get("ENTERPRISE_NAME")), FonttextChinese));    cell3.setHorizontalAlignment(Element.ALIGN_CENTER);    Cell cell4 = new Cell(new Phrase(getOrderTypeName(String.valueOf(recordMap.get("ORDER_TYPE")), "1"),      FonttextChinese));    cell4.setHorizontalAlignment(Element.ALIGN_CENTER);    Cell cell5 = new Cell(new Phrase(String.valueOf(recordMap.get("CONTACT_NAME")),      FonttextChinese));    cell5.setHorizontalAlignment(Element.ALIGN_CENTER);    Cell cell6 = new Cell(new Phrase(String.valueOf(recordMap.get("CONTACT_TEL")), FonttextChinese));    cell6.setHorizontalAlignment(Element.ALIGN_CENTER);        Cell cell7 = new Cell(new Phrase(String.valueOf(recordMap.get("BANK")), FonttextChinese));    cell7.setHorizontalAlignment(Element.ALIGN_CENTER);        Cell cell8 = new Cell(new Phrase(String.valueOf(recordMap.get("PRICE")), FonttextChinese));    cell8.setHorizontalAlignment(Element.ALIGN_CENTER);        Cell cell9 = new Cell(new Phrase(getFormatTime(String.valueOf(recordMap.get("SUB_TIME"))), FonttextChinese));    cell9.setHorizontalAlignment(Element.ALIGN_CENTER);        Cell cell10 = new Cell(new Phrase(getOrderTypeName(String.valueOf(recordMap.get("AUDITING_STATE")), "2"), FonttextChinese));    cell10.setHorizontalAlignment(Element.ALIGN_CENTER);    mytable.addCell(cell1);    mytable.addCell(cell2);    mytable.addCell(cell3);    mytable.addCell(cell4);    mytable.addCell(cell5);    mytable.addCell(cell6);    mytable.addCell(cell7);    mytable.addCell(cell8);    mytable.addCell(cell9);    mytable.addCell(cell10);       }   document.add(mytable);  } catch (Exception de) {   de.printStackTrace();  }  document.close();  //方法1 好处:将PDF文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下                            //任何“遗迹”。                 //直接调用客户端的PDF阅读器打开生成的PDF文件           response.setContentType("application/pdf");           response.setContentLength(ba.size());           ServletOutputStream out = response.getOutputStream();           ba.writeTo(out);           out.flush();                                      //方法2  要服务器端生成PDF文件                 
 //设置应用程序路径及PDF文件路径  String path = "/"C://Program Files//Adobe//Reader 8.0//Reader//AcroRd32.exe/" d:/" + dateToday + ".pdf" ;    //自动打开生成的PDF文件,以便管理员打印预览      Runtime.getRuntime().exec(path);
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: