jar
jinlin
2025-03-10 f0aae4b39afbcbb18fb5a0b32e3ca689662c5ca7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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<DjJdgzTrackRecord> 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());
 
                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).getGeneralRepairUnitDirector());
                }
                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();
        }
    }
}