您的位置:首页 > 编程语言 > Java开发

struts实现poi导出成excel

2013-10-29 16:52 344 查看
public String export() throws Exception {

  

  int i=12;

  int j=1;

  

  if ("per".equals(ddlCustClassId)) {

   status = 1;

  } else if ("com".equals(ddlCustClassId)) {

   status = 0;

  } else {

   status = 2;

  }

  List exportList = withdrowAuditBiz.getexportList(userName, status);     //获得List数据
  

  if(null!=exportList&&exportList.size()>0){

   

   response.setHeader("Content-Disposition",

   "attachment;filename=%e6%8f%90%e7%8e%b0%e5%ae%a1%e6%a0%b8%e8%a1%a8.xls");

   response.setContentType("application/ vnd.ms-excel;charset=utf-8");// 设置导出类型为excel

   response.setCharacterEncoding("utf-8");

   HSSFWorkbook wb = new HSSFWorkbook();

   HSSFSheet sheet1 = wb.createSheet("sheet1");

   

   sheet1.setColumnWidth(0, 3000);

   sheet1.setColumnWidth(1, 4000);

   sheet1.setColumnWidth(2, 4000);

   sheet1.setColumnWidth(3, 10000);

   sheet1.setColumnWidth(4, 4000);

   sheet1.setColumnWidth(5, 10000);

   sheet1.setColumnWidth(6, 4000);

   sheet1.setColumnWidth(7, 4000);

   sheet1.setColumnWidth(8, 4000);

   sheet1.setColumnWidth(9, 4000);

   sheet1.setColumnWidth(10, 6000);

   sheet1.setColumnWidth(11, 3000);

   // 创建字体样式

   HSSFFont font = wb.createFont();

   font.setFontName("Verdana");

   font.setBoldweight((short) 100);

   font.setFontHeight((short) 300);

   font.setColor(HSSFColor.BLUE.index);

   

   // 创建单元格样式

   HSSFCellStyle style = wb.createCellStyle();

   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

   style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);

//   style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

   style.setFont(font);// 设置字体

   

   // 创建Excel的sheet的一行

         HSSFRow row = sheet1.createRow(0);

         row.setHeight((short) 500);// 设定行的高度

        

         // 创建一个Excel的单元格

         HSSFCell cell = row.createCell((short) 0);

         // 给Excel的单元格设置样式和赋值

         cell.setCellStyle(style);

         cell.setCellValue("序号");

        

         HSSFCell cell1 = row.createCell((short) 1);

         cell1.setCellStyle(style);

         cell1.setCellValue("客户名称");

   

         HSSFCell cell2 = row.createCell((short) 2);

         cell2.setCellStyle(style);

         cell2.setCellValue("真实名称");

        

         HSSFCell cell3 = row.createCell((short) 3);

         cell3.setCellStyle(style);

         cell3.setCellValue("提现账号");

        

         HSSFCell cell4 = row.createCell((short) 4);

         cell4.setCellStyle(style);

         cell4.setCellValue("提现银行");

        

         HSSFCell cell5 = row.createCell((short) 5);

         cell5.setCellStyle(style);

         cell5.setCellValue("支行");

        

         HSSFCell cell6 = row.createCell((short) 6);

         cell6.setCellStyle(style);

         cell6.setCellValue("提现总额");

        

         HSSFCell cell7 = row.createCell((short) 7);

         // 给Excel的单元格设置样式和赋值

         cell7.setCellStyle(style);

         cell7.setCellValue("到账金额");

        

         HSSFCell cell8 = row.createCell((short) 8);

         cell8.setCellStyle(style);

         cell8.setCellValue("手续费");

   

         HSSFCell cell9 = row.createCell((short) 9);

         cell9.setCellStyle(style);

         cell9.setCellValue("红包抵扣");

        

         HSSFCell cell10 = row.createCell((short) 10);

         cell10.setCellStyle(style);

         cell10.setCellValue("提现时间");

        

         HSSFCell cell11 = row.createCell((short) 11);

         cell11.setCellStyle(style);

         cell11.setCellValue("状态");

        

        

        

       

   Iterator iterator = exportList.iterator();

   while(iterator.hasNext()) {

    Object [] objects=(Object[])iterator.next();

    UUserInfo uUserInfo =(UUserInfo) objects[0];

    UWithdrawCash uWithdrowCash = (UWithdrawCash) objects[1];

    UBankAccount uBankAccount = (UBankAccount) objects[2];

    style.setFillForegroundColor(HSSFColor.WHITE.index);

    HSSFRow rows = sheet1.createRow(j);

          rows.setHeight((short) 500);// 设定行的高度

         

         

         

          HSSFCell a = rows.createCell((short) 0);

        

          a.setCellStyle(style);

          a.setCellValue(j);

          j++;

         

          if(null!=uUserInfo.getUserName()){

           HSSFCell cells2 = rows.createCell((short) 1);

           cells2.setCellStyle(style);

           cells2.setCellValue(uUserInfo.getUserName());

          }else{

           HSSFCell cells2 = rows.createCell((short) 1);

           cells2.setCellStyle(style);

           cells2.setCellValue("");

          }

         

          if(null!=uUserInfo.getRealName()){

           HSSFCell cells3 = rows.createCell((short) 2);

           cells3.setCellStyle(style);

           cells3.setCellValue(uUserInfo.getRealName());

          }else{

           HSSFCell cells3 = rows.createCell((short) 2);

           cells3.setCellStyle(style);

           cells3.setCellValue("");

          }

         

          if(null!=uBankAccount.getBankAccount()){

           HSSFCell cells4 = rows.createCell((short) 3);

           cells4.setCellStyle(style);

           cells4.setCellValue(uBankAccount.getBankAccount());

          }else{

           HSSFCell cells4 = rows.createCell((short) 3);

           cells4.setCellStyle(style);

           cells4.setCellValue("");

          }

         

          if(null!=uBankAccount.getOpenBank()){

           HSSFCell cells5 = rows.createCell((short) 4);

           cells5.setCellStyle(style);

           cells5.setCellValue(uBankAccount.getOpenBank());

          }else{

           HSSFCell cells5 = rows.createCell((short) 4);

           cells5.setCellStyle(style);

           cells5.setCellValue("");

          }

          if(null!=uBankAccount.getOpenBankName()){

           HSSFCell cells6 = rows.createCell((short) 5);

           cells6.setCellStyle(style);

           cells6.setCellValue(uBankAccount.getOpenBankName());

          }else{

           HSSFCell cells6 = rows.createCell((short) 5);

           cells6.setCellStyle(style);

           cells6.setCellValue("");

          }

         

         

          if(null!=uWithdrowCash.getWithdrawTotal()){

           HSSFCell cells7 = rows.createCell((short) 6);

           cells7.setCellStyle(style);

           cells7.setCellValue(uWithdrowCash.getWithdrawTotal());

          }else{

           HSSFCell cells7 = rows.createCell((short) 6);

           cells7.setCellStyle(style);

           cells7.setCellValue("");

          }

         

          if(null!=uWithdrowCash.getToAccountMoney()){

           HSSFCell cells8 = rows.createCell((short) 7);

           cells8.setCellStyle(style);

           cells8.setCellValue(uWithdrowCash.getToAccountMoney());

          }else{

           HSSFCell cells8 = rows.createCell((short) 7);

           cells8.setCellStyle(style);

           cells8.setCellValue("");

          }

         

         

          if(null!=uWithdrowCash.getPoundageFee()){

           HSSFCell cells9 = rows.createCell((short) 8);

           cells9.setCellStyle(style);

           cells9.setCellValue(uWithdrowCash.getPoundageFee());

          }else{

           HSSFCell cells9 = rows.createCell((short) 8);

           cells9.setCellStyle(style);

           cells9.setCellValue("");

          }

          if(null!=uWithdrowCash.getRedDeduction()){

           HSSFCell cells10 = rows.cr
b8bb
eateCell((short) 9);

           cells10.setCellStyle(style);

           cells10.setCellValue(uWithdrowCash.getRedDeduction());

          }else{

           HSSFCell cells10 = rows.createCell((short) 9);

           cells10.setCellStyle(style);

           cells10.setCellValue("");

          }

         

          if(null!=uWithdrowCash.getWithdrawTime()){

           HSSFCell cells11 = rows.createCell((short) 10);

           cells11.setCellStyle(style);

           cells11.setCellValue(uWithdrowCash.getWithdrawTime());

          }else{

           HSSFCell cells11 = rows.createCell((short) 10);

           cells11.setCellStyle(style);

           cells11.setCellValue("");

          }

         

         

          if(null!=uWithdrowCash.getStatus()){

           if(uWithdrowCash.getStatus()==1){

            HSSFCell cells12 = rows.createCell((short) 11);

            cells12.setCellStyle(style);

            cells12.setCellValue("通过");

           }else{

            HSSFCell cells12 = rows.createCell((short) 11);

            cells12.setCellStyle(style);

            cells12.setCellValue("未通过");

            

           }

          }else{

           HSSFCell cells12 = rows.createCell((short) 11);

           cells12.setCellStyle(style);

           cells12.setCellValue("");

          }

         

         

         

   }

   

   

   try {

    wb.write(response.getOutputStream());

    response.getOutputStream().flush();

    response.getOutputStream().close();

   } catch (IOException e) {

    e.printStackTrace();

    System.out.println("Output   is   closed ");

   }

   

  }

  return null;     //此处一定要return null!!
 }

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poi struts