excel下载(使用线程)

使用线程下载excel
    @PrivilegeRequired(Privilege.USER_DOWNLOAD)
    public Response download() throws IOException {
        ContentDisposition contentDisposition = ContentDisposition.type("attachment")
                .fileName("user".concat(".csv")).build();
        final PipedOutputStream output = new PipedOutputStream();
        PipedInputStream input = new PipedInputStream(output);
        final List
   
   
    
     userList = userService.listAllUsers(appBean.getClientCode());
        Runnable writer = new Runnable() {
            @Override
            public void run() {
            //构造方法BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output, "GBK"));
                try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output, "GBK"));) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("客户姓名").append(CSV_SEPERATOR)
	                    .append("手机号").append(CSV_SEPERATOR)
	                    .append("身份证号").append(CSV_SEPERATOR)
	                    .append("客户经理").append(CSV_SEPERATOR)
	                    .append("开户行").append(CSV_SEPERATOR)
	                    .append("银行卡").append(CSV_SEPERATOR)
	                    .append("注册时间");
                    bw.append(sb.toString());
                    bw.newLine();

                    for (User user : userList) {
                        sb = new StringBuilder();
                        String uid = user.getId();
                        sb.append(user.getName() == null ? "" : user.getName()).append(CSV_SEPERATOR);
                        sb.append(user.getMobile()).append(CSV_SEPERATOR);
                        sb.append(user.getIdNumber() == null ? "" : user.getIdNumber()).append(CSV_SEPERATOR);
                        if(user.getReferralEntity() ==null || user.getReferralEntity().getEntityId()== null) {
                        	sb.append("").append(CSV_SEPERATOR);
                        }else {
                        	Employee employee =employeeBridge.findById(user.getReferralEntity().getEntityId());
                            sb.append(employee==null? "":StringUtils.isBlank(employee.getName()) ? "" : employee.getName()).append(CSV_SEPERATOR);                        	
                        }
                        List
    
    
     
      listAcc = fundAccountService.listAccountByUser(uid, false);
                        if(listAcc!=null && listAcc.size()>0) {
                        	sb.append(listAcc.get(0).getAccount().getBranch()==null? "":listAcc.get(0).getAccount().getBranch()).append(CSV_SEPERATOR);
                        	sb.append(listAcc.get(0).getAccount().getAccount()==null ? "":listAcc.get(0).getAccount().getAccount()).append(CSV_SEPERATOR);
                        } else {
                        	sb.append("").append(CSV_SEPERATOR);
                        	sb.append("").append(CSV_SEPERATOR);
                        }
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                        sb.append(user.getRegisterDate()==null? "": formatter.format(user.getRegisterDate())).append(CSV_SEPERATOR);
                        bw.append(sb.toString());
                        bw.newLine();//写入一个行分隔符。
                    }
                    bw.flush();//刷新该流中的缓冲。将缓冲数据写到目的文件中去。
                } catch (Exception ex) {
                    logger.error("Exception happened when write CSV for user list.", ex);
                }
            }
        };

        Thread thread = new Thread(writer);
        thread.start();
        return Response.ok(input, "text/csv").encoding("GBK").header("Content-Disposition", contentDisposition).build();
    }
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值