root 2 vuotta sitten
vanhempi
commit
2526338e92

+ 13 - 0
api-common/pom.xml

@@ -18,10 +18,23 @@
         <sshd-netty.version>2.8.0</sshd-netty.version>
         <dom4j.version>2.1.3</dom4j.version>
         <gitlab4j-api.version>5.0.1</gitlab4j-api.version>
+        <poi.version>5.2.2</poi.version>
     </properties>
 
     <dependencies>
 
+        <!-- excel 处理库 -->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>${poi.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>${poi.version}</version>
+        </dependency>
+
         <!-- gitlab -->
         <dependency>
             <groupId>org.gitlab4j</groupId>

+ 245 - 0
api-common/src/main/java/api/common/util/ExcelUtil.java

@@ -0,0 +1,245 @@
+package api.common.util;
+
+
+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.InputStream;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <dependency>
+ * <groupId>org.apache.poi</groupId>
+ * <artifactId>poi</artifactId>
+ * <version>${poi.version}</version>
+ * </dependency>
+ * <dependency>
+ * <groupId>org.apache.poi</groupId>
+ * <artifactId>poi-ooxml</artifactId>
+ * <version>${poi-ooxml.version}</version>
+ * </dependency>
+ */
+public class ExcelUtil {
+
+    /**
+     * 获取 Excel 对象
+     */
+    public static Workbook getExcel(String path) {
+        try {
+            File excel = new File(path);
+            Workbook result;
+            //1 获取 Excel 文件输入流
+            String excelName = excel.getName();
+            String suffix = excelName.substring(excelName.lastIndexOf(".") + 1);
+            InputStream inputStream = Files.newInputStream(excel.toPath());
+
+            //2 获取 Workbook 对象,".xls"和".xlsx"文件不同。
+            if ("xls".equalsIgnoreCase(suffix)) {
+                result = new HSSFWorkbook(inputStream);
+            } else if ("xlsx".equalsIgnoreCase(suffix)) {
+                result = new XSSFWorkbook(inputStream);
+            } else {
+                throw new RuntimeException("文件" + excel.getAbsolutePath() + "不是 excel 文件");
+            }
+            inputStream.close();
+            return result;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 获取 Excel 对象
+     */
+    public static Workbook getExcel(InputStream inputStream, String suffix) {
+        try {
+            Workbook result;
+            //2 获取 Workbook 对象,".xls"和".xlsx"文件不同。
+            if ("xls".equalsIgnoreCase(suffix)) {
+                result = new HSSFWorkbook(inputStream);
+            } else if ("xlsx".equalsIgnoreCase(suffix)) {
+                result = new XSSFWorkbook(inputStream);
+            } else {
+                throw new RuntimeException("getExcel() 后缀为 " + suffix + " 的文件不是 excel 文件");
+            }
+            inputStream.close();
+            return result;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 获取 Sheet 对象
+     *
+     * @param excel
+     * @param name
+     * @return
+     */
+    public static Sheet getSheetByName(Workbook excel, String name) {
+        try {
+            Sheet sheet = excel.getSheet(name);
+            if (sheet == null) {
+                throw new RuntimeException("excel 不存在名为 " + name + "的 sheet !");
+            }
+            return sheet;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+
+    /**
+     * 获取 Sheet 对象
+     *
+     * @param excel
+     * @param index
+     * @return
+     */
+    public static Sheet getSheetByIndex(Workbook excel, int index) {
+        try {
+            Sheet sheet = excel.getSheetAt(index);
+            if (sheet == null) {
+                throw new RuntimeException("excel 不存在下标为 " + index + " 的 sheet !");
+            }
+            return sheet;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+
+    /**
+     * 读取 Excel 文件的某一行
+     *
+     * @param row 行数
+     * @return 结果列表
+     */
+    public static List<String> getRow(Sheet sheet, int row, int startColumn, int stopColumn) {
+        try {
+            List<String> result = new ArrayList<>();
+            //1 获取行
+            Row dataRow = sheet.getRow(row);
+            //2 开始解析每一列
+            for (int columnIndex = startColumn; columnIndex <= stopColumn; columnIndex++) {
+
+                Cell cell = dataRow.getCell(columnIndex);
+                CellType cellType = cell.getCellType();
+                String cellValue = "";
+                switch (cellType) {
+                    case NUMERIC:
+                        cellValue = String.valueOf(cell.getNumericCellValue());
+                        break;
+                    case STRING:
+                        cellValue = cell.getStringCellValue();
+                        break;
+                    case BLANK:
+                        cellValue = "";
+                        break;
+                    default:
+                        throw new RuntimeException("单元格类型为:" + cellType + ",暂时无法解析!");
+                }
+                result.add(cellValue);
+            }
+            return result;
+        } catch (RuntimeException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 读取 Excel 所有数据
+     *
+     * @return 二维数组
+     */
+    public static List<List<String>> getAll(Sheet sheet) {
+        try {
+
+            List<List<String>> table = new ArrayList<>();
+            int lastRowNum = sheet.getLastRowNum(); // (最后一行下标)即(总行数-1)(下标从0开始)
+            //1 循环获取行
+            for (int i = 0; i <= lastRowNum; i++) {
+                Row row = sheet.getRow(i);
+                short lastCellNum = row.getLastCellNum();// (最后一列下标+1)即(总列数)(下标从0开始)
+                //2 开始解析每一列
+                List<String> cellList = new ArrayList<>();
+                for (int columnIndex = 0; columnIndex < lastCellNum; columnIndex++) {
+                    Cell cell = row.getCell(columnIndex);
+                    CellType cellType = cell.getCellType();
+                    String cellValue;
+                    switch (cellType) {
+                        case NUMERIC:
+                            cellValue = String.valueOf(cell.getNumericCellValue());
+                            break;
+                        case STRING:
+                            cellValue = cell.getStringCellValue();
+                            break;
+                        case BLANK:
+                            cellValue = "";
+                            break;
+                        default:
+                            throw new RuntimeException("单元格类型为:" + cellType + ",暂时无法解析!");
+                    }
+                    cellList.add(cellValue);
+                }
+                table.add(cellList);
+            }
+            return table;
+        } catch (RuntimeException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 读取 Excel 文件中某一块二维表格
+     *
+     * @param stopRow    Integer.MAX_VALUE 代表到最后一行
+     * @param stopColumn Integer.MAX_VALUE 代表到最后一列
+     * @return 结果列表
+     */
+    public static List<List<String>> getTable(Sheet sheet, int startRow, int stopRow, int startColumn, int stopColumn) {
+        try {
+            List<List<String>> table = new ArrayList<>();
+            //1 循环获取行(下标从0开始)
+            if (stopRow == Integer.MAX_VALUE) {
+                stopRow = sheet.getLastRowNum();
+            }
+
+            for (int i = startRow; i <= stopRow; i++) {
+                Row row = sheet.getRow(i);
+                //2 开始解析每一列
+                List<String> cellList = new ArrayList<>();
+                if (stopColumn == Integer.MAX_VALUE) {
+                    stopColumn = row.getLastCellNum() - 1;
+                }
+                for (int columnIndex = startColumn; columnIndex <= stopColumn; columnIndex++) {
+                    Cell cell = row.getCell(columnIndex);
+                    CellType cellType = cell.getCellType();
+                    String cellValue;
+                    switch (cellType) {
+                        case NUMERIC:
+                            cellValue = String.valueOf(cell.getNumericCellValue());
+                            break;
+                        case STRING:
+                            cellValue = cell.getStringCellValue();
+                            break;
+                        case BLANK:
+                            cellValue = "";
+                            break;
+                        default:
+                            throw new RuntimeException("getTable() 单元格类型为 " + cellType + ",暂时无法解析!");
+                    }
+                    cellList.add(cellValue);
+                }
+                table.add(cellList);
+            }
+            return table;
+        } catch (RuntimeException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 30 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/ExcelController.java

@@ -0,0 +1,30 @@
+package com.css.simulation.resource.common.controller;
+
+import api.common.util.ExcelUtil;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+@RestController
+@RequestMapping("/excel")
+public class ExcelController {
+
+    @PostMapping(value = "/parse", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+    public List<List<String>> parseExcel(@RequestPart("excel") MultipartFile excel) {
+        try {
+            String suffix = excel.getName().split("\\.")[1];
+            InputStream inputStream = excel.getInputStream();
+            Workbook workbook = ExcelUtil.getExcel(inputStream, suffix);
+            Sheet sheet = ExcelUtil.getSheetByIndex(workbook, 0);
+            return ExcelUtil.getTable(sheet, 1, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/CameraCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/CameraCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 import api.common.pojo.common.ResponseBodyVO;
 import api.common.pojo.constants.LogConstants;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/ConfigCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/ConfigCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 
 import api.common.pojo.common.ResponseBodyVO;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/GpsCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/GpsCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 
 import api.common.pojo.common.ResponseBodyVO;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/LidarCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/LidarCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 
 import api.common.pojo.common.ResponseBodyVO;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/OgtCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/OgtCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 import api.common.pojo.common.ResponseBodyVO;
 import api.common.pojo.constants.LogConstants;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/VehicleCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/VehicleCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 
 import api.common.pojo.common.ResponseBodyVO;

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/model/ctrl/VehicleTempCtrl.java → simulation-resource-server/src/main/java/com/css/simulation/resource/model/controller/VehicleTempCtrl.java

@@ -1,4 +1,4 @@
-package com.css.simulation.resource.model.ctrl;
+package com.css.simulation.resource.model.controller;
 
 import api.common.pojo.common.ResponseBodyVO;
 import api.common.pojo.param.model.VehiclePageParam;