yeguodong před 1 týdnem
rodič
revize
a103e0d743

+ 7 - 0
src/main/java/thyyxxk/webserver/controller/settings/SettingsController.java

@@ -25,6 +25,7 @@ import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice;
 import thyyxxk.webserver.service.settings.SettingsService;
 import thyyxxk.webserver.utils.ResultVoUtil;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
@@ -161,4 +162,10 @@ public class SettingsController {
         service.setUserConfig(js.getUserConfig());
         return ResultVoUtil.success();
     }
+
+    @PassToken
+    @GetMapping("/importUser")
+    public void importUser() throws IOException {
+        service.importUser();
+    }
 }

+ 37 - 0
src/main/java/thyyxxk/webserver/service/settings/SettingsService.java

@@ -35,6 +35,8 @@ import thyyxxk.webserver.service.hutoolcache.ExtraCache;
 import thyyxxk.webserver.service.hutoolcache.UserCache;
 import thyyxxk.webserver.utils.*;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -338,4 +340,39 @@ public class SettingsService {
         userCache.refreshCache(id);
     }
 
+    public void importUser() throws IOException {
+        File file = new File("D:\\his\\基础数据\\铭和\\上级专家(2).xlsx");
+        List<List<String>> data = ExcelReaderUtil.readExcel(file);
+
+        // 打印读取到的数据
+        for (List<String> row : data) {
+            if("科室".equals(row.get(0))) {
+                continue;
+            }
+            try {
+                UserInfo userInfo = new UserInfo();
+                userInfo.setCodeRs(row.get(7));
+                userInfo.setName(row.get(1));
+                userInfo.setDeptCode(row.get(0));
+                userInfo.setSocialNo(row.get(3));
+                userInfo.setYbCode(row.get(6));
+                userInfo.setPhoneNo(row.get(4));
+                userInfo.setYbName(row.get(5));
+                userInfo.setDoctorXzYp("0");
+                userInfo.setOrderYn("1");
+                userInfo.setCode("0" + publicServer.getPersonnelCode());
+                userInfo.setPyCode(PingYinUtils.pyShouZiMuDaXie(userInfo.getName()));
+                userInfo.setDCode(PingYinUtils.getWBCode(userInfo.getName()));
+                dao.saveEmployeeInfo(userInfo);
+                dao.delPartTimeDeptByCode(userInfo.getCode());
+                if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
+                    dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+    }
+
 }

+ 200 - 0
src/main/java/thyyxxk/webserver/utils/ExcelReaderUtil.java

@@ -0,0 +1,200 @@
+package thyyxxk.webserver.utils;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Excel文件读取工具类,支持xls和xlsx格式
+ */
+public class ExcelReaderUtil {
+
+    private static final String XLS = "xls";
+    private static final String XLSX = "xlsx";
+
+    /**
+     * 读取Excel文件内容
+     * @param file Excel文件
+     * @return 表格数据列表,每个元素是一行数据的列表
+     * @throws IOException 当文件操作发生错误时抛出
+     */
+    public static List<List<String>> readExcel(File file) throws IOException {
+        // 验证文件
+        validateFile(file);
+
+        // 根据文件后缀名创建对应的Workbook
+        try (InputStream is = new FileInputStream(file)) {
+            Workbook workbook;
+            if (file.getName().endsWith(XLS)) {
+                workbook = new HSSFWorkbook(is);
+            } else if (file.getName().endsWith(XLSX)) {
+                workbook = new XSSFWorkbook(is);
+            } else {
+                throw new IllegalArgumentException("不支持的文件格式,仅支持xls和xlsx格式");
+            }
+
+            // 读取所有工作表数据
+            List<List<String>> result = new ArrayList<>();
+            for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
+                Sheet sheet = workbook.getSheetAt(sheetNum);
+                if (sheet == null) {
+                    continue;
+                }
+
+                // 读取当前工作表数据
+                readSheet(sheet, result);
+            }
+
+            workbook.close();
+            return result;
+        }
+    }
+
+    /**
+     * 读取指定工作表的内容
+     * @param file Excel文件
+     * @param sheetIndex 工作表索引(从0开始)
+     * @return 表格数据列表,每个元素是一行数据的列表
+     * @throws IOException 当文件操作发生错误时抛出
+     */
+    public static List<List<String>> readExcelBySheet(File file, int sheetIndex) throws IOException {
+        // 验证文件
+        validateFile(file);
+
+        // 根据文件后缀名创建对应的Workbook
+        try (InputStream is = new FileInputStream(file)) {
+            Workbook workbook;
+            if (file.getName().endsWith(XLS)) {
+                workbook = new HSSFWorkbook(is);
+            } else if (file.getName().endsWith(XLSX)) {
+                workbook = new XSSFWorkbook(is);
+            } else {
+                throw new IllegalArgumentException("不支持的文件格式,仅支持xls和xlsx格式");
+            }
+
+            // 验证工作表索引
+            if (sheetIndex < 0 || sheetIndex >= workbook.getNumberOfSheets()) {
+                throw new IllegalArgumentException("工作表索引超出范围");
+            }
+
+            // 读取指定工作表数据
+            List<List<String>> result = new ArrayList<>();
+            Sheet sheet = workbook.getSheetAt(sheetIndex);
+            if (sheet != null) {
+                readSheet(sheet, result);
+            }
+
+            workbook.close();
+            return result;
+        }
+    }
+
+    /**
+     * 读取单个工作表的数据
+     * @param sheet 工作表对象
+     * @param result 存储结果的列表
+     */
+    private static void readSheet(Sheet sheet, List<List<String>> result) {
+        // 获取首行和尾行
+        int firstRowNum = sheet.getFirstRowNum();
+        int lastRowNum = sheet.getLastRowNum();
+
+        // 遍历所有行
+        for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {
+            Row row = sheet.getRow(rowNum);
+            if (row == null) {
+                continue;
+            }
+
+            // 读取当前行数据
+            List<String> rowData = new ArrayList<>();
+            int firstCellNum = row.getFirstCellNum();
+            int lastCellNum = row.getLastCellNum();
+
+            for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
+                Cell cell = row.getCell(cellNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
+                rowData.add(getCellValue(cell));
+            }
+
+            result.add(rowData);
+        }
+    }
+
+    /**
+     * 获取单元格的值
+     * @param cell 单元格对象
+     * @return 单元格的字符串值
+     */
+    private static String getCellValue(Cell cell) {
+        if (cell == null) {
+            return "";
+        }
+
+        // 根据单元格类型获取值
+        String cellValue;
+        switch (cell.getCellType()) {
+            case STRING:
+                cellValue = cell.getStringCellValue();
+                break;
+            case NUMERIC:
+                cellValue = cell.getDateCellValue().toString();
+                break;
+            case BOOLEAN:
+                cellValue = String.valueOf(cell.getBooleanCellValue());
+                break;
+            case FORMULA:
+                // 计算公式结果
+                Workbook workbook = cell.getSheet().getWorkbook();
+                FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
+                cellValue = getCellValue(evaluator.evaluateInCell(cell));
+                break;
+            default:
+                cellValue = "";
+        }
+
+        return cellValue.trim();
+    }
+
+    /**
+     * 验证文件是否有效
+     * @param file 要验证的文件
+     */
+    private static void validateFile(File file) {
+        if (file == null) {
+            throw new IllegalArgumentException("文件不能为空");
+        }
+        if (!file.exists()) {
+            throw new IllegalArgumentException("文件不存在: " + file.getAbsolutePath());
+        }
+        if (!file.isFile()) {
+            throw new IllegalArgumentException("不是一个文件: " + file.getAbsolutePath());
+        }
+        if (!file.getName().endsWith(XLS) && !file.getName().endsWith(XLSX)) {
+            throw new IllegalArgumentException("不支持的文件格式,仅支持xls和xlsx格式");
+        }
+    }
+
+    // 使用示例
+    public static void main(String[] args) {
+        try {
+            File file = new File("D:\\his\\基础数据\\铭和\\上级专家(2).xlsx");
+            List<List<String>> data = ExcelReaderUtil.readExcel(file);
+
+            // 打印读取到的数据
+            for (List<String> row : data) {
+                System.out.println(row);
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}
+