|
|
@ -1,6 +1,7 @@ |
|
|
|
package com.example.device.service; |
|
|
|
import com.example.device.entity.Status; |
|
|
|
import org.apache.poi.ss.usermodel.*; |
|
|
|
import org.apache.poi.ss.util.CellRangeAddress; |
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
@ -9,6 +10,7 @@ import java.io.IOException; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
@ -17,38 +19,49 @@ public class ExcelExportService { |
|
|
|
|
|
|
|
public void exportDataToExcel(Map<Integer, List<Status>> ipMap, String filePath) { |
|
|
|
Workbook workbook = new XSSFWorkbook(); |
|
|
|
for (Map.Entry<Integer, List<Status>> entry : ipMap.entrySet()) { |
|
|
|
Sheet sheet = workbook.createSheet(entry.getKey()+"小时设备故障率"); |
|
|
|
LocalDateTime dateTime = LocalDateTime.now(); |
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|
String formattedDateTime = dateTime.format(formatter); |
|
|
|
Sheet sheet = workbook.createSheet("设备故障率统计页"); |
|
|
|
int rowNum = 0; |
|
|
|
Row row = sheet.createRow(rowNum); |
|
|
|
row.createCell(0).setCellValue("设备名称"); |
|
|
|
row.createCell(1).setCellValue("设备IP"); |
|
|
|
row.createCell(2).setCellValue("在线率"); |
|
|
|
row.createCell(3).setCellValue("丢包率"); |
|
|
|
CellStyle style = workbook.createCellStyle(); |
|
|
|
// 应用样式到单元格
|
|
|
|
row.getCell(0).setCellStyle(style); |
|
|
|
row.getCell(1).setCellStyle(style); |
|
|
|
row.getCell(2).setCellStyle(style); |
|
|
|
row.getCell(3).setCellStyle(style); |
|
|
|
rowNum = 1; |
|
|
|
|
|
|
|
Row row0 = sheet.createRow(0); |
|
|
|
row0.createCell(0).setCellValue("设备名称"); |
|
|
|
row0.createCell(1).setCellValue("设备IP"); |
|
|
|
row0.createCell(2).setCellValue("设备桩号"); |
|
|
|
row0.createCell(3).setCellValue("设备方向"); |
|
|
|
row0.createCell(4).setCellValue("设备厂家"); |
|
|
|
row0.createCell(5).setCellValue("设备型号"); |
|
|
|
row0.createCell(6).setCellValue("备注"); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6)); |
|
|
|
int i=0; |
|
|
|
boolean flag = true; |
|
|
|
Row row1= sheet.createRow(1); |
|
|
|
for (Map.Entry<Integer, List<Status>> entry : ipMap.entrySet()) { |
|
|
|
row0.createCell(2*i+7).setCellValue(entry.getKey()+"时"); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8)); |
|
|
|
row1.createCell(2*i+7).setCellValue("在线率"); |
|
|
|
row1.createCell(2*i+8).setCellValue("丢包率"); |
|
|
|
rowNum = 2; |
|
|
|
List<Status> groupItems = entry.getValue(); |
|
|
|
for (Status ignored : groupItems) { |
|
|
|
row= sheet.createRow(rowNum++); |
|
|
|
Row row = sheet.createRow(rowNum++); |
|
|
|
if(flag) { |
|
|
|
row.createCell(0).setCellValue(ignored.getDeviceName()); |
|
|
|
row.createCell(1).setCellValue(ignored.getDeviceIp()); |
|
|
|
row.createCell(2).setCellValue(ignored.getSuccessRate()); |
|
|
|
row.createCell(3).setCellValue(ignored.getLostRate()); |
|
|
|
// 应用样式到单元格
|
|
|
|
row.getCell(0).setCellStyle(style); |
|
|
|
row.getCell(1).setCellStyle(style); |
|
|
|
row.getCell(2).setCellStyle(style); |
|
|
|
row.getCell(3).setCellStyle(style); |
|
|
|
row.createCell(2).setCellValue(ignored.getDeviceNo()); |
|
|
|
row.createCell(3).setCellValue(ignored.getDirection()); |
|
|
|
row.createCell(4).setCellValue(ignored.getProduction()); |
|
|
|
row.createCell(5).setCellValue(ignored.getModel()); |
|
|
|
row.createCell(6).setCellValue(ignored.getContent()); |
|
|
|
} |
|
|
|
row.createCell(2*i+7).setCellValue(ignored.getSuccessRate()); |
|
|
|
row.createCell(2*i+8).setCellValue(ignored.getLostRate()); |
|
|
|
} |
|
|
|
i++; |
|
|
|
flag = false; |
|
|
|
} |
|
|
|
try (FileOutputStream outputStream = new FileOutputStream(filePath)) { |
|
|
|
workbook.write(outputStream); |
|
|
|