From 189756284a9ded833f1da2eaddaa605439391014 Mon Sep 17 00:00:00 2001 From: sunjiabei Date: Fri, 27 Mar 2026 11:26:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20260327=E6=B6=88=E8=80=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/password/ExcelProcessor.java | 55 +++++++++++----------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/example/demo/password/ExcelProcessor.java b/src/main/java/com/example/demo/password/ExcelProcessor.java index f0921d9..36c08be 100644 --- a/src/main/java/com/example/demo/password/ExcelProcessor.java +++ b/src/main/java/com/example/demo/password/ExcelProcessor.java @@ -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 result = callApi(jwcode, restTemplate); + String jwcode = row.getCell(1).getStringCellValue(); + Map 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(); From 0e4aa60bff3a903ca7f2b03938cbe7484ff608cf Mon Sep 17 00:00:00 2001 From: sunjiabei Date: Fri, 27 Mar 2026 11:29:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20260327=E6=B6=88=E8=80=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/password/ExcelProcessor.java | 55 +++++++++++----------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/example/demo/password/ExcelProcessor.java b/src/main/java/com/example/demo/password/ExcelProcessor.java index 36c08be..f0921d9 100644 --- a/src/main/java/com/example/demo/password/ExcelProcessor.java +++ b/src/main/java/com/example/demo/password/ExcelProcessor.java @@ -19,43 +19,44 @@ public class ExcelProcessor { String inputFilePath = "D:\\地区导出.xls"; // 输入文件路径 String outputFilePath = "D:\\output.xlsx"; // 输出文件路径 - try (FileInputStream fis = new FileInputStream(inputFilePath); - HSSFWorkbook oldWorkbook = new HSSFWorkbook(fis)) { + try (FileInputStream fis = new FileInputStream(new File(inputFilePath)); + Workbook workbook = new HSSFWorkbook(fis); + FileOutputStream fos = new FileOutputStream(new File(outputFilePath))) { - Sheet sheet = oldWorkbook.getSheetAt(0); + Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 RestTemplate restTemplate = new RestTemplate(); - // 2. 单独创建新xlsx + 输出流(必须放一起自动关闭) - try (FileOutputStream fos = new FileOutputStream(outputFilePath); - XSSFWorkbook outputWorkbook = new XSSFWorkbook()) { + // 创建新的输出工作簿 + 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"); - 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"); + int outputRowIndex = 1; - int outputRowIndex = 1; + // 遍历输入文件的每一行 + for (int i = 1; i <= sheet.getLastRowNum(); i++) { // 假设第一行是标题 + Row row = sheet.getRow(i); + if (row == null) continue; - for (int i = 1; i <= sheet.getLastRowNum(); i++) { - Row row = sheet.getRow(i); - if (row == null) continue; + String jwcode = row.getCell(1).getStringCellValue(); // 假设 jwcode 在第一列 + System.out.println("Processing jwcode: " + jwcode); - String jwcode = row.getCell(1).getStringCellValue(); - Map result = callApi(jwcode, restTemplate); + // 调用接口 + Map result = callApi(jwcode, restTemplate); - 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(); + // 写入结果到新 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", "未知")); } - System.out.println("文件生成成功:" + outputFilePath); + // 保存输出文件 + outputWorkbook.write(fos); + System.out.println("Processing completed. Output file saved to: " + outputFilePath); } catch (Exception e) { e.printStackTrace();