ServiceImpl包中代码
@Override
public void reportGoodsOrder(GoodsOrderVo goodsOrderVo, OutputStream fos) {
List<GoodsOrderBo> orderGoodsOrderBos = getGoodsOrderBoList(goodsOrderVo);//获得列表信息的方法
for(GoodsOrderBo goodsOrder:orderGoodsOrderBos){
if(goodsOrder.getStatusItype()==1){
goodsOrder.setStatusStr("待发货");
}
if(goodsOrder.getStatusItype()==2){
goodsOrder.setStatusStr("已发货");
}
if(goodsOrder.getStatusItype()==3){
goodsOrder.setStatusStr("已完成");
}//将状态从数字映射成字符串
if(goodsOrder.getPayItype()==1){
String payAmtStr = goodsOrder.getPayAmt()+"CBS";
goodsOrder.setPayAmtStr(payAmtStr);
}else{
String payAmtStr = "¥"+goodsOrder.getPayAmt();
goodsOrder.setPayAmtStr(payAmtStr);
}
}
log.info("#############orderGoodsOrderBos:{}", orderGoodsOrderBos);
try {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("goodsOrderCode", "订单号");
map.put("createDate", "下单时间");
map.put("userName", "下单人姓名");
map.put("phone", "手机号码");
map.put("payAmtStr", "订单金额");
map.put("statusStr", "状态");
map.put("orderExpressName", "物流公司");
map.put("expressCode", "物流编码");//映射到表格
CSVUtils.createExcel(orderGoodsOrderBos, map, 0, "", "", fos);
} catch (Exception e) {
e.printStackTrace();
}
}
Reource包中代码
@ResponseBody
@RequestMapping(value = "/nofilter/order", method = RequestMethod.GET)
public void getDownloadOrderList(@QueryParam("key") String key,@QueryParam("statusItype") Integer statusItype) {//获取删选条件
statusItype = statusItype == null ? 0 : statusItype;
GoodsOrderVo goodsOrderVo = GoodsOrderVo.builder().key(key).statusItype(statusItype).build();
log.info("###########goodsOrderVo:{}",goodsOrderVo);
try {
String fileName = "GoodsOrderList.xls";
OutputStream fos = null;
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "UTF-8"));
fos = response.getOutputStream();
goodsOrderService.reportGoodsOrder(goodsOrderVo, fos);;
response.getOutputStream().flush();
response.getOutputStream().close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
易错误点:列表传递参数时没有PAGESIZE参数,但是列表查询的SQL语句排序时用到PAGESIZE参数,会导致报错;
解决方法:增加判断条件,如果pageSize=0时排序只根据ID排序;或者传递pageSize限制表格生成数据的数量