|
|
|
@ -19,44 +19,43 @@ public class ExcelProcessor { |
|
|
|
String inputFilePath = "D:\\地区导出.xls"; // 输入文件路径 |
|
|
|
String outputFilePath = "D:\\output.xlsx"; // 输出文件路径 |
|
|
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(new File(inputFilePath)); |
|
|
|
Workbook workbook = new HSSFWorkbook(fis); |
|
|
|
FileOutputStream fos = new FileOutputStream(new File(outputFilePath))) { |
|
|
|
try (FileInputStream fis = new FileInputStream(inputFilePath); |
|
|
|
HSSFWorkbook oldWorkbook = new HSSFWorkbook(fis)) { |
|
|
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 |
|
|
|
Sheet sheet = oldWorkbook.getSheetAt(0); |
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
|
|
|
// 创建新的输出工作簿 |
|
|
|
Workbook outputWorkbook = new XSSFWorkbook(); |
|
|
|
Sheet outputSheet = outputWorkbook.createSheet("Result"); |
|
|
|
Row headerRow = outputSheet.createRow(0); |
|
|
|
headerRow.createCell(0).setCellValue("jwcode"); |
|
|
|
headerRow.createCell(1).setCellValue("name"); |
|
|
|
headerRow.createCell(2).setCellValue("country"); |
|
|
|
// 2. 单独创建新xlsx + 输出流(必须放一起自动关闭) |
|
|
|
try (FileOutputStream fos = new FileOutputStream(outputFilePath); |
|
|
|
XSSFWorkbook outputWorkbook = new XSSFWorkbook()) { |
|
|
|
|
|
|
|
int outputRowIndex = 1; |
|
|
|
Sheet outputSheet = outputWorkbook.createSheet("Result"); |
|
|
|
Row headerRow = outputSheet.createRow(0); |
|
|
|
headerRow.createCell(0).setCellValue("jwcode"); |
|
|
|
headerRow.createCell(1).setCellValue("name"); |
|
|
|
headerRow.createCell(2).setCellValue("country"); |
|
|
|
|
|
|
|
// 遍历输入文件的每一行 |
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) { // 假设第一行是标题 |
|
|
|
Row row = sheet.getRow(i); |
|
|
|
if (row == null) continue; |
|
|
|
int outputRowIndex = 1; |
|
|
|
|
|
|
|
String jwcode = row.getCell(1).getStringCellValue(); // 假设 jwcode 在第一列 |
|
|
|
System.out.println("Processing jwcode: " + jwcode); |
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) { |
|
|
|
Row row = sheet.getRow(i); |
|
|
|
if (row == null) continue; |
|
|
|
|
|
|
|
// 调用接口 |
|
|
|
Map<String, String> result = callApi(jwcode, restTemplate); |
|
|
|
String jwcode = row.getCell(1).getStringCellValue(); |
|
|
|
Map<String, String> result = callApi(jwcode, restTemplate); |
|
|
|
|
|
|
|
// 写入结果到新 Excel 文件 |
|
|
|
Row outputRow = outputSheet.createRow(outputRowIndex++); |
|
|
|
outputRow.createCell(0).setCellValue(jwcode); |
|
|
|
outputRow.createCell(1).setCellValue(result.getOrDefault("name", "未知")); |
|
|
|
outputRow.createCell(2).setCellValue(result.getOrDefault("country", "未知")); |
|
|
|
Row outputRow = outputSheet.createRow(outputRowIndex++); |
|
|
|
outputRow.createCell(0).setCellValue(jwcode); |
|
|
|
outputRow.createCell(1).setCellValue(result.getOrDefault("name", "未知")); |
|
|
|
outputRow.createCell(2).setCellValue(result.getOrDefault("country", "未知")); |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 写入 + 强制刷盘 |
|
|
|
outputWorkbook.write(fos); |
|
|
|
fos.flush(); |
|
|
|
} |
|
|
|
|
|
|
|
// 保存输出文件 |
|
|
|
outputWorkbook.write(fos); |
|
|
|
System.out.println("Processing completed. Output file saved to: " + outputFilePath); |
|
|
|
System.out.println("文件生成成功:" + outputFilePath); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
|