ExcelUtil工具类: package com.leigod.NNN.base.util; import org.apache.poi.hssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.URLEncoder; public class exportExcelUtil { //创建表头 public void createTitle(HSSFWorkbook workbook, HSSFSheet sheet){ HSSFRow row = sheet.createRow(0); //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度 sheet.setColumnWidth(1,12*256); sheet.setColumnWidth(3,17*256); //设置为居中加粗 HSSFCellStyle style = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setBold(true); // style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setFont(font); HSSFCell cell; cell = row.createCell(0); cell.setCellValue("订单编号"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("昵称"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("ID"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("类型"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("单价"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("数量"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("订单金额"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("实付金额"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("下单用户"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("下单用户ID"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("订单状态"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("订单生成时间"); cell.setCellStyle(style); cell = row.createCell(12); cell.setCellValue("订单支付时间"); cell.setCellStyle(style); cell = row.createCell(13); cell.setCellValue("接单时间"); cell.setCellStyle(style); cell = row.createCell(14); cell.setCellValue("订单取消时间"); cell.setCellStyle(style); cell = row.createCell(15); cell.setCellValue("订单完成时间"); cell.setCellStyle(style); } //生成user表excel /* @GetMapping(value = "/getUser") public String getUser(HttpServletResponse response) throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("统计表"); createTitle(workbook,sheet); Listrows = userService.getAll(); //设置日期格式 HSSFCellStyle style = workbook.createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); //新增数据行,并且设置单元格数据 int rowNum=1; for(User user:rows){ HSSFRow row = sheet.createRow(rowNum); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getUsername()); HSSFCell cell = row.createCell(3); cell.setCellValue(user.getCreate_time()); cell.setCellStyle(style); rowNum++; } String fileName = "导出excel例子.xls"; //生成excel文件 buildExcelFile(fileName, workbook); //浏览器下载excel buildExcelDocument(fileName,workbook,response); return "download excel"; }*/ //生成excel文件 public void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{ FileOutputStream fos = new FileOutputStream(filename); workbook.write(fos); fos.flush(); fos.close(); } //浏览器下载excel public void buildExcelDocument(String filename, HSSFWorkbook workbook, HttpServletResponse response) throws Exception{ response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8")); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } }
后台controller:
@ApiOperation(value = "导出excel") @RequestMapping(value = "/exportExcel", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public Object exportExcel(HttpServletResponse response) throws Exception{ exportExcelUtil exportexcelUtil = new exportExcelUtil(); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("订单表"); exportexcelUtil.createTitle(workbook,sheet); Listlist = orderMemberService.getOrderList(3, TransitionUtil.orderPlayerStatus(0), 0, null, null, ""); //设置日期格式 HSSFCellStyle style = workbook.createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); //新增数据行,并且设置单元格数据 SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); int rowNum=1; for(OrderObj orderObj:list){ HSSFRow row = sheet.createRow(rowNum); if(!StringUtils.isEmpty(orderObj.getOrderNo())){ row.createCell(0).setCellValue(orderObj.getOrderNo()); } if(!StringUtils.isEmpty(orderObj.getGoodsName())){ row.createCell(1).setCellValue(orderObj.getGoodsName()); } if(!StringUtils.isEmpty(orderObj.getGoodsId())){ row.createCell(2).setCellValue(orderObj.getGoodsId()); } if(!StringUtils.isEmpty(orderObj.getGoodsSpecifica())){ row.createCell(3).setCellValue(orderObj.getGoodsSpecifica()); } if(!StringUtils.isEmpty(orderObj.getOrderPrice())){ row.createCell(4).setCellValue(orderObj.getOrderPrice().toString()); } if(!StringUtils.isEmpty(orderObj.getOrderNumber())){ row.createCell(5).setCellValue(orderObj.getOrderNumber()); } if(!StringUtils.isEmpty(orderObj.getOrderAmount())){ row.createCell(6).setCellValue(orderObj.getOrderAmount().toString()); } if(!StringUtils.isEmpty(orderObj.getPay())){ row.createCell(7).setCellValue(orderObj.getPay().getPayAmount().toString()); } if(!StringUtils.isEmpty(orderObj.getMemberNickName())){ row.createCell(8).setCellValue(orderObj.getMemberNickName()); } if(!StringUtils.isEmpty(orderObj.getStatusType())){ row.createCell(9).setCellValue(orderObj.getMemberNo()); } if(!StringUtils.isEmpty(orderObj.getStatusType())){ row.createCell(10).setCellValue(orderObj.getStatusType()); } if(!StringUtils.isEmpty(orderObj.getOrderTime())){ row.createCell(11).setCellValue(sdf.format(orderObj.getOrderTime())); } if(!StringUtils.isEmpty(orderObj.getPaymentTime())){ row.createCell(12).setCellValue(sdf.format(orderObj.getPaymentTime())); } if(!StringUtils.isEmpty(orderObj.getReceiptTime())){ row.createCell(13).setCellValue(sdf.format(orderObj.getReceiptTime())); } if(!StringUtils.isEmpty(orderObj.getPaymentTime())){ row.createCell(14).setCellValue(sdf.format(orderObj.getPaymentTime())); } if(!StringUtils.isEmpty(orderObj.getReceiptTime())){ row.createCell(15).setCellValue(sdf.format(orderObj.getReceiptTime())); } }
elementUI,导出的事件:
exportExcel(){ exportExcelAPI(this.formInline).then(res => { if(res.size > 0){ const content = res const blob = new Blob([content]) const fileName = '订单表.xlsx' if ('download' in document.createElement('a')) { // 非IE下载 const elink = document.createElement('a') elink.download = fileName elink.style.display = 'none' elink.href = URL.createObjectURL(blob) document.body.appendChild(elink) elink.click() URL.revokeObjectURL(elink.href) // 释放URL 对象 document.body.removeChild(elink) } else { // IE10+下载 navigator.msSaveBlob(blob, fileName) } /* this.$message.success("导出成功")*/ } }).catch(() => { }); },