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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值