-----controller---
@GET
@Path("export")
@Produces(MediaType.APPLICATION_JSON)
public void export(@Context HttpServletResponse response, @QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime,
@QueryParam("username") String username, @QueryParam("usable") String usable
) throws ParseException, IOException {
Long startTimeStamp = DateUtil.getTimeStamp(
DateUtil.formatDate(startTime + " 00:00:00", DateUtil.DATE_FORMAT_PATTERN_DEFAULT));
Long endTimeStamp = DateUtil.getTimeStamp(
DateUtil.formatDate(endTime + " 23:59:59", DateUtil.DATE_FORMAT_PATTERN_DEFAULT));
investService.exportRepaymentRecords(response, startTimeStamp,endTimeStamp,username,usable);
}
--------------
------service-----------
/**
* 导出excel表格
*/
public void exportRepaymentRecords(HttpServletResponse response, Long startDate, Long endDate, String username, String usable) throws IOException {
List list = getList(startDate, endDate, username, usable, null, null);
response.setHeader("Content-disposition", "attachment;filename=" +
URLEncoder.encode("还款信息管理", "UTF-8") +
DateFormatUtils.format(DateUtil.getTime(startDate), "(yyyy-MM-dd - ") +
DateFormatUtils.format(DateUtil.getTime(endDate), "yyyy-MM-dd)") +
".xls");
ExportExcel exportExcel = new ExportExcel<>();
byte[] bytes = exportExcel.exportExcel(
"还款信息",
new String[]{"用户ID", "用户名", "身份证", "投资次数", "投资总额", "充值次数", "充值总额", "提现次数", "提现总额",
"可用额度", "注册时间", "首次投资时间", "最后登录地址", "渠道"},
new String[]{"userId", "userName", "cardId", "tenderCount", "tenderMoney", "rechargeCount", "rechargeMoney", "cashCount", "cashMoney",
"balance", "regTime", "firstTenderTime", "lastIp", "agent"},
list,
"yyyy-MM-dd HH:mm:ss", true
);
response.getOutputStream().write(bytes);
response.flushBuffer();
}
/*********************
* private methods
***************************/
private List getList(Long startDate, Long endDate, String username, String usable, Integer pageOffset, Integer pageSize) {
List list = investDao.excelList(startDate, endDate, username, usable, pageOffset, pageSize);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
InvestRecord record = list.get(i);
//时间
record.setRegTime(DateUtil.formatDate(DateUtil.getTime(Long.valueOf(record.getRegTime()).longValue()), "yyyy-MM-dd HH:mm:ss"));
record.setFirstTenderTime(DateUtil.formatDate(DateUtil.getTime(Long.valueOf(record.getFirstTenderTime()).longValue()), "yyyy-MM-dd HH:mm:ss"));
//渠道
if (record.getAgent() != null) {
record.setAgent(AgentEnum.convert(Integer.parseInt(record.getAgent())).getValue());
} else {
record.setAgent("未知");
}
//IP地址
String province = IpUtil.getArea(record.getLastIp()).getProvince();
String city = IpUtil.getArea(record.getLastIp()).getCity();
if (province == null && city == null) {
record.setLastIp("未知");
} else if (province != null && city == null) {
record.setLastIp(province + ",未知");
} else if (province == null && city != null) {
record.setLastIp("未知," + city);
} else {
record.setLastIp(province + "," + city);
}
//提现总额 充值总额为空
if (record.getRechargeMoney() == null) {
record.setRechargeMoney("0");
}
if (record.getCashMoney() == null) {
record.setCashMoney("0");
}
}
}
return list;
}
------------------
------Dao 返回对象需要序列化implements Serializable-----
public List excelList(Long startDate, Long endDate, String username, String usable, Integer pageOffset, Integer pageSize) {
Map map = new HashMap<>();
map.put("startDate", startDate);
map.put("endDate", endDate);
map.put("username", username);
map.put("usable", usable);
return hcdSqlSession.selectList("financial.invest.listExcel", map);
}
-------