by-crm/backend/src/main/java/com/bycrm/util/ImportSchools.java
2026-01-26 16:01:15 +08:00

56 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.bycrm.util;
import com.bycrm.dto.SchoolDTO;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.File;
import java.util.List;
/**
* 学校数据导入执行类
* 使用方法:运行 main 方法,传入 school.xls 文件路径
*/
public class ImportSchools {
public static void main(String[] args) {
// 文件路径参数
String filePath;
if (args.length > 0) {
filePath = args[0];
} else {
// 默认路径:项目根目录下的 docs/school.xls
filePath = "docs/school.xls";
}
File file = new File(filePath);
if (!file.exists()) {
System.err.println("文件不存在: " + file.getAbsolutePath());
return;
}
try {
System.out.println("开始读取文件: " + file.getAbsolutePath());
List<SchoolDTO> schools = SchoolImporter.importFromXls(filePath);
System.out.println("成功读取 " + schools.size() + " 条学校数据");
// 打印前5条数据预览
System.out.println("\n数据预览前5条:");
int previewCount = Math.min(5, schools.size());
for (int i = 0; i < previewCount; i++) {
SchoolDTO dto = schools.get(i);
System.out.printf("%d. [%s] %s - %s%n",
i + 1, dto.getSchoolCode(), dto.getSchoolName(), dto.getLocation());
}
// 保存到文件供后续使用
System.out.println("\n数据已准备好请通过 API 接口导入到数据库");
System.out.println("或者使用 SchoolService.batchImport() 方法批量导入");
} catch (Exception e) {
System.err.println("导入失败: " + e.getMessage());
e.printStackTrace();
}
}
}