燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-08-08 436e03fe73a19bd485e23f78da5d851bbfe85d25
1
已添加6个文件
已修改1个文件
139 ■■■■■ 文件已修改
pom.xml 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/entity/ExcelHead.java 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/utils/ExcelUtils.java 98 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/templates/day.xlsx 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/templates/month.xlsx 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/templates/week.xlsx 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/templates/溯源系统统计分析报表-计算.doc 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -10,7 +10,7 @@
        <relativePath/>
    </parent>
    <!--打包成jar、war-->
    <packaging>jar</packaging>
    <packaging>war</packaging>
    <groupId>com</groupId>
    <artifactId>yssh</artifactId>
@@ -53,10 +53,10 @@
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除内置tomcat容器,让外部容器运行spring-boot项目-->
            <exclusions>
                <!--exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion-->
                </exclusion>
                <!--<exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
@@ -286,6 +286,12 @@
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!--easyexcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>
    </dependencies>
    <profiles>
src/main/java/com/yssh/entity/ExcelHead.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,29 @@
package com.yssh.entity;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Excel标题头类
 * @author WWW
 * @date 2023-08-08
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelHead {
    /**
     * Excel头行数
     *
     * @return å¤´è¡Œæ•°
     */
    int headRows() default 1;
    /**
     * æŽ’除的Sheet名称(多个用逗号隔开)
     *
     * @return
     */
    String excludeSheets() default "";
}
src/main/java/com/yssh/utils/ExcelUtils.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,98 @@
package com.yssh.utils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.yssh.entity.ExcelHead;
import java.util.*;
/**
 * Excel帮助类
 * @author WWW
 * @date 2023-08-08
 */
public class ExcelUtils {
    /**
     * è¯»å–Excel
     *
     * @param pathName æ–‡ä»¶è·¯å¾„
     * @param <T>      æ³›åž‹ç±»
     * @return æ³›åž‹ç±»é›†åˆ
     */
    public static <T> List<T> readExcel(Class<?> clazz, String pathName) {
        ExcelHead head = getExcelHead(clazz);
        int headRowNumber = head == null ? 1 : head.headRows();
        String[] strs = null == head || StringUtils.isEmpty(head.excludeSheets()) ? null : head.excludeSheets().split(",");
        List<String> excludeSheets = null == strs ? null : Arrays.asList(strs);
        List<T> list = new ArrayList<T>();
        ExcelReader reader = EasyExcel.read(pathName, clazz, new AnalysisEventListener<T>() {
            @Override
            public void invoke(T t, AnalysisContext context) {
                list.add(t);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                //
            }
        }).headRowNumber(headRowNumber).build();
        List<ReadSheet> sheets = reader.excelExecutor().sheetList();
        for (ReadSheet sheet : sheets) {
            if (strs != null && excludeSheets.contains(sheet.getSheetName())) {
                continue;
            }
            reader.read(sheet);
        }
        reader.finish();
        return list;
    }
    /**
     * èŽ·å–Excel头注解类
     *
     * @param clazz Class
     * @param <T>   æ³›åž‹ç±»
     * @return å¤´è¡Œæ•°
     */
    public static <T> ExcelHead getExcelHead(Class<?> clazz) {
        ExcelHead head = clazz.getAnnotation(ExcelHead.class);
        return head;
    }
    /**
     * å†™å…¥Excel模板
     *
     * @param source æºæ–‡ä»¶ï¼ˆæ¨¡æ¿ï¼‰
     * @param target ç›®å½•文件
     * @param map    æ•°æ®æº
     */
    public static <T> void writeToTemplate(String source, String target, Map<String, List<T>> map) {
        // æ ¹æ®æ¨¡æ¿å†™å…¥æ•°æ®ï¼Œå¦‚果目标文件不存在,则自动创建文件
        ExcelWriter excelWriter = EasyExcel.write(target).withTemplate(source).build();
        // åœ¨å·¥ä½œç°¿0中写入数据,如果模板中不存在练习工作簿,则会在目标文件中自动创建
        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
        // åž‚直写入数据,如果要水平写入,将VERTICAL替换为HORIZONTAL
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
        // å†™å…¥æ•°æ®
        map.forEach((k, v) -> excelWriter.fill(new FillWrapper(k, v), fillConfig, writeSheet));
        // ç»“束写入
        excelWriter.finish();
    }
}
src/main/resources/templates/day.xlsx
Binary files differ
src/main/resources/templates/month.xlsx
Binary files differ
src/main/resources/templates/week.xlsx
Binary files differ
src/main/resources/templates/ËÝԴϵͳͳ¼Æ·ÖÎö±¨±í-¼ÆËã.doc
Binary files differ