潘志宝
2025-03-20 8688bfa3bba72f0e89520014a67454493b25e416
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/Readtxt.java
@@ -1,8 +1,8 @@
package com.iailab.module.model.mpk.common.utils;
import com.iailab.module.model.common.utils.ASCIIUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@@ -110,32 +110,48 @@
    }
    public static List<double[][]> readMethodExcel(MultipartFile file) throws IOException {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        //获取工作薄里面sheet的个数
        int sheetNum = workbook.getNumberOfSheets();
        List<double[][]> datas = new ArrayList<>(sheetNum - 1);
        //从第二个sheet开始读,第一个为示例
        for (int i = 1; i < sheetNum; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            //获取sheet中有数据的行数
            int rows = sheet.getPhysicalNumberOfRows();
            double[][] doubles2 = new double[rows][];
            for (int j = 0; j < rows; j++) {
                //获取每一行的数据
                Row row = sheet.getRow(j);
                //得到每一行中有效单元格的数据
                short cells = row.getLastCellNum();
                //定义一个doubles数组用来存储读取单元格的数据
                double[] doubles1 = new double[cells];
                //遍历每一个有效的单元格数据
                for (int k = 0; k < cells; k++) {
                    //获取每一个单元格的数据
                    Cell cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    doubles1[k] = cell.getNumericCellValue();
        List<double[][]> datas;
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            //获取工作薄里面sheet的个数
            int sheetNum = workbook.getNumberOfSheets();
            datas = new ArrayList<>(sheetNum - 1);
            //从第二个sheet开始读,第一个为示例
            for (int i = 1; i < sheetNum; i++) {
                Sheet sheet = workbook.getSheetAt(i);
                //获取sheet中有数据的行数
                int rows = sheet.getPhysicalNumberOfRows();
                double[][] doubles2 = new double[rows][];
                for (int j = 0; j < rows; j++) {
                    //获取每一行的数据
                    Row row = sheet.getRow(j);
                    //得到每一行中有效单元格的数据
                    short cells = row.getLastCellNum();
                    //定义一个doubles数组用来存储读取单元格的数据
                    double[] doubles1 = new double[cells];
                    //遍历每一个有效的单元格数据
                    for (int k = 0; k < cells; k++) {
                        //获取每一个单元格的数据
                        Cell cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                        CellType cellType = cell.getCellType();
                        if (cellType.equals(CellType.NUMERIC)) {
                            doubles1[k] = cell.getNumericCellValue();
                        } else if (cellType.equals(CellType.STRING)) {
                            // 校验数据格式
                            if (k != 0) {
                                throw new RuntimeException();
                            }
                            String stringCellValue = cell.getStringCellValue();
                            doubles1 = ASCIIUtil.stringToAsciiArray(stringCellValue);
                            break;
                        }
                    }
                    doubles2[j] = doubles1;
                }
                doubles2[j] = doubles1;
                datas.add(doubles2);
            }
            datas.add(doubles2);
        } catch (Exception e) {
            throw new RuntimeException("解析Excel异常", e);
        }
        return datas;