时间有限,下个版本上线,数据导出通用版,即直接在pojo类上将注解,即可生成Excel
!--Excel导出依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
/** * 生成Excel * * @param operateRecords */ public int generateTable(List<OperateRecord> operateRecords, String path) { XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("sheet1"); for (int i = 0; i < 9; i++) { sheet.setColumnWidth(i, 4300); } // 标题样式 样式 XSSFFont titleFont = wb.createFont(); titleFont.setFontHeight(24); titleFont.setBold(true); CellStyle titleCellStyle = this.getCellStyle(wb); titleCellStyle.setFont(titleFont); titleCellStyle.setFillBackgroundColor((short) 1); //主 标题 在这里插入主标题 Row titleRow; Cell titleCell; sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 2, (short) 0, (short) 7)); for (int i = 0; i <= 2; i++) { titleRow = sheet.createRow(i); for (int j = 0; j < 8; j++) { titleCell = titleRow.createCell(j); titleCell.setCellType(CellType.STRING); titleCell.setCellStyle(titleCellStyle); titleCell.setCellValue("操作记录表"); } } //列 标题 在这里插入标题 Row rowLabel; Cell cellLabel; rowLabel = sheet.createRow(3); for (int j = 0; j < tableHeaders.size(); j++) { cellLabel = rowLabel.createCell(j); XSSFFont rowsTitleFont = wb.createFont(); rowsTitleFont.setBold(true); CellStyle rowsTitleCellStyle = this.getCellStyle(wb); rowsTitleCellStyle.setFont(rowsTitleFont); cellLabel.setCellType(CellType.STRING); cellLabel.setCellStyle(rowsTitleCellStyle); cellLabel.setCellValue(tableHeaders.get(j)); } //列 数据 在这里插入数据 Row rowCheck; Cell cellCheck; int rows = 4; for (OperateRecord operateRecord : operateRecords) { int column = 0; rowCheck = sheet.createRow((rows++)); cellCheck = rowCheck.createCell(column++); cellCheck.setCellType(CellType.STRING); cellCheck.setCellStyle(this.getCellStyle(wb)); cellCheck.setCellValue(operateRecord.getId()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(format.format(operateRecord.getRecordTime())); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(operateRecord.getUsername()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(operateRecord.getRequestIp()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(operateRecord.getType()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(operateRecord.getRequestMethod()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column++).setCellValue(operateRecord.getRequestAnnotationName()); this.setCellCheck(cellCheck, rowCheck, this.getCellStyle(wb), column).setCellValue(operateRecord.getExceptionMsg()); } if (!path.endsWith("/")) { path = path + "/"; } String filePath = path + format.format(new Date()).subSequence(0, 10) + "操作记录.xlsx"; return this.downloadFile(filePath, wb); } /** * 设置单元格样式 */ private Cell setCellCheck(Cell cellCheck, Row rowCheck, CellStyle cellStyle, int column) { cellCheck = rowCheck.createCell(column); cellCheck.setCellType(CellType.STRING); cellCheck.setCellStyle(cellStyle); return cellCheck; } /** * 设置样式 * * @param wb * @return */ private CellStyle getCellStyle(XSSFWorkbook wb) { CellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setTopBorderColor(HSSFColor.BLACK.index); cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); cellStyle.setRightBorderColor(HSSFColor.BLACK.index); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中 return cellStyle; } /** * 下载电子表格 * * @param path * @return */ private int downloadFile(String path, XSSFWorkbook wb) { try { File file = new File(path); FileOutputStream fileOutputStream = new FileOutputStream(file); wb.write(fileOutputStream); fileOutputStream.close(); wb.close(); return 1; } catch (Exception e) { e.printStackTrace(); return 0; } }
/** * 数据导出 * * @param operateRecords 需要导入的数据 * @param path 文件存储的路径 * @return */ @PostMapping("/generateTable") public Result generateTable(@RequestBody List<OperateRecord> operateRecords, @RequestParam String path) { int flag = operateRecordService.generateTable(operateRecords, path); if (flag <= 0) { return new Result(false, StatusCode.GENERATE_FAIL); } return new Result(true, StatusCode.OK); }