java导出excel

需要jar:

jxl.jar  下载地址:jxl.jar

List authlist = queryList();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {

   // 2.生成工作簿
   WritableWorkbook wb = Workbook.createWorkbook(baos);
   // 3.生成工作表
   WritableSheet sheet = wb.createSheet("交易结果记录查询", 0);
   //统计
   Label label = new Label(0, 0, "交易验证总笔数");
   sheet.addCell(label);
   label = new Label(1, 0, String.valueOf(authlist.size()));
   sheet.addCell(label);
   label = new Label(2, 0, "成功笔数");
   sheet.addCell(label);
   label = new Label(3, 0, String.valueOf(request.getAttribute("success")));
   sheet.addCell(label);
   label = new Label(4, 0, "失败笔数");
   sheet.addCell(label);
   label = new Label(5, 0,String.valueOf( request.getAttribute("failure")));
   sheet.addCell(label);
   //标题
    label = new Label(0, 1, "交易时间");
   sheet.addCell(label);
   label = new Label(1, 1, "交易种类");
   sheet.addCell(label);
   label = new Label(2, 1, "交易号");
   sheet.addCell(label);
   label = new Label(3, 1, "商户代码");
   sheet.addCell(label);
   label = new Label(4, 1, "VCN虚拟卡号");
   sheet.addCell(label);
   label = new Label(5, 1, "交易结果");
   sheet.addCell(label);
   
   List totalList = new ArrayList();
   // 封装数据
   for (int i = 0; i < authlist.size(); i++) {// 行
    AuthTransactionMessageData auth = (AuthTransactionMessageData) authlist
      .get(i);
    int tmpresult = auth.getResult();
    String result = "";
    if(tmpresult==4){
     result="成功";
    }
    else{
     result="失败";
    }
    //每列的集合
    List list = new ArrayList();
  
    list.add(auth.getAuthTime().toString());
    list.add("支付验证");
    list.add(auth.getTradeRecordID());
    list.add(auth.getMerID());
    list.add(auth.getPan());
    list.add(result);
    totalList.add(list);
   }
   
   for (int i = 0; i < totalList.size(); i++) {
    List list = (List) totalList.get(i);
    for (int j = 0; j < list.size(); j++) {
     label = new Label(j, i + 2, list.get(j).toString());
     sheet.addCell(label);
    }
   }

   wb.write();
   wb.close();
   byte[] ba = baos.toByteArray();
   ByteArrayInputStream bais = new ByteArrayInputStream(ba);

   String filename = "历史交易记录查询结果"+(new SimpleDateFormat("yyyy-MM-dd")).format(new Date()) + ".xls";

//解决下载文件的名称的中文乱码问题
   response.setHeader("Content-disposition", "attachment; filename="
     + new String(filename.getBytes("gb2312"), "iso8859-1"));
   int b;
   while ((b = bais.read()) != -1) {
    response.getOutputStream().write(b);
   }
   response.getOutputStream().flush();
   if (bais != null)
    bais.close();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (RowsExceededException e) {
   e.printStackTrace();
  } catch (WriteException e) {
   e.printStackTrace();
  } finally {
   if (baos != null) {
    try {
     baos.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值