使用线程下载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();
}