java报表制作组件itext使用

使用起来比较简单,把代码贴出来

  1. package com.yinbo.satisfy.web.struts;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.struts.action.Action;
  5. import org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.ActionMapping;
  8. import java.io.*;
  9. import com.lowagie.text.*;
  10. import com.lowagie.text.pdf.*;
  11. import com.yinbo.satisfy.service.satisfy.SatisfyManage;
  12. import com.yinbo.satisfy.service.sysmanage.BranchManage;
  13. import com.yinbo.satisfy.service.sysmanage.ParamManage;
  14. import com.yinbo.satisfy.util.CommonUtils;
  15. import com.yinbo.satisfy.vo.Branch;
  16. import com.yinbo.satisfy.vo.SysParam;
  17. import java.util.Date;
  18. import java.text.SimpleDateFormat;
  19. import java.util.List;
  20. import java.util.Iterator;
  21. import javax.servlet.ServletOutputStream;
  22. public class ReportAction extends Action {
  23.     
  24.     private SatisfyManage satisfyManage;
  25.     private BranchManage branchManage;
  26.     private ParamManage paramManage;
  27.     
  28.     /** 
  29.      * Method execute
  30.      * @param mapping
  31.      * @param form
  32.      * @param request
  33.      * @param response
  34.      * @return ActionForward
  35.      */
  36.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  37.             HttpServletRequest request, HttpServletResponse response) {
  38.         String branchId = request.getParameter("branchId");
  39.         String level = request.getParameter("level");
  40.         
  41.         try {
  42.             Document doc = new Document(PageSize.A4, 10101010);
  43.             response.setContentType("application/pdf");
  44.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  45.             PdfWriter.getInstance(doc, new FileOutputStream("c://BalanceReport.pdf"));
  46.             PdfWriter.getInstance(doc, baos);
  47.             doc.open();
  48.             BaseFont bf = BaseFont.createFont("STSong-Light""UniGB-UCS2-H",
  49.                     BaseFont.NOT_EMBEDDED);
  50.             //        BaseFont bf2 = BaseFont.createFont("c:/SIMLI.TTF",
  51.             //                                           BaseFont.IDENTITY_H,
  52.             //                                           BaseFont.EMBEDDED);
  53.             Font fontChinese = new Font(bf, 10, Font.NORMAL);
  54.             Font fontChineseHei = new Font(bf, 15, Font.BOLD);
  55.             Font fontChineseTH = new Font(bf, 10, Font.BOLD);
  56.             String strTitle = "XXX统计报表";
  57.             Paragraph ptitle = new Paragraph(
  58.                     new Chunk(strTitle, fontChineseHei));
  59.             ptitle.setAlignment(Element.ALIGN_CENTER);
  60.             ptitle.setSpacingAfter(30);
  61.             doc.add(ptitle);
  62.             
  63.             //添加表头
  64.             ///////////////////////////////////////////////////////////
  65.             PdfPTable tab = new PdfPTable(5);
  66.             //tab.setBorderWidth(1);
  67.             int width[] = {4015151515};
  68.             tab.setWidths(width); //各列宽度
  69.             //添加所在单位
  70.             Branch branch = branchManage.getBranchById(branchId);
  71.             String branchName = branch.getBranchName();
  72.             if(CommonUtils.isEmpty(branchName)) branchName = "所有机构";
  73.             
  74.             //添加级别
  75.             SysParam param = paramManage.getParam("3", level);
  76.             String paramName = param.getParamName();
  77.             if(CommonUtils.isEmpty(paramName)) paramName = "所有级别";
  78.             
  79.             //添加报表生成日期
  80.             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  81.             PdfPCell cellDate = new PdfPCell(new Phrase(
  82.                     "所在单位:" + branchName + 
  83.                     "                                      级别:" + paramName + 
  84.                     "                                      统计日期:" + df.format(new Date()),
  85.                     fontChinese));
  86.             cellDate.setColspan(5);
  87.             cellDate.setBorderWidth(0);
  88.             cellDate.setFixedHeight(20);
  89.             cellDate.setHorizontalAlignment(Element.ALIGN_CENTER);
  90.             cellDate.setVerticalAlignment(Element.ALIGN_TOP);
  91.             //tab.addCell("所在单位" + branchName  + " 统计日期:" + cellDate);
  92.             tab.addCell(cellDate);
  93.             
  94.             //
  95.             String[] titles = {"事项""满意""基本满意""不满意""说不清楚"};
  96.             for (int i = 0; i < 5; i++) {
  97.                 PdfPCell cell = new PdfPCell(new Phrase(titles[i], fontChineseTH));
  98.                 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  99.                 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  100.                 tab.addCell(cell);
  101.             }
  102.             ///////////////////////////////////////////////////////////
  103.             //添加表头结束
  104.             
  105.             List list = satisfyManage.getReportInfo(branchId, level);
  106.             for(int i=0;i<list.size();i++) {
  107.                 String values[] = (String[])list.get(i);
  108.                 for (int j = 0; j < 5; j++) {
  109.                     PdfPCell cell = new PdfPCell(new Phrase(values[j], fontChinese));
  110.                     if(j==0)
  111.                         cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  112.                     else
  113.                         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  114.                     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  115.                     tab.addCell(cell);
  116.                 }
  117.             }
  118.             
  119.             doc.add(tab);
  120.             doc.close();
  121.             ServletOutputStream out = response.getOutputStream();
  122.             baos.writeTo(out);
  123.             out.flush();
  124.         } catch (Exception e) {
  125.             e.printStackTrace();
  126.         }
  127.         return null;
  128.     }
  129.     public SatisfyManage getSatisfyManage() {
  130.         return satisfyManage;
  131.     }
  132.     public void setSatisfyManage(SatisfyManage satisfyManage) {
  133.         this.satisfyManage = satisfyManage;
  134.     }
  135.     public BranchManage getBranchManage() {
  136.         return branchManage;
  137.     }
  138.     public void setBranchManage(BranchManage branchManage) {
  139.         this.branchManage = branchManage;
  140.     }
  141.     public ParamManage getParamManage() {
  142.         return paramManage;
  143.     }
  144.     public void setParamManage(ParamManage paramManage) {
  145.         this.paramManage = paramManage;
  146.     }
  147. }

你可能感兴趣的:(java,c,String,报表)