Struts+iText生成Pdf报表

Struts+iText生成Pdf报表
作者: jfish 发表日期: 2006-01-16 09:21 文章属性: 原创 复制链接


Struts+iText生成Pdf报表
1.iText简介
iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,
图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够
使你正确的控制Servlet的输出。

下载:http://www.lowagie.com/iText/download.html
中文包下载:http://itextdocs.lowagie.com/downloads/iTextAsian.jar
开发指南:http://itextdocs.lowagie.com/tutorial/

2.struts+iText开发实例
下面举例介绍一下用struts+itext开发生成pdf:
UserPdfViewAction.java
package net.pms.web.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.pms.model.PmsUser;
import net.pms.service.UserManager;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class UserPdfViewAction extends Action {
     private static final String CONTENT_TYPE = "application/pdf";

     private UserManager mgr = null;

     private PmsUser b = null;

     public void setUserManager(UserManager userManager) {
           this.mgr = userManager;
     }

     public ActionForward execute(ActionMapping mapping, ActionForm form,
                 HttpServletRequest request, HttpServletResponse response)
                 throws IOException, ServletException, DocumentException {

           response.setContentType(CONTENT_TYPE);
           BaseFont bfChinese = null;
           try {
                 bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
           } catch (DocumentException e) {
                 e.printStackTrace();
           } catch (IOException e) {
                 e.printStackTrace();
           }
           Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
           try {
                 //Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
                 //Document document = new Document(PageSize.A4, 36, 36, 36, 36);
                 Document document = new Document();
                 ByteArrayOutputStream ba = new ByteArrayOutputStream();
                 try {
                       //
                       PdfWriter.getInstance(document, ba);
                       document.open();                        
                       PdfPTable table = new PdfPTable(6);
                       table.setWidthPercentage(80f);
                       PdfPCell h1 = new PdfPCell(new Paragraph("编号", FontChinese));
                       PdfPCell h2 = new PdfPCell(new Paragraph("登陆名", FontChinese));
                       PdfPCell h3 = new PdfPCell(new Paragraph("姓名", FontChinese));
                       PdfPCell h4 = new PdfPCell(new Paragraph("部门", FontChinese));
                       PdfPCell h5 = new PdfPCell(new Paragraph("职务", FontChinese));
                       PdfPCell h6 = new PdfPCell(new Paragraph("创建时间", FontChinese));
                       table.setHeaderRows(1);
                       table.addCell(h1);
                       table.addCell(h2);
                       table.addCell(h3);
                       table.addCell(h4);
                       table.addCell(h5);
                       table.addCell(h6);
                       PdfPCell cell;
                       List list = mgr .getPmsUsers("from PmsUser p where p.active='Y'");
                       for (short i = 0; i < list.size(); i++) {
                             b = (PmsUser) list.get(i);
                             cell = new PdfPCell(new Paragraph(String.valueOf(i + 1), FontChinese));
                             table.addCell(cell);
                             cell = new PdfPCell(new Paragraph(b.getLoginname(),FontChinese));
                             table.addCell(cell);
                             cell = new PdfPCell(new Paragraph(b.getName(), FontChinese));
                             table.addCell(cell);
                             cell = new PdfPCell(new Paragraph(b.getPmsDept().getName(),FontChinese));
                             table.addCell(cell);
                             cell = new PdfPCell(new Paragraph(b.getPmsDuty().getName(),FontChinese));
                             table.addCell(cell);
                             cell = new PdfPCell(new Paragraph(b.getTs().toString().substring(0, 16), FontChinese));
                             table.addCell(cell);
                       }
                       document.add(table);

                 } catch (DocumentException de) {
                       System.err.println(de.getMessage());
                 }
                 document.close();
                 response.setContentLength(ba.size());
                 ServletOutputStream out = response.getOutputStream();
                 ba.writeTo(out);
                 out.flush();
                 out.close();
                 return null;
           } catch (Throwable e) {
                 e.printStackTrace();
           }

           return new ActionForward(mapping.getInput());

     }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值