使用起来比较简单,把代码贴出来
- packagecom.yinbo.satisfy.web.struts;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importjava.io.*;
- importcom.lowagie.text.*;
- importcom.lowagie.text.pdf.*;
- importcom.yinbo.satisfy.service.satisfy.SatisfyManage;
- importcom.yinbo.satisfy.service.sysmanage.BranchManage;
- importcom.yinbo.satisfy.service.sysmanage.ParamManage;
- importcom.yinbo.satisfy.util.CommonUtils;
- importcom.yinbo.satisfy.vo.Branch;
- importcom.yinbo.satisfy.vo.SysParam;
- importjava.util.Date;
- importjava.text.SimpleDateFormat;
- importjava.util.List;
- importjava.util.Iterator;
- importjavax.servlet.ServletOutputStream;
- publicclassReportActionextendsAction{
- privateSatisfyManagesatisfyManage;
- privateBranchManagebranchManage;
- privateParamManageparamManage;
- /**
- *Methodexecute
- *@parammapping
- *@paramform
- *@paramrequest
- *@paramresponse
- *@returnActionForward
- */
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse){
- StringbranchId=request.getParameter("branchId");
- Stringlevel=request.getParameter("level");
- try{
- Documentdoc=newDocument(PageSize.A4,10,10,10,10);
- response.setContentType("application/pdf");
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- PdfWriter.getInstance(doc,newFileOutputStream("c:\\BalanceReport.pdf"));
- PdfWriter.getInstance(doc,baos);
- doc.open();
- BaseFontbf=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",
- BaseFont.NOT_EMBEDDED);
- //BaseFontbf2=BaseFont.createFont("c:/SIMLI.TTF",
- //BaseFont.IDENTITY_H,
- //BaseFont.EMBEDDED);
- FontfontChinese=newFont(bf,10,Font.NORMAL);
- FontfontChineseHei=newFont(bf,15,Font.BOLD);
- FontfontChineseTH=newFont(bf,10,Font.BOLD);
- StringstrTitle="XXX统计报表";
- Paragraphptitle=newParagraph(
- newChunk(strTitle,fontChineseHei));
- ptitle.setAlignment(Element.ALIGN_CENTER);
- ptitle.setSpacingAfter(30);
- doc.add(ptitle);
- //添加表头
- ///////////////////////////////////////////////////////////
- PdfPTabletab=newPdfPTable(5);
- //tab.setBorderWidth(1);
- intwidth[]={40,15,15,15,15};
- tab.setWidths(width);//各列宽度
- //添加所在单位
- Branchbranch=branchManage.getBranchById(branchId);
- StringbranchName=branch.getBranchName();
- if(CommonUtils.isEmpty(branchName))branchName="所有机构";
- //添加级别
- SysParamparam=paramManage.getParam("3",level);
- StringparamName=param.getParamName();
- if(CommonUtils.isEmpty(paramName))paramName="所有级别";
- //添加报表生成日期
- SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-dd");
- PdfPCellcellDate=newPdfPCell(newPhrase(
- "所在单位:"+branchName+
- "级别:"+paramName+
- "统计日期:"+df.format(newDate()),
- 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(inti=0;i<5;i++){
- PdfPCellcell=newPdfPCell(newPhrase(titles[i],fontChineseTH));
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- tab.addCell(cell);
- }
- ///////////////////////////////////////////////////////////
- //添加表头结束
- Listlist=satisfyManage.getReportInfo(branchId,level);
- for(inti=0;i<list.size();i++){
- Stringvalues[]=(String[])list.get(i);
- for(intj=0;j<5;j++){
- PdfPCellcell=newPdfPCell(newPhrase(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();
- ServletOutputStreamout=response.getOutputStream();
- baos.writeTo(out);
- out.flush();
- }catch(Exceptione){
- e.printStackTrace();
- }
- returnnull;
- }
- publicSatisfyManagegetSatisfyManage(){
- returnsatisfyManage;
- }
- publicvoidsetSatisfyManage(SatisfyManagesatisfyManage){
- this.satisfyManage=satisfyManage;
- }
- publicBranchManagegetBranchManage(){
- returnbranchManage;
- }
- publicvoidsetBranchManage(BranchManagebranchManage){
- this.branchManage=branchManage;
- }
- publicParamManagegetParamManage(){
- returnparamManage;
- }
- publicvoidsetParamManage(ParamManageparamManage){
- this.paramManage=paramManage;
- }
- }