POI工具类
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class PoiUtils {/*** 导出Excel* @param sheetName sheet名称* @param title 标题* @param values 内容* @param wb HSSFWorkbook对象* @return*/public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){// 第一步,创建一个HSSFWorkbook,对应一个Excel文件if(wb == null){wb = new HSSFWorkbook();}// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet(sheetName);// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制HSSFRow row = sheet.createRow(0);// 第四步,创建单元格,并设置值表头 设置表头居中HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式//声明列对象HSSFCell cell = null;//创建标题for(int i=0;i<title.length;i++){cell = row.createCell(i);cell.setCellValue(title[i]);cell.setCellStyle(style);}//创建内容for(int i=0;i<values.length;i++){row = sheet.createRow(i + 1);for(int j=0;j<values[i].length;j++){//将内容按顺序赋给对应的列对象row.createCell(j).setCellValue(values[i][j]);}}return wb;}//发送响应流方法public static void setResponseHeader(HttpServletResponse response, String fileName) {try {try {fileName = new String(fileName.getBytes(),"ISO8859-1");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}response.setContentType("application/octet-stream;charset=ISO8859-1");response.setHeader("Content-Disposition", "attachment;filename="+ fileName);response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");} catch (Exception ex) {ex.printStackTrace();}}
}
导出接口
@GetMapping("/exportParkOrderByPage")@ApiOperation(value = "导出订单明细信息(默认查询全部)", notes = "导出订单明细信息(默认查询全部)", response = ParkOrder.class)public void exportParkOrderByPage(@RequestParam(value = "page", required = false, defaultValue = "0") @ApiParam(required = false, defaultValue = "0", value = "当前页数,默认从0开始") Integer page,@RequestParam(value = "limit", required = false, defaultValue = "10") @ApiParam(required = false, defaultValue = "10", value = "每一页条数,默认10条") Integer limit,@RequestParam(value = "pkid", required = false) @ApiParam(required = false, value = "车场名称") String pkid,@RequestParam(value = "payWay", required = false) @ApiParam(required = false, value = "线上线下(0.线上 1.线下)") String payWay,@RequestParam(value = "orderSource", required = false) @ApiParam(required = false, value = "订单来源") String orderSource,@RequestParam(value = "userId", required = false) @ApiParam(required = false, value = "操作人") String userId,@RequestParam(value = "plateNo", required = false) @ApiParam(required = false, value = "车牌号码") String plateNo,@RequestParam(value = "orderTimeStart", required = false) @ApiParam(required = false, value = "开始订单时间") String orderTimeStart,@RequestParam(value = "orderTimeEnd", required = false) @ApiParam(required = false, value = "结束订单时间") String orderTimeEnd,HttpServletResponse response) {try {log.info("++++++++++访问/count/exportParkOrderByPage接口入参:>>>>>>pkid:" + pkid+ " payWay:" + payWay+ " orderSource:" + orderSource+ " userId:" + userId+ " plateNo:" + plateNo+ " orderTimeStart:" + orderTimeStart+ " orderTimeEnd:" + orderTimeEnd);limit = Integer.MAX_VALUE;Page<ParkOrder> requestPage = new Page<>(page, limit);Map<String, Object> requestParam = new HashMap<String, Object>();requestParam.put("pkid", pkid);requestParam.put("payWay", payWay);requestParam.put("orderSource", orderSource);requestParam.put("userId", userId);requestParam.put("plateNo", plateNo);requestParam.put("orderTimeStart", orderTimeStart);requestParam.put("orderTimeEnd", orderTimeEnd);Page<ParkOrder> parkOrderByPage = parkOrderService.getParkOrderByPage(requestPage, requestParam);List<ParkOrder> records = parkOrderByPage.getRecords();//excel标题String[] title = {"车场名称", "车牌号码", "订单编号", "金额", "已付金额", "未付金额", "折扣金额", "订单类型", "支付方式", "订单时间", "订单来源", "操作人", "备注"};//excel文件名String fileName = "订单明细信息" + System.currentTimeMillis() + ".xls";//sheet名String sheetName = "订单明细信息表";SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String[][] content = new String[records.size()][title.length];for (int i = 0; i < records.size(); i++) {//车场名称content[i][0] = records.get(i).getPkName() == null ? "" : records.get(i).getPkName().toString();//车牌号码content[i][1] = records.get(i).getPlateNo() == null ? "" : records.get(i).getPlateNo().toString();//订单编号content[i][2] = records.get(i).getOrderNo() == null ? "" : records.get(i).getOrderNo().toString();//金额content[i][3] = records.get(i).getAmount() == null ? "" : records.get(i).getAmount().toString();//已付金额content[i][4] = records.get(i).getPayAmount() == null ? "" : records.get(i).getPayAmount().toString();//未付金额content[i][5] = records.get(i).getUnPayAmount() == null ? "" : records.get(i).getUnPayAmount().toString();//折扣金额content[i][6] = records.get(i).getDiscountAmount() == null ? "" : records.get(i).getDiscountAmount().toString();//订单类型content[i][7] = "";if (records.get(i).getOrderType() != null && StrUtil.isNotBlank(records.get(i).getOrderType().toString())) {Integer orderType = Integer.valueOf(records.get(i).getOrderType().toString());switch (orderType) {case 1:content[i][7] = "临时卡缴费";break;case 2:content[i][7] = "月卡缴费";break;case 3:content[i][7] = "VIP卡缴费";break;case 4:content[i][7] = "储值卡充值";break;case 5:content[i][7] = "临时卡续期";break;case 6:content[i][7] = "储值卡缴费";break;case 12:content[i][7] = "车位预定";break;}}//支付方式content[i][8] = "";if (records.get(i).getPayWay() != null && StrUtil.isNotBlank(records.get(i).getPayWay().toString())) {Integer payType = Integer.valueOf(records.get(i).getPayWay().toString());switch (payType) {case 1:content[i][8] = "现金";break;case 2:content[i][8] = "微信";break;case 3:content[i][8] = "支付宝";break;case 4:content[i][8] = "网银";break;case 5:content[i][8] = "电子钱包";break;case 6:content[i][8] = "优免券";break;case 7:content[i][8] = "余额";break;}}//订单时间content[i][9] = records.get(i).getOrderTime() == null ? "" : dateFormat.format(records.get(i).getOrderTime());//订单来源content[i][10] = "";if (records.get(i).getOrderSource() != null && StrUtil.isNotBlank(records.get(i).getOrderSource().toString())) {Integer source = Integer.valueOf(records.get(i).getOrderSource().toString());switch (source) {case 1:content[i][10] = "微信";break;case 2:content[i][10] = "APP";break;case 3:content[i][10] = "中心缴费";break;case 4:content[i][10] = "岗亭缴费";break;case 5:content[i][10] = "管理处";break;case 6:content[i][10] = "第三方平台";break;}}//操作人content[i][11] = records.get(i).getUserId() == null ? "" : records.get(i).getUserId().toString();//备注content[i][12] = records.get(i).getRemark() == null ? "" : records.get(i).getRemark().toString();}//创建HSSFWorkbookHSSFWorkbook wb = PoiUtils.getHSSFWorkbook(sheetName, title, content, null);//响应到客户端PoiUtils.setResponseHeader(response, fileName);OutputStream os = response.getOutputStream();wb.write(os);os.flush();os.close();log.info("++++++++++访问/count/exportParkOrderByPage接口调用成功>>>>>>");} catch (Exception e) {e.printStackTrace();log.error("++++++++++访问/count/exportParkOrderByPage接口系统异常原因 : +++++++++" + e.getMessage());} catch (Throwable throwable) {throwable.printStackTrace();log.error("++++++++++访问/count/exportParkOrderByPage接口系统异常原因 : +++++++++" + throwable.getMessage());}}
前端操作
handleDownload() {this.downloadLoading = trueexportParkOrderByPage(this.listQuery).then(res=>{console.log(res)var blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型var downloadElement = document.createElement('a');var href = window.URL.createObjectURL(blob); //创建下载的链接downloadElement.href = href;downloadElement.download = '订单明细.xls'; //下载后文件名document.body.appendChild(downloadElement);downloadElement.click(); //点击下载document.body.removeChild(downloadElement); //下载完成移除元素window.URL.revokeObjectURL(href); //释放掉blob对象 this.downloadLoading = false;})},