springboot數據導出

1.數導出

時間有限,下個版本上線,數據導出通用版,即直接在pojo類上將註解,即可生成Excel

1.1 引入依賴

!--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>

1.2 service類

/**
 * 生成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;
    }
}

1.2 Controller類

/**
 * 數據導出
 *
 * @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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章