mengff
11 months ago
3 changed files with 88 additions and 11 deletions
@ -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…
Reference in new issue