package com.example.server.utils; import com.example.Application; import com.example.server.progressTrack.model.DjJdgzTrackRecord; import com.example.server.progressTrack.service.DjJdgzDismantTrackService; import com.example.server.progressTrack.service.DjJdgzTrackRecordService; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import javax.swing.*; import java.io.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.List; public class DownLoadTmpFile { public static void down(String name, JFrame frame1) { InputStream inputStream = Application.class.getClassLoader().getResourceAsStream(name); if (inputStream == null) { try { inputStream = new FileInputStream(name); } catch (FileNotFoundException e) { e.printStackTrace(); } } // 创建临时文件 File tempFile; try { tempFile = File.createTempFile("tmp", ".tmp"); tempFile.deleteOnExit(); // 确保临时文件在程序退出时删除 Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { JOptionPane.showMessageDialog(frame1, "无法创建临时文件: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); return; } // 弹出文件选择对话框 JFileChooser fileChooser = new JFileChooser(); fileChooser.setSelectedFile(new File(name)); // 默认文件名 int result = fileChooser.showSaveDialog(frame1); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try { // 将临时文件复制到用户选择的位置 Files.copy(tempFile.toPath(), selectedFile.toPath(), StandardCopyOption.REPLACE_EXISTING); JOptionPane.showMessageDialog(frame1, "文件下载成功", "成功", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(frame1, "文件保存失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } else { // 如果用户取消操作,不显示额外提示 tempFile.delete(); // 删除临时文件 } } public static void buildTrack(List list, String path, JFrame frame1) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 将当前日期格式化为字符串 String date = currentDate.format(formatter); InputStream inputStream = Application.class.getClassLoader().getResourceAsStream(path); if (inputStream == null) { try { inputStream = new FileInputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } } try { Workbook workbook = new XSSFWorkbook(inputStream); // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); int num = 1; for (int i = 0; i < list.size(); i++) { num = num + 1; Row row = sheet.createRow(num); Cell cell = row.createCell(0); cell.setCellValue(list.get(i).getId().toString()); cell = row.createCell(1); cell.setCellValue(date); cell = row.createCell(2); cell.setCellValue(list.get(i).getLevel3NetworkName()); cell = row.createCell(12); if (StringUtils.isNotBlank(list.get(i).getTrackPerson())) { cell.setCellValue(list.get(i).getTrackPerson()); } cell = row.createCell(13); if (StringUtils.isNotBlank(list.get(i).getTrackPersonContact())) { cell.setCellValue(list.get(i).getTrackPersonContact()); } cell = row.createCell(14); if (StringUtils.isNotBlank(list.get(i).getTrackPersonUnit())) { cell.setCellValue(list.get(i).getTrackPersonUnit()); } cell = row.createCell(15); if (StringUtils.isNotBlank(list.get(i).getTrackedPerson())) { cell.setCellValue(list.get(i).getTrackPerson()); } cell = row.createCell(16); if (StringUtils.isNotBlank(list.get(i).getTrackedPersonContact())) { cell.setCellValue(list.get(i).getTrackPersonContact()); } cell = row.createCell(17); if (StringUtils.isNotBlank(list.get(i).getTrackedPersonUnit())) { cell.setCellValue(list.get(i).getTrackPersonUnit()); } cell = row.createCell(18); if (StringUtils.isNotBlank(list.get(i).getGeneralRepairUnit())) { cell.setCellValue(list.get(i).getGeneralRepairUnit()); } cell = row.createCell(19); if (StringUtils.isNotBlank(list.get(i).getGeneralRepairUnitDirector())) { cell.setCellValue(list.get(i).getGeneralRepairUnitDirector()); } cell = row.createCell(20); if (StringUtils.isNotBlank(list.get(i).getGeneralRepairUnitContact())) { cell.setCellValue(list.get(i).getGeneralRepairUnitContact()); } cell = row.createCell(21); if (StringUtils.isNotBlank(list.get(i).getRepairUnit())) { cell.setCellValue(list.get(i).getRepairUnit()); } cell = row.createCell(22); if (StringUtils.isNotBlank(list.get(i).getRepairUnitDirector())) { cell.setCellValue(list.get(i).getRepairUnitDirector()); } cell = row.createCell(23); if (StringUtils.isNotBlank(list.get(i).getRepairUnitContact())) { cell.setCellValue(list.get(i).getRepairUnitContact()); } if (i >= list.size() - 1) { row = sheet.createRow(sheet.getLastRowNum() + 3); cell = row.createCell(1); cell.setCellValue("注:"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("1. 设备名称:导出模板时,系统自动生成该专业的所有设备,用户不要修改"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("2. 黄色栏目为主要填写信息,其中,预计完成时间不填时,导入时系统将当前节点的要求完成时间作为预计完成时间"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("3. 绿色栏目为导出模板时带有数据,可根据需要修改"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("4. 数据录入时间为跟踪的时间,导入后系统自动换算成周"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("5. 数据录入时间或者当前节点栏目未录数据时,导入时系统判断该行为空"); row = sheet.createRow(sheet.getLastRowNum() + 1); cell = row.createCell(2); cell.setCellValue("6. 以下栏目为下拉选:当前节点、跟踪方式、节点进展、有无脱期风险"); } } // 弹出文件选择对话框 JFileChooser fileChooser = new JFileChooser(); fileChooser.setSelectedFile(new File(path)); // 默认文件名 int result = fileChooser.showSaveDialog(frame1); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try (FileOutputStream outputStream = new FileOutputStream(selectedFile)) { workbook.write(outputStream); JOptionPane.showMessageDialog(frame1, "文件保存成功", "成功", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(frame1, "文件保存失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } } catch ( IOException e) { e.printStackTrace(); } } }