Easy Excel文件存在则追加数据,不存在则导出数据。

    @Override
    public void writeRecordsToExcel(@NotBlank String filePath, @NotNull List<RecordExtExcel> recordExtExcelList) throws IOException {
        File file = new File(filePath);
        if (file.exists()) {
            appendDataToExcel(filePath, recordExtExcelList);
        } else {
            createExcelAndWrite(filePath, recordExtExcelList);
        }
    }
 private void appendDataToExcel(@NotBlank String filePath, @NotNull List<RecordExtExcel> recordExtExcelList) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        FileOutputStream out = new FileOutputStream(filePath);
        for (RecordExtExcel recordExtExcel : recordExtExcelList) {
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            XSSFRow row = sheet.createRow((short) (sheet.getLastRowNum() + 1));
            XSSFCell cell = row.createCell(0);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(recordExtExcel.getDateString());
            XSSFCell cell1 = row.createCell(1);
            cell1.setCellStyle(cellStyle);
            cell1.setCellValue(recordExtExcel.getScheduleIndex());
            XSSFCell cell2 = row.createCell(2);
            cell2.setCellStyle(cellStyle);
            cell2.setCellValue(recordExtExcel.getMachine());
            XSSFCell cell3 = row.createCell(3);
            cell3.setCellStyle(cellStyle);
            cell3.setCellValue(recordExtExcel.getWarnModule());
            XSSFCell cell4 = row.createCell(4);
            cell4.setCellStyle(cellStyle);
            cell4.setCellValue(recordExtExcel.getWarnRecord());
            XSSFCell cell5 = row.createCell(5);
            cell5.setCellStyle(cellStyle);
            cell5.setCellValue(recordExtExcel.getStartDateString());
            XSSFCell cell6 = row.createCell(6);
            cell6.setCellStyle(cellStyle);
            cell6.setCellValue(recordExtExcel.getStartTimeString());
            XSSFCell cell7 = row.createCell(7);
            cell7.setCellStyle(cellStyle);
            cell7.setCellValue(recordExtExcel.getEndDateString());
            XSSFCell cell8 = row.createCell(8);
            cell8.setCellStyle(cellStyle);
            cell8.setCellValue(recordExtExcel.getEndTimeString());
            XSSFCell cell9 = row.createCell(9);
            cell9.setCellStyle(cellStyle);
            cell9.setCellValue(recordExtExcel.getShutdownDuration());
            XSSFCell cell10 = row.createCell(10);
            cell10.setCellStyle(cellStyle);
            cell10.setCellValue(recordExtExcel.getName());
            XSSFCell cell11 = row.createCell(11);
            cell11.setCellStyle(cellStyle);
            cell11.setCellValue(recordExtExcel.getChipType());
        }
        out.flush();
        workbook.write(out);
        out.close();
    }
    private void createExcelAndWrite(@NotBlank String filePath, @NotNull List<RecordExtExcel> recordExtExcelList) throws FileNotFoundException {
        if (CollectionUtils.isEmpty(recordExtExcelList)) {
            return;
        }
        FileOutputStream out = new FileOutputStream(filePath);
        EasyExcel.write(out, RecordExtExcel.class)
                .autoCloseStream(Boolean.TRUE)
                .sheet("Sheet1")
                .doWrite(recordExtExcelList);
    }

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值