Parcourir la source

1.数据库修改、poi配置

GouGengquan il y a 9 mois
Parent
commit
095715b1a6

+ 151 - 2
biz-base/src/test/java/com/dayou/BaseApplicationTests.java

@@ -1,11 +1,22 @@
 package com.dayou;
 
 import cn.hutool.core.lang.Console;
-import cn.hutool.http.Header;
 import cn.hutool.http.HttpRequest;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddressList;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
 @SpringBootTest
 class BaseApplicationTests {
 
@@ -14,7 +25,7 @@ class BaseApplicationTests {
 
     }
 
-    @Test
+    // @Test
     void hutoolPostTest(){
         //链式构建请求
         String result = HttpRequest.post("localhost:8088/api/auth/checkOaToken?token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzI5MDc1MTI5LCJpYXQiOjE3Mjg5ODg3Mjl9.KC1n8xRJBfvJL4C0wQXZ9xRQX_opisIznyDkWHW3NxyDvEEM3K-bm7cnJ7BLYNCroOEbaEj6V5OdQ-YK2RGERg")
@@ -24,4 +35,142 @@ class BaseApplicationTests {
         Console.log(tokenStatus);
     }
 
+    /**
+     * 测试使用EasyExcel填充值到带公式的excel模板
+     */
+    // @Test
+    void testEasyExcel(){
+        // 模板文件路径
+        String templateFilePath = "E:\\test\\input.xlsx";
+        // 输出文件路径
+        String outputPath = "E:\\test\\output.xlsx";
+
+        // 创建一个Map来存储要填充的数据
+        Map<String, Object> dataMap = new HashMap<>();
+        dataMap.put("a", 13.7);
+        dataMap.put("b", 46.9);
+
+        // 使用EasyExcel填充数据
+        ExcelWriter excelWriter = EasyExcel.write(outputPath)
+                .withTemplate(templateFilePath)
+                .inMemory(true) // 启用内存模式
+                .build();
+        WriteSheet writeSheet = EasyExcel.writerSheet().build();
+        excelWriter.fill(dataMap, writeSheet); // 填充数据
+
+        // 由于填充后不会自动更新公式值,所以此处需要手动更新,大文件慎用(可能会导致内存溢出)
+        Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();
+        workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); // 强制计算公式
+
+        excelWriter.finish();
+    }
+
+    /**
+     * 测试使用poi合并Excel文件
+     * 大概就是读取模板文件的行信息(包括数据,样式等等),再写入到目标文件中
+     */
+    // @Test
+    void testExcelMerge(){
+        String sourceExcelPath = "E:\\test\\source.xlsx"; // 源Excel文件路径
+        String targetExcelPath = "E:\\test\\target.xlsx"; // 目标Excel文件路径
+        int targetSheetIndex = 0; // 目标sheet索引
+        int targetRowNum = 4; // 目标行索引
+
+        // 加载源Excel工作簿
+        try (InputStream inputStream = Files.newInputStream(Paths.get(sourceExcelPath))) {
+            Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
+            Sheet sourceSheet = sourceWorkbook.getSheetAt(0); // 读取第一个sheet
+
+            // 打开已存在的Excel工作簿
+            Workbook targetWorkbook = WorkbookFactory.create(Files.newInputStream(Paths.get(targetExcelPath)));
+            Sheet targetSheet = targetWorkbook.getSheetAt(targetSheetIndex); // 获取目标sheet
+
+            // 复制样式映射
+            Map<Short, Short> styleMap = copyCellStyle(sourceWorkbook, targetWorkbook);
+
+            // 复制行和单元格
+            for (Row sourceRow : sourceSheet) {
+                Row targetRow = targetSheet.createRow(targetRowNum + sourceRow.getRowNum());
+                for (Cell sourceCell : sourceRow) {
+                    Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
+                    copyCell(sourceCell, targetCell, targetWorkbook, styleMap);
+                }
+            }
+
+            // 复制数据验证
+            copyDataValidations(sourceSheet, targetSheet);
+
+            // 保存目标Excel工作簿
+            try (FileOutputStream outputStream = new FileOutputStream(targetExcelPath)) {
+                targetWorkbook.write(outputStream);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 复制单元格样式
+     * @param srcBook
+     * @param desBook
+     * @return
+     */
+    private static Map<Short, Short> copyCellStyle(Workbook srcBook, Workbook desBook) {
+        Map<Short, Short> styleMap = new HashMap<>();
+        for (short i = 0; i < srcBook.getNumCellStyles(); i++) {
+            CellStyle srcStyle = srcBook.getCellStyleAt(i);
+            CellStyle desStyle = desBook.createCellStyle();
+            desStyle.cloneStyleFrom(srcStyle);
+            styleMap.put(srcStyle.getIndex(), desStyle.getIndex());
+        }
+        return styleMap;
+    }
+
+    /**
+     * 复制单元格
+     * @param srcCell
+     * @param desCell
+     * @param targetWorkbook
+     * @param styleMap
+     */
+    private static void copyCell(Cell srcCell, Cell desCell, Workbook targetWorkbook, Map<Short, Short> styleMap) {
+        // 判断单元格值类型
+        if (srcCell.getCellType() == CellType.NUMERIC) {
+            desCell.setCellValue(srcCell.getNumericCellValue());
+        } else if (srcCell.getCellType() == CellType.STRING) {
+            desCell.setCellValue(srcCell.getStringCellValue());
+        } else if (srcCell.getCellType() == CellType.BOOLEAN) {
+            desCell.setCellValue(srcCell.getBooleanCellValue());
+        } else if (srcCell.getCellType() == CellType.BLANK) {
+            desCell.setBlank();
+        } else if (srcCell.getCellType() == CellType.FORMULA) {
+            desCell.setCellFormula(srcCell.getCellFormula());
+        }
+
+        // 复制样式
+        desCell.setCellStyle(targetWorkbook.getCellStyleAt(styleMap.get(srcCell.getCellStyle().getIndex())));
+    }
+
+    /**
+     * 复制数据验证
+     * @param sourceSheet
+     * @param targetSheet
+     */
+    private static void copyDataValidations(Sheet sourceSheet, Sheet targetSheet) {
+        for (DataValidation validation : sourceSheet.getDataValidations()) {
+            // 设置要设置数据验证的单元格坐标
+            CellRangeAddressList regions = new CellRangeAddressList(validation.getRegions().getCellRangeAddress(0).getFirstRow() + 4,
+                    validation.getRegions().getCellRangeAddress(0).getLastRow() + 4,
+                    validation.getRegions().getCellRangeAddress(0).getFirstColumn(),
+                    validation.getRegions().getCellRangeAddress(0).getLastColumn());
+            // 获取数据源的数据验证信息
+            DataValidationHelper validationHelper = targetSheet.getDataValidationHelper();
+            DataValidationConstraint constraint = validation.getValidationConstraint();
+            // 新建数据验证
+            DataValidation newValidation = validationHelper.createValidation(constraint, regions);
+            // 在目标excel中设置数据验证
+            targetSheet.addValidationData(newValidation);
+        }
+    }
+
 }

+ 22 - 0
common/pom.xml

@@ -54,6 +54,28 @@
             <groupId>cn.hutool</groupId>
             <artifactId>hutool-all</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+        </dependency>
+
+
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 4 - 2
domain/src/main/java/com/dayou/dto/LoginByOADTO.java

@@ -2,6 +2,8 @@ package com.dayou.dto;
 
 import lombok.Data;
 
+import java.util.List;
+
 @Data
 public class LoginByOADTO {
 
@@ -23,12 +25,12 @@ public class LoginByOADTO {
     /**
      * 部门id
      */
-    private String departmentIds;
+    private List<Long> departmentIds;
 
     /**
      * 岗位id
      */
-    private String postIds;
+    private List<Long> postIds;
 
     /**
      * oa系统token

+ 0 - 10
domain/src/main/java/com/dayou/entity/User.java

@@ -34,15 +34,5 @@ public class User extends BaseEntity implements Serializable {
      */
     private String staffNo;
 
-    /**
-     * 部门id
-     */
-    private String departmentIds;
-
-    /**
-     * 岗位id
-     */
-    private String postIds;
-
 
 }

+ 28 - 0
pom.xml

@@ -32,6 +32,10 @@
         <pagehelper.version>2.1.0</pagehelper.version>
         <commons-fileupload.version>1.5</commons-fileupload.version>
         <sa-token.version>1.39.0</sa-token.version>
+        <easyexcel.vsersion>4.0.3</easyexcel.vsersion>
+        <poi.vsersion>5.2.3</poi.vsersion>
+        <poi-ooxml.version>5.2.3</poi-ooxml.version>
+        <commons-io.version>2.15.0</commons-io.version>
     </properties>
 
     <dependencyManagement>
@@ -108,6 +112,30 @@
                 <version>${sa-token.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>com.alibaba</groupId>
+                <artifactId>easyexcel</artifactId>
+                <version>${easyexcel.vsersion}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>org.apache.poi</groupId>
+                <artifactId>poi</artifactId>
+                <version>${poi.vsersion}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>org.apache.poi</groupId>
+                <artifactId>poi-ooxml</artifactId>
+                <version>${poi-ooxml.version}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>commons-io</groupId>
+                <artifactId>commons-io</artifactId>
+                <version>${commons-io.version}</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 

+ 52 - 0
sql/update_sql.sql

@@ -0,0 +1,52 @@
+/**
+  更新的SQL
+ */
+
+/**
+  日期:2024-10-18
+  修改人:苟耕铨
+  未更新到test-env
+ */
+DROP TABLE IF EXISTS `assets_calculate`;
+CREATE TABLE `assets_calculate` (
+                                    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '资产项目测算表id',
+                                    `assets_id` bigint(20) NOT NULL COMMENT '资产项目id',
+                                    `calculate_name` varchar(255) DEFAULT NULL COMMENT '测算名',
+                                    `valuation_basis_date` date DEFAULT NULL COMMENT '评估基准日',
+                                    `create_user_id` bigint(20) DEFAULT NULL COMMENT '创建项目user_id',
+                                    `calculate_info` json DEFAULT NULL COMMENT '测算信息',
+                                    `calculate_file_name` varchar(255) DEFAULT NULL COMMENT '测算表文件名',
+                                    `calculate_file_url` varchar(500) DEFAULT NULL COMMENT '测算表文件存储位置',
+                                    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
+                                    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
+                                    `delete_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除状态',
+                                    PRIMARY KEY (`id`),
+                                    KEY `assets_id` (`assets_id`,`create_user_id`) USING BTREE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='资产项目测算信息表';
+
+DROP TABLE IF EXISTS `assets`;
+CREATE TABLE `assets` (
+                          `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '资产项目id',
+                          `project_name` varchar(500) DEFAULT NULL COMMENT '项目名称',
+                          `project_type_id` bigint(20) DEFAULT NULL COMMENT '项目类型id(字典表)',
+                          `create_user_id` bigint(20) DEFAULT NULL COMMENT '创建项目user_id',
+                          `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
+                          `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
+                          `delete_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除状态',
+                          PRIMARY KEY (`id`),
+                          KEY `project_type_id` (`project_type_id`,`create_user_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+DROP TABLE IF EXISTS `user_post`;
+CREATE TABLE `user_post` (
+                             `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户岗位-部门信息id',
+                             `user_oa_id` bigint(20) unsigned DEFAULT NULL COMMENT '用户OA id',
+                             `post_name` varchar(100) DEFAULT NULL COMMENT '岗位名称',
+                             `department_name` varchar(100) DEFAULT NULL COMMENT '部门名称',
+                             `role` varchar(50) DEFAULT NULL COMMENT '用户角色',
+                             `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
+                             `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
+                             `delete_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除状态',
+                             PRIMARY KEY (`id`),
+                             KEY `user_oa_id` (`user_oa_id`) USING BTREE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;