@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);
}