使用起来比较简单,把代码贴出来
- package com.yinbo.satisfy.web.struts;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- 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 java.io.*;
- import com.lowagie.text.*;
- import com.lowagie.text.pdf.*;
- import com.yinbo.satisfy.service.satisfy.SatisfyManage;
- import com.yinbo.satisfy.service.sysmanage.BranchManage;
- import com.yinbo.satisfy.service.sysmanage.ParamManage;
- import com.yinbo.satisfy.util.CommonUtils;
- import com.yinbo.satisfy.vo.Branch;
- import com.yinbo.satisfy.vo.SysParam;
- import java.util.Date;
- import java.text.SimpleDateFormat;
- import java.util.List;
- import java.util.Iterator;
- import javax.servlet.ServletOutputStream;
- public class ReportAction extends Action {
- private SatisfyManage satisfyManage;
- private BranchManage branchManage;
- private ParamManage paramManage;
- /**
- * Method execute
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- String branchId = request.getParameter("branchId");
- String level = request.getParameter("level");
- try {
- Document doc = new Document(PageSize.A4, 10, 10, 10, 10);
- response.setContentType("application/pdf");
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PdfWriter.getInstance(doc, new FileOutputStream("c://BalanceReport.pdf"));
- PdfWriter.getInstance(doc, baos);
- doc.open();
- BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
- BaseFont.NOT_EMBEDDED);
- // BaseFont bf2 = BaseFont.createFont("c:/SIMLI.TTF",
- // BaseFont.IDENTITY_H,
- // BaseFont.EMBEDDED);
- Font fontChinese = new Font(bf, 10, Font.NORMAL);
- Font fontChineseHei = new Font(bf, 15, Font.BOLD);
- Font fontChineseTH = new Font(bf, 10, Font.BOLD);
- String strTitle = "XXX统计报表";
- Paragraph ptitle = new Paragraph(
- new Chunk(strTitle, fontChineseHei));
- ptitle.setAlignment(Element.ALIGN_CENTER);
- ptitle.setSpacingAfter(30);
- doc.add(ptitle);
- //添加表头
- ///
- PdfPTable tab = new PdfPTable(5);
- //tab.setBorderWidth(1);
- int width[] = {40, 15, 15, 15, 15};
- tab.setWidths(width); //各列宽度
- //添加所在单位
- Branch branch = branchManage.getBranchById(branchId);
- String branchName = branch.getBranchName();
- if(CommonUtils.isEmpty(branchName)) branchName = "所有机构";
- //添加级别
- SysParam param = paramManage.getParam("3", level);
- String paramName = param.getParamName();
- if(CommonUtils.isEmpty(paramName)) paramName = "所有级别";
- //添加报表生成日期
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- PdfPCell cellDate = new PdfPCell(new Phrase(
- "所在单位:" + branchName +
- " 级别:" + paramName +
- " 统计日期:" + df.format(new Date()),
- fontChinese));
- cellDate.setColspan(5);
- cellDate.setBorderWidth(0);
- cellDate.setFixedHeight(20);
- cellDate.setHorizontalAlignment(Element.ALIGN_CENTER);
- cellDate.setVerticalAlignment(Element.ALIGN_TOP);
- //tab.addCell("所在单位" + branchName + " 统计日期:" + cellDate);
- tab.addCell(cellDate);
- //
- String[] titles = {"事项", "满意", "基本满意", "不满意", "说不清楚"};
- for (int i = 0; i < 5; i++) {
- PdfPCell cell = new PdfPCell(new Phrase(titles[i], fontChineseTH));
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- tab.addCell(cell);
- }
- ///
- //添加表头结束
- List list = satisfyManage.getReportInfo(branchId, level);
- for(int i=0;i<list.size();i++) {
- String values[] = (String[])list.get(i);
- for (int j = 0; j < 5; j++) {
- PdfPCell cell = new PdfPCell(new Phrase(values[j], fontChinese));
- if(j==0)
- cell.setHorizontalAlignment(Element.ALIGN_LEFT);
- else
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- tab.addCell(cell);
- }
- }
- doc.add(tab);
- doc.close();
- ServletOutputStream out = response.getOutputStream();
- baos.writeTo(out);
- out.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public SatisfyManage getSatisfyManage() {
- return satisfyManage;
- }
- public void setSatisfyManage(SatisfyManage satisfyManage) {
- this.satisfyManage = satisfyManage;
- }
- public BranchManage getBranchManage() {
- return branchManage;
- }
- public void setBranchManage(BranchManage branchManage) {
- this.branchManage = branchManage;
- }
- public ParamManage getParamManage() {
- return paramManage;
- }
- public void setParamManage(ParamManage paramManage) {
- this.paramManage = paramManage;
- }
- }