|
|
@@ -0,0 +1,120 @@
|
|
|
+package com.platform.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
+import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
|
|
+import com.alibaba.excel.write.metadata.style.WriteFont;
|
|
|
+import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
|
|
+import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: 马超伟
|
|
|
+ * @CreateTime: 2025-09-08
|
|
|
+ * @Description:
|
|
|
+ * @Version: 1.0
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+public class EasyExcelUtil {
|
|
|
+
|
|
|
+ public static <T> List<T> importExcel(InputStream inputStream, Class<T> clazz) {
|
|
|
+ return EasyExcel.read(inputStream).head(clazz).sheet().doReadSync();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> void exportExcel(String fileName, List<T> data, HttpServletResponse response) throws Exception {
|
|
|
+ if (CollectionUtils.isEmpty(data)) {
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ T t = data.get(0);
|
|
|
+ // 设置响应内容类型
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+
|
|
|
+ // 设置文件名
|
|
|
+ String name = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xlsx");
|
|
|
+ //解决跨域问题
|
|
|
+ response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
+ // 创建表头样式
|
|
|
+ WriteCellStyle headWriteCellStyle = new WriteCellStyle();
|
|
|
+ // 设置表头居中对齐
|
|
|
+ headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置表头字体
|
|
|
+ WriteFont headWriteFont = new WriteFont();
|
|
|
+ headWriteFont.setFontHeightInPoints((short) 12);
|
|
|
+ headWriteFont.setBold(true);
|
|
|
+ headWriteCellStyle.setWriteFont(headWriteFont);
|
|
|
+
|
|
|
+ // 创建内容样式
|
|
|
+ WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
|
|
|
+ // 设置内容居中对齐
|
|
|
+ contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ // 初始化表格样式策略
|
|
|
+ HorizontalCellStyleStrategy horizontalCellStyleStrategy =
|
|
|
+ new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
|
|
|
+
|
|
|
+ // 使用EasyExcel写入数据
|
|
|
+ EasyExcel.write(response.getOutputStream(), t.getClass()).excelType(ExcelTypeEnum.XLSX)
|
|
|
+ .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
+ .sheet()
|
|
|
+ .doWrite(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载Excel模板
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @param clazz 模板对应的实体类
|
|
|
+ * @param response HttpServletResponse对象
|
|
|
+ * @param <T> 实体类泛型
|
|
|
+ * @throws Exception 异常
|
|
|
+ */
|
|
|
+ public static <T> void exportTemplate(String fileName, Class<T> clazz, HttpServletResponse response) throws Exception {
|
|
|
+ // 设置响应内容类型
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+
|
|
|
+ // 设置文件名
|
|
|
+ String name = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xlsx");
|
|
|
+ // 解决跨域问题
|
|
|
+ response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
+
|
|
|
+ // 创建表头样式
|
|
|
+ WriteCellStyle headWriteCellStyle = new WriteCellStyle();
|
|
|
+ // 设置表头居中对齐
|
|
|
+ headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置表头字体
|
|
|
+ WriteFont headWriteFont = new WriteFont();
|
|
|
+ headWriteFont.setFontHeightInPoints((short) 12);
|
|
|
+ headWriteFont.setBold(true);
|
|
|
+ headWriteCellStyle.setWriteFont(headWriteFont);
|
|
|
+
|
|
|
+ // 创建内容样式
|
|
|
+ WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
|
|
|
+ // 设置内容居中对齐
|
|
|
+ contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ // 初始化表格样式策略
|
|
|
+ HorizontalCellStyleStrategy horizontalCellStyleStrategy =
|
|
|
+ new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
|
|
|
+
|
|
|
+ // 使用EasyExcel写入空数据生成模板
|
|
|
+ EasyExcel.write(response.getOutputStream(), clazz)
|
|
|
+ .excelType(ExcelTypeEnum.XLSX)
|
|
|
+ .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
+ .registerWriteHandler(horizontalCellStyleStrategy)
|
|
|
+ .sheet("模板")
|
|
|
+ .doWrite(new ArrayList<>()); // 写入空列表,只生成表头
|
|
|
+ }
|
|
|
+
|
|
|
+}
|