From 436e03fe73a19bd485e23f78da5d851bbfe85d25 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 08 八月 2023 20:14:44 +0800 Subject: [PATCH] 1 --- src/main/resources/templates/month.xlsx | 0 src/main/java/com/yssh/entity/ExcelHead.java | 29 +++++++++ src/main/resources/templates/week.xlsx | 0 src/main/resources/templates/day.xlsx | 0 pom.xml | 12 +++- src/main/resources/templates/溯源系统统计分析报表-计算.doc | 0 src/main/java/com/yssh/utils/ExcelUtils.java | 98 ++++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 3c63189..14bae42 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ <relativePath/> </parent> <!--鎵撳寘鎴恓ar銆亀ar--> - <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> diff --git a/src/main/java/com/yssh/entity/ExcelHead.java b/src/main/java/com/yssh/entity/ExcelHead.java new file mode 100644 index 0000000..ced1ca5 --- /dev/null +++ b/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; + + /** + * 鎺掗櫎鐨凷heet鍚嶇О锛堝涓敤閫楀彿闅斿紑锛� + * + * @return + */ + String excludeSheets() default ""; +} diff --git a/src/main/java/com/yssh/utils/ExcelUtils.java b/src/main/java/com/yssh/utils/ExcelUtils.java new file mode 100644 index 0000000..c3b81ba --- /dev/null +++ b/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澶存敞瑙g被 + * + * @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鏇挎崲涓篐ORIZONTAL + FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build(); + + // 鍐欏叆鏁版嵁 + map.forEach((k, v) -> excelWriter.fill(new FillWrapper(k, v), fillConfig, writeSheet)); + + // 缁撴潫鍐欏叆 + excelWriter.finish(); + } +} diff --git a/src/main/resources/templates/day.xlsx b/src/main/resources/templates/day.xlsx new file mode 100644 index 0000000..49a5786 --- /dev/null +++ b/src/main/resources/templates/day.xlsx Binary files differ diff --git a/src/main/resources/templates/month.xlsx b/src/main/resources/templates/month.xlsx new file mode 100644 index 0000000..c40e52f --- /dev/null +++ b/src/main/resources/templates/month.xlsx Binary files differ diff --git a/src/main/resources/templates/week.xlsx b/src/main/resources/templates/week.xlsx new file mode 100644 index 0000000..f35f229 --- /dev/null +++ b/src/main/resources/templates/week.xlsx Binary files differ diff --git "a/src/main/resources/templates/\346\272\257\346\272\220\347\263\273\347\273\237\347\273\237\350\256\241\345\210\206\346\236\220\346\212\245\350\241\250-\350\256\241\347\256\227.doc" "b/src/main/resources/templates/\346\272\257\346\272\220\347\263\273\347\273\237\347\273\237\350\256\241\345\210\206\346\236\220\346\212\245\350\241\250-\350\256\241\347\256\227.doc" new file mode 100644 index 0000000..639861d --- /dev/null +++ "b/src/main/resources/templates/\346\272\257\346\272\220\347\263\273\347\273\237\347\273\237\350\256\241\345\210\206\346\236\220\346\212\245\350\241\250-\350\256\241\347\256\227.doc" Binary files differ -- Gitblit v1.9.3