in hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java [168:236]
public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os) {
try {
Workbook workbook = WorkbookFactory.create(true);
String sheetName = "Export AlertDefine";
Sheet sheet = workbook.createSheet(sheetName);
sheet.setDefaultColumnWidth(20);
sheet.setColumnWidth(2, 40 * 256);
sheet.setColumnWidth(5, 40 * 256);
sheet.setColumnWidth(6, 40 * 256);
sheet.setColumnWidth(7, 40 * 256);
// set header style
CellStyle headerCellStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerCellStyle.setFont(headerFont);
headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
// set cell style
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// set header
String[] headers = {"Name", "Type", "Expr", "Period", "Times", "Labels", "Annotations", "Template", "Enable"};
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerCellStyle);
}
// Traverse the threshold rule list, each threshold rule object corresponds to a row of data
int rowIndex = 1;
for (ExportAlertDefineDTO alertDefine : exportAlertDefineList) {
AlertDefineDTO alertDefineDTO = alertDefine.getAlertDefine();
Row row = sheet.createRow(rowIndex++);
// Threshold rule information only needs to be written once
Cell nameCell = row.createCell(0);
nameCell.setCellValue(alertDefineDTO.getName());
nameCell.setCellStyle(cellStyle);
Cell typeCell = row.createCell(1);
typeCell.setCellValue(alertDefineDTO.getType());
typeCell.setCellStyle(cellStyle);
Cell exprCell = row.createCell(2);
exprCell.setCellValue(alertDefineDTO.getExpr());
exprCell.setCellStyle(cellStyle);
Cell periodCell = row.createCell(3);
periodCell.setCellValue(alertDefineDTO.getPeriod());
periodCell.setCellStyle(cellStyle);
Cell timesCell = row.createCell(4);
timesCell.setCellValue(alertDefineDTO.getTimes());
timesCell.setCellStyle(cellStyle);
Cell labelsCell = row.createCell(5);
labelsCell.setCellValue(JsonUtil.toJson(alertDefineDTO.getLabels()));
labelsCell.setCellStyle(cellStyle);
Cell annoCell = row.createCell(6);
annoCell.setCellValue(JsonUtil.toJson(alertDefineDTO.getAnnotations()));
annoCell.setCellStyle(cellStyle);
Cell templateCell = row.createCell(7);
templateCell.setCellValue(alertDefineDTO.getTemplate());
templateCell.setCellStyle(cellStyle);
Cell enableCell = row.createCell(8);
enableCell.setCellValue(alertDefineDTO.getEnable());
enableCell.setCellStyle(cellStyle);
}
workbook.write(os);
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}