|
|
@@ -0,0 +1,303 @@
|
|
|
+package org.jeecg.modules.utils.excel;
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.ExcelWriter;
|
|
|
+import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
+import com.alibaba.excel.write.metadata.fill.FillConfig;
|
|
|
+import com.alibaba.excel.write.metadata.fill.FillWrapper;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author bao
|
|
|
+ * @ClassName ExcelUtil.java
|
|
|
+ * @Description easyExcel工具类
|
|
|
+ * @Date 2021/8/3 11:03
|
|
|
+ **/
|
|
|
+public class ExcelUtil {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel(字段全部导出)
|
|
|
+ * 浏览器
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title
|
|
|
+ * @param head
|
|
|
+ * @param list
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelWeb(HttpServletResponse response, String title, Class head, List list) throws IOException {
|
|
|
+ exportExcelWeb(response, title, head, list, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel(字段全部导出)
|
|
|
+ * 本地
|
|
|
+ *
|
|
|
+ * @param filePath 文件导出路径
|
|
|
+ * @param title
|
|
|
+ * @param head
|
|
|
+ * @param list
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelLocal(String filePath, String title, Class head, List list) throws IOException {
|
|
|
+ exportExcelLocal(filePath, title, head, list, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel(按照模板导出)
|
|
|
+ * 本地
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title
|
|
|
+ * @param templateUri
|
|
|
+ * @param list
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelByTemplateLocal(HttpServletResponse response, String title, String templateUri, Map map, List list) throws IOException {
|
|
|
+ exportExcelByTemplateLocal(response, title, templateUri, map, list, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel(按照模板导出)
|
|
|
+ * 远程
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title
|
|
|
+ * @param templateUri
|
|
|
+ * @param list
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelByTemplateWeb(HttpServletResponse response, String title, String templateUri, Map map, List list) throws IOException {
|
|
|
+ exportExcelByTemplateWeb(response, title, templateUri, map, list, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出功能(过滤需要倒数的属性)
|
|
|
+ * 本地
|
|
|
+ *
|
|
|
+ * @param path 文件路径
|
|
|
+ * @param title 导出文件的名称
|
|
|
+ * @param head 导出数据的类
|
|
|
+ * @param list 需要导出的数据列表
|
|
|
+ * @param includeColumnFiledNames 需要导出的字段属性
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelLocal(String path, String title, Class head, List list, Set<String> includeColumnFiledNames) throws IOException {
|
|
|
+ String file = path + File.separator + URLEncoder.encode(title, "UTF-8");
|
|
|
+ if (includeColumnFiledNames == null) {
|
|
|
+ EasyExcel.write(file, head).sheet("Sheet1").doWrite(list);
|
|
|
+ } else {
|
|
|
+ EasyExcel.write(file, head).includeColumnFiledNames(includeColumnFiledNames).sheet("Sheet1")
|
|
|
+ .doWrite(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出功能(过滤需要倒数的属性)
|
|
|
+ * 浏览器
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title 导出文件的名称
|
|
|
+ * @param head 导出数据的类
|
|
|
+ * @param list 需要导出的数据列表
|
|
|
+ * @param includeColumnFiledNames 需要导出的字段属性
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelWeb(HttpServletResponse response, String title, Class head, List list, Set<String> includeColumnFiledNames) throws IOException {
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode(title, "UTF-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+ if (includeColumnFiledNames == null) {
|
|
|
+ EasyExcel.write(response.getOutputStream(), head).sheet("Sheet1")
|
|
|
+ .doWrite(list);
|
|
|
+ } else {
|
|
|
+ EasyExcel.write(response.getOutputStream(), head).includeColumnFiledNames(includeColumnFiledNames).sheet("Sheet1")
|
|
|
+ .doWrite(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出csv
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title
|
|
|
+ * @param head
|
|
|
+ * @param list
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportCsvWeb(HttpServletResponse response, String title, Class head, List list) throws IOException {
|
|
|
+ exportCsvWeb(response, title, head, list, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出csv
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title
|
|
|
+ * @param head
|
|
|
+ * @param list
|
|
|
+ * @param includeColumnFiledNames
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportCsvWeb(HttpServletResponse response, String title, Class head, List list, Set<String> includeColumnFiledNames) throws IOException {
|
|
|
+ response.setContentType("text/csv");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode(title, "UTF-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".csv");
|
|
|
+ if (includeColumnFiledNames == null) {
|
|
|
+ EasyExcel.write(response.getOutputStream(), head).sheet("Sheet1")
|
|
|
+ .doWrite(list);
|
|
|
+ } else {
|
|
|
+ EasyExcel.write(response.getOutputStream(), head).includeColumnFiledNames(includeColumnFiledNames).sheet("Sheet1")
|
|
|
+ .doWrite(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按照模板导出(模板的上半部分是固定的数据,下半部分是一个不定数量的列表,使用时请注意场景是否一样)
|
|
|
+ * 本地
|
|
|
+ * 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
|
|
+ * {} 代表普通变量 {.} 代表是list的变量
|
|
|
+ * {data.xx} 代表对象的变量
|
|
|
+ * 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
|
|
+ * forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
|
|
+ * 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title 导出文件的名称
|
|
|
+ * @param templateUri 模板在资源文件夹下的全路径
|
|
|
+ * @param map 上半部分的固定数据,默认new HashMap(),不属于list的数据,如:表头、末尾统计,操作这里
|
|
|
+ * @param list 下半部分的列表数据
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void exportExcelByTemplateLocal(HttpServletResponse response, String title, String templateUri, Map map, List list, boolean isWeb) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource(templateUri);
|
|
|
+ inputStream = classPathResource.getInputStream();
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode(title, "UTF-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream).build();
|
|
|
+ WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
+ FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
|
+ excelWriter.fill(map, writeSheet);
|
|
|
+ excelWriter.fill(new FillWrapper("data", list), fillConfig, writeSheet);
|
|
|
+ excelWriter.finish();
|
|
|
+ response.flushBuffer();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按照模板导出(模板的上半部分是固定的数据,下半部分是一个不定数量的列表,使用时请注意场景是否一样)
|
|
|
+ * 远程
|
|
|
+ * 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
|
|
+ * {} 代表普通变量 {.} 代表是list的变量
|
|
|
+ * {data.xx} 代表对象的变量
|
|
|
+ * 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
|
|
+ * forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
|
|
+ * 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param title 导出文件的名称
|
|
|
+ * @param templateUri 模板在资源文件夹下的全路径
|
|
|
+ * @param map 上半部分的固定数据,默认new HashMap(),不属于list的数据,如:表头、末尾统计,操作这里
|
|
|
+ * @param list 下半部分的列表数据
|
|
|
+ */
|
|
|
+ public static void exportExcelByTemplateWeb(HttpServletResponse response, String title, String templateUri, Map map, List list, boolean isWeb) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ // 统一资源
|
|
|
+ URL url = new URL(templateUri);
|
|
|
+ // 连接类的父类,抽象类
|
|
|
+ URLConnection urlConnection = url.openConnection();
|
|
|
+ // http的连接类
|
|
|
+ HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
|
|
|
+ //设置超时
|
|
|
+ httpURLConnection.setConnectTimeout(1000 * 5);
|
|
|
+ //设置请求方式,默认是GET
|
|
|
+ httpURLConnection.setRequestMethod("GET");
|
|
|
+ // 设置字符编码
|
|
|
+ httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
|
|
+ // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
|
|
|
+ httpURLConnection.connect();
|
|
|
+ inputStream = httpURLConnection.getInputStream();
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode(title, "UTF-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream).build();
|
|
|
+ WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
+ FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
|
+ excelWriter.fill(map, writeSheet);
|
|
|
+ excelWriter.fill(new FillWrapper("data", list), fillConfig, writeSheet);
|
|
|
+ excelWriter.finish();
|
|
|
+ response.flushBuffer();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入功能
|
|
|
+ * 本地
|
|
|
+ *
|
|
|
+ * @param file 文件准确路径
|
|
|
+ * @param clazz 导入数据的对象
|
|
|
+ * @param importDataListener 自定义存储接口
|
|
|
+ * @return 返回导入数据的条数
|
|
|
+ */
|
|
|
+ public static int importExcelLocal(String file, Class clazz, Integer headRowNumber, ImportDataListener importDataListener) {
|
|
|
+ EasyExcel.read(file, clazz, importDataListener).sheet().headRowNumber(headRowNumber).doRead();
|
|
|
+ return importDataListener.getTotal();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入功能
|
|
|
+ * 浏览器
|
|
|
+ *
|
|
|
+ * @param inputStream
|
|
|
+ * @param clazz 导入数据的对象
|
|
|
+ * @param headRowNumber 头部所占行数
|
|
|
+ * @param importDataListener 自定义存储接口
|
|
|
+ * @return 返回导入数据的条数
|
|
|
+ */
|
|
|
+ public static int importExcel(InputStream inputStream, Class clazz, Integer headRowNumber, ImportDataListener importDataListener) {
|
|
|
+ EasyExcel.read(inputStream, clazz, importDataListener).sheet().headRowNumber(headRowNumber).doRead();
|
|
|
+ return importDataListener.getTotal();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|