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

Struts+iText生成Pdf报表

2010-07-08 10:55 288 查看
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());

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: