Browse Source

----增加扫描结果导出到excel表格中

main
mengff 11 months ago
parent
commit
fef3a8d284
  1. 11
      pom.xml
  2. 45
      src/main/java/com/example/device/controller/DeviceStatus.java
  3. 43
      src/main/java/com/example/device/service/ExcelExportService.java

11
pom.xml

@ -49,6 +49,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
<build>

45
src/main/java/com/example/device/controller/DeviceStatus.java

@ -2,6 +2,7 @@ package com.example.device.controller;
import com.example.device.entity.Device;
import com.example.device.entity.Status;
import com.example.device.service.DeviceService;
import com.example.device.service.ExcelExportService;
import com.example.device.service.StatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
@ -10,11 +11,15 @@ import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
@Component
@EnableScheduling
@ -25,9 +30,11 @@ public class DeviceStatus {
@Autowired
private StatusService statusService;
@Autowired
private ExcelExportService excelExportService;
//每天凌晨开始执行,每4小时执行一次
@Scheduled(cron = "0 33 * * * ?")
@Scheduled(cron = "0 51 8 * * ?")
// @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
public void generateDeviceStatus() {
deviceStatus();
@ -55,12 +62,13 @@ public class DeviceStatus {
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
}
}
calculateSuccessRate();
}
/**
* 计算成功率
*/
public Map<String, String> calculateSuccessRate() {
public void calculateSuccessRate() {
LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
LocalDateTime currentTime = LocalDateTime.now();
@ -71,18 +79,33 @@ public class DeviceStatus {
status.setStartTime(todayStart);
status.setTime(currentTime);
List<Status> listStatus = statusService.list(status);
Map<String, List<Status>> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceIp));
for (String ip : map.keySet()) {
Map<String, List<Status>> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceName));
for (String name : map.keySet()) {
// 成功的次数
long successNumber = map.get(ip).stream().filter(item -> item.getDeviceStatus() == 1).count();
long successNumber = map.get(name).stream().filter(item -> item.getDeviceStatus() == 1).count();
// 此ip对应的成功率
String successRate = String.format("%.2f%%", (double) successNumber / map.get(ip).size() * 100);
ipMap.put(ip, successRate);
String successRate = String.format("%.2f%%", (double) successNumber / map.get(name).size() * 100);
ipMap.put(name, successRate);
}
return ipMap;
String filePath=CreateNamedExcel();
excelExportService.exportDataToExcel(ipMap,filePath);
}
public String CreateNamedExcel() {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
String filePath = "/Users/mengfanfeng/Downloads/excel/"+formattedDateTime+"--deviceStatus.xlsx";
try (Workbook workbook = new XSSFWorkbook()) {
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
System.out.println("Empty named Excel created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
}

43
src/main/java/com/example/device/service/ExcelExportService.java

@ -0,0 +1,43 @@
package com.example.device.service;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Map;
@Service
public class ExcelExportService {
public void exportDataToExcel(Map<String, String> ipMap, String filePath) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("设备故障率");
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
int rowNum = 0;
for (String name : ipMap.keySet()) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(name);
row.createCell(1).setCellValue(ipMap.get(name));
row.createCell(2).setCellValue(formattedDateTime);
CellStyle style = workbook.createCellStyle();
// 应用样式到单元格
row.getCell(0).setCellStyle(style);
row.getCell(1).setCellStyle(style);
row.getCell(2).setCellStyle(style);
}
try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading…
Cancel
Save