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限制表格生成數據的數量