导出pdf

//pdf 导出
 public String LoginAndOutPdfOut(){
  String [] ss={"用户","登录时间","退出时间","登录IP"};
  String username=loginLog.getUserName();
  String[] uName = null;
  if(username!=null&&(!username.equals(""))){
   uName = username.split(",");
  }
  StringBuilder sBuilder=new StringBuilder();
  String getUserName="";
  if(uName!=null){
   for(int i=0;i<uName.length;i++){
    //System.out.println(uName[i].startsWith("'"));
    if(!uName[0].startsWith("'")){
     sBuilder.append("'");
     sBuilder.append(uName[i]);
     sBuilder.append("',");
     getUserName=sBuilder.toString();
     getUserName=getUserName.substring(0,getUserName.length()-1);
     loginLog.setUserName(getUserName);
    }else{
     loginLog.setUserName(username);
    }
   }
  }
  try {
   getUserName =  new String(getUserName.getBytes("ISO-8859-1"),("UTF-8"));
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String ip=loginLog.getIp();
  loginLog.setIp(ip);
  if(loginEndTime!=null&&!loginEndTime.equals("")){
   loginEndTime=getendTime(loginEndTime);
  }
  List<LoginLog> list=this.operateService.getLoginInfo(loginLog,loginStartTime,loginEndTime,logoutStartTime,logoutEndTime);
  
  HttpServletResponse response=ServletActionContext.getResponse();
  HttpServletRequest request =ServletActionContext.getRequest();
  response.setContentType( "application/pdf" );
  response.setHeader("Content-disposition",
  "attachment; filename=loginAndOutRecordReport.pdf");
   try {
   OutputStream os = response.getOutputStream();
   exportPdF("用户访问记录报表",ss,list,os);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return null;
 }  

 

 

 

  public static void exportPdF(String title,String[] headers,List<NmsEmployee>list, OutputStream out){


       Rectangle rectPageSize = new Rectangle(PageSize.A4);// 定义A4页面大小
       Document document = new Document(rectPageSize, 30, 30, 30, 30);// 其余4个参数,设置了页面的4个边距
          try {
             // 将PDF文档写出到out所接洽关系IO设备上的书写对象
             PdfWriter.getInstance(document, out);
             // 添加文档元数据信息
             document.addTitle(StrHelp.getChinese(title));
             document.addSubject("export information");
             document.addAuthor("leno");
             document.addCreator("leno");
             document.addKeywords("pdf itext");
             // 定义页头和页尾
             HeaderFooter header = new HeaderFooter(new PdfParagraph(title, 20,  true), false);
             header.setAlignment(Element.ALIGN_CENTER);
             HeaderFooter footer = new HeaderFooter(new Phrase(  "This   is   page   "), new Phrase("."));
             footer.setAlignment(Element.ALIGN_CENTER);
             document.setHeader(header);
             document.setFooter(footer);
             // 打开PDF文档
             document.open();
                            0姓名、1性别、2工号、3部门、4卡号、5类型、6手机号码、7电子邮箱、8办公电话、9家庭电话、10传真、11地址。12关联人的姓名、13部门、14手机号码
             float[] widths = {0.13f,0.07f, 0.12f, 0.12f, 0.14f, 0.12f, 0.15f,0.20f, 0.19f, 0.19f,0.19f,0.30f, 0.14f,0.14f,0.14f};
             PdfPTable table = new PdfPTable(headers.length);
             table.setWidths(widths);
             table.setWidthPercentage(100);
             // 产生表格题目行
             for (int i = 0; i < headers.length; i++) {
                PdfPCell cell = new PdfPCell(new PdfParagraph(headers[i],7, true));
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
                cell.setBackgroundColor(Color.white);
                cell.setBorderColor(Color.black);
                table.addCell(cell);
             }
        for(int i=0;i<list.size();i++){//姓名、性别、工号、部门、卡号、类型、手机号码、电子邮箱、办公电话、家庭电话、传真、地址。 关联人的姓名、部门、手机号码
          String textValue = list.get(i).getEmployeeName();//员工姓名
          if(null==textValue) textValue="";
              table.addCell(getPdfCell(textValue,6));
              String textValue1 = list.get(i).getEmployeeSex();//员工性别
              if(null==textValue1) textValue1="";
              table.addCell(getPdfCell(textValue1,6));
               String textValue2 = list.get(i).getJobNumber();//员工工号
              if(null==textValue2) textValue2="";
              table.addCell(getPdfCell(textValue2,6));
           String textValue3= "";
           if(null!=list.get(i).getNmsDpartMent()&&list.get(i).getNmsDpartMent().getDepartmentName()!=null)
            textValue3=list.get(i).getNmsDpartMent().getDepartmentName();//部门
              table.addCell(getPdfCell(textValue3,6));
              String textValue4= list.get(i).getAccessCard();//卡号
             if(null==textValue4) textValue4="";
             table.addCell(getPdfCell(textValue4,6));
             //类型
            String textValue5= list.get(i).getEmployeeType();
             if(null==textValue5) textValue5="";
             table.addCell(getPdfCell(textValue5,6));
             //手机号码
            String textValue6="";
            if(list.get(i).getMobileNumber()!=null)
             textValue6=list.get(i).getMobileNumber();
             table.addCell(getPdfCell(textValue6,6));
          //电子邮箱
            String textValue7="";
             if(list.get(i).getEmployeeEmail()!=null)
              textValue7=list.get(i).getEmployeeEmail();
              table.addCell(getPdfCell(textValue7,6));
           //办公电话
            String textValue8="";
         if(list.get(i).getOfficePhone()!=null)
          textValue8=list.get(i).getOfficePhone();
             table.addCell(getPdfCell(textValue8,6));
             //家庭电话
         String textValue9="";
         if(list.get(i).getHomePhone()!=null)
          textValue9=list.get(i).getHomePhone();
             table.addCell(getPdfCell(textValue9,6));
              //传真
          String textValue10="";
       if(list.get(i).getFax()!=null)
        textValue10=list.get(i).getFax();
           table.addCell(getPdfCell(textValue10,6));
          //地址
          String textValue11="";
           if(list.get(i).getEmployeeAddress()!=null)
            textValue11=list.get(i).getEmployeeAddress();
           PdfPCell addp=getPdfCell(textValue11,6);
              addp.setNoWrap(false);
               table.addCell( addp);
               //关联人的姓名
              String textValue12="";
               if(list.get(i).getNmsEmployee()!=null&&list.get(i).getNmsEmployee().getEmployeeName()!=null)
                textValue12=list.get(i).getNmsEmployee().getEmployeeName();
                   table.addCell(getPdfCell(textValue12,6));      
               //、部门、
                  String textValue13="";
                   if(list.get(i).getNmsEmployee()!=null&&list.get(i).getNmsEmployee().getNmsDpartMent()!=null)
                   textValue13=list.get(i).getNmsEmployee().getNmsDpartMent().getDepartmentName();
                  table.addCell(getPdfCell(textValue13,6));  
                   //手机号码
                    String textValue14="";
                   if(list.get(i).getNmsEmployee()!=null&&list.get(i).getNmsEmployee().getMobileNumber()!=null)
                   textValue14=list.get(i).getNmsEmployee().getMobileNumber();
                  table.addCell(getPdfCell(textValue14,6));
        }
             document.add(table);
             document.close();
          } catch (DocumentException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裂魂人1214

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值