java报表制作组件itext使用

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

  1. packagecom.yinbo.satisfy.web.struts;
  2. importjavax.servlet.http.HttpServletRequest;
  3. importjavax.servlet.http.HttpServletResponse;
  4. importorg.apache.struts.action.Action;
  5. importorg.apache.struts.action.ActionForm;
  6. importorg.apache.struts.action.ActionForward;
  7. importorg.apache.struts.action.ActionMapping;
  8. importjava.io.*;
  9. importcom.lowagie.text.*;
  10. importcom.lowagie.text.pdf.*;
  11. importcom.yinbo.satisfy.service.satisfy.SatisfyManage;
  12. importcom.yinbo.satisfy.service.sysmanage.BranchManage;
  13. importcom.yinbo.satisfy.service.sysmanage.ParamManage;
  14. importcom.yinbo.satisfy.util.CommonUtils;
  15. importcom.yinbo.satisfy.vo.Branch;
  16. importcom.yinbo.satisfy.vo.SysParam;
  17. importjava.util.Date;
  18. importjava.text.SimpleDateFormat;
  19. importjava.util.List;
  20. importjava.util.Iterator;
  21. importjavax.servlet.ServletOutputStream;
  22. publicclassReportActionextendsAction{
  23. privateSatisfyManagesatisfyManage;
  24. privateBranchManagebranchManage;
  25. privateParamManageparamManage;
  26. /**
  27. *Methodexecute
  28. *@parammapping
  29. *@paramform
  30. *@paramrequest
  31. *@paramresponse
  32. *@returnActionForward
  33. */
  34. publicActionForwardexecute(ActionMappingmapping,ActionFormform,
  35. HttpServletRequestrequest,HttpServletResponseresponse){
  36. StringbranchId=request.getParameter("branchId");
  37. Stringlevel=request.getParameter("level");
  38. try{
  39. Documentdoc=newDocument(PageSize.A4,10,10,10,10);
  40. response.setContentType("application/pdf");
  41. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  42. PdfWriter.getInstance(doc,newFileOutputStream("c:\\BalanceReport.pdf"));
  43. PdfWriter.getInstance(doc,baos);
  44. doc.open();
  45. BaseFontbf=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",
  46. BaseFont.NOT_EMBEDDED);
  47. //BaseFontbf2=BaseFont.createFont("c:/SIMLI.TTF",
  48. //BaseFont.IDENTITY_H,
  49. //BaseFont.EMBEDDED);
  50. FontfontChinese=newFont(bf,10,Font.NORMAL);
  51. FontfontChineseHei=newFont(bf,15,Font.BOLD);
  52. FontfontChineseTH=newFont(bf,10,Font.BOLD);
  53. StringstrTitle="XXX统计报表";
  54. Paragraphptitle=newParagraph(
  55. newChunk(strTitle,fontChineseHei));
  56. ptitle.setAlignment(Element.ALIGN_CENTER);
  57. ptitle.setSpacingAfter(30);
  58. doc.add(ptitle);
  59. //添加表头
  60. ///////////////////////////////////////////////////////////
  61. PdfPTabletab=newPdfPTable(5);
  62. //tab.setBorderWidth(1);
  63. intwidth[]={40,15,15,15,15};
  64. tab.setWidths(width);//各列宽度
  65. //添加所在单位
  66. Branchbranch=branchManage.getBranchById(branchId);
  67. StringbranchName=branch.getBranchName();
  68. if(CommonUtils.isEmpty(branchName))branchName="所有机构";
  69. //添加级别
  70. SysParamparam=paramManage.getParam("3",level);
  71. StringparamName=param.getParamName();
  72. if(CommonUtils.isEmpty(paramName))paramName="所有级别";
  73. //添加报表生成日期
  74. SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-dd");
  75. PdfPCellcellDate=newPdfPCell(newPhrase(
  76. "所在单位:"+branchName+
  77. "级别:"+paramName+
  78. "统计日期:"+df.format(newDate()),
  79. fontChinese));
  80. cellDate.setColspan(5);
  81. cellDate.setBorderWidth(0);
  82. cellDate.setFixedHeight(20);
  83. cellDate.setHorizontalAlignment(Element.ALIGN_CENTER);
  84. cellDate.setVerticalAlignment(Element.ALIGN_TOP);
  85. //tab.addCell("所在单位"+branchName+"统计日期:"+cellDate);
  86. tab.addCell(cellDate);
  87. //
  88. String[]titles={"事项","满意","基本满意","不满意","说不清楚"};
  89. for(inti=0;i<5;i++){
  90. PdfPCellcell=newPdfPCell(newPhrase(titles[i],fontChineseTH));
  91. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  92. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  93. tab.addCell(cell);
  94. }
  95. ///////////////////////////////////////////////////////////
  96. //添加表头结束
  97. Listlist=satisfyManage.getReportInfo(branchId,level);
  98. for(inti=0;i<list.size();i++){
  99. Stringvalues[]=(String[])list.get(i);
  100. for(intj=0;j<5;j++){
  101. PdfPCellcell=newPdfPCell(newPhrase(values[j],fontChinese));
  102. if(j==0)
  103. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  104. else
  105. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  106. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  107. tab.addCell(cell);
  108. }
  109. }
  110. doc.add(tab);
  111. doc.close();
  112. ServletOutputStreamout=response.getOutputStream();
  113. baos.writeTo(out);
  114. out.flush();
  115. }catch(Exceptione){
  116. e.printStackTrace();
  117. }
  118. returnnull;
  119. }
  120. publicSatisfyManagegetSatisfyManage(){
  121. returnsatisfyManage;
  122. }
  123. publicvoidsetSatisfyManage(SatisfyManagesatisfyManage){
  124. this.satisfyManage=satisfyManage;
  125. }
  126. publicBranchManagegetBranchManage(){
  127. returnbranchManage;
  128. }
  129. publicvoidsetBranchManage(BranchManagebranchManage){
  130. this.branchManage=branchManage;
  131. }
  132. publicParamManagegetParamManage(){
  133. returnparamManage;
  134. }
  135. publicvoidsetParamManage(ParamManageparamManage){
  136. this.paramManage=paramManage;
  137. }
  138. }

你可能感兴趣的:(java,apache,struts,servlet,J#)