|
@@ -12,6 +12,7 @@ import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.zcxk.core.common.pojo.AjaxMessage;
|
|
|
import com.zcxk.core.utils.DateUtil;
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
@@ -31,13 +32,67 @@ import java.util.*;
|
|
|
public class EasyExcelUtil {
|
|
|
|
|
|
/**
|
|
|
- * @MethodName:Excel导出
|
|
|
- * @Description: TODO
|
|
|
- * @Param: path 导出基础路径 t实体名 excelName 文件名 data数据
|
|
|
- * @Return:
|
|
|
- * @Author: WYY
|
|
|
- * @Date: 2020/10/28
|
|
|
- **/
|
|
|
+ * 仅仅输出明细数据到excel文件, 增加自定义的头部
|
|
|
+ * @author Andy
|
|
|
+ * @date 15:02 2021/8/23
|
|
|
+ * @param response:
|
|
|
+ * @param excelName:
|
|
|
+ * @param head:
|
|
|
+ * @param list:
|
|
|
+ * @return void
|
|
|
+ **/
|
|
|
+ public static void excelWriteWithHeader(HttpServletResponse response, String excelName, List<List<String>> head, List list) throws IOException {
|
|
|
+ setHeader(excelName, response);
|
|
|
+ // 这里 需要指定写用哪个class去写,指定模板名称及数据
|
|
|
+ EasyExcel.write(response.getOutputStream())
|
|
|
+ .head(head)
|
|
|
+ .excelType(ExcelTypeEnum.XLSX)
|
|
|
+ .registerWriteHandler(new CustemhandlerUtils())
|
|
|
+ .registerWriteHandler(getStyleStrategy())
|
|
|
+ .sheet("sheet")
|
|
|
+ .doWrite(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 仅仅输出明细数据到excel文件, 增加自定义的头部
|
|
|
+ * @author Andy
|
|
|
+ * @date 15:02 2021/8/23
|
|
|
+ * @param path:
|
|
|
+ * @param excelName:
|
|
|
+ * @param head:
|
|
|
+ * @param list:
|
|
|
+ * @return java.lang.String
|
|
|
+ **/
|
|
|
+ public static String excelWriteWithHeader(String path, String excelName, List<List<String>> head, List list) {
|
|
|
+ String filePath = "";
|
|
|
+ String fileName = DateFormatUtils.format(new Date(), "yyyy/MM/dd") + File.separator;
|
|
|
+ fileName = fileName + excelName + System.currentTimeMillis() + ".xlsx";
|
|
|
+ try {
|
|
|
+ // 这里 需要指定写用哪个class去写,指定模板名称及数据
|
|
|
+ EasyExcel.write(path + fileName)
|
|
|
+ .head(head)
|
|
|
+ .excelType(ExcelTypeEnum.XLSX)
|
|
|
+ .registerWriteHandler(new CustemhandlerUtils())
|
|
|
+ .registerWriteHandler(getStyleStrategy())
|
|
|
+ .sheet("sheet")
|
|
|
+ .doWrite(list);
|
|
|
+ filePath = getPathFileName(path, fileName);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Excel导出
|
|
|
+ * @author Andy
|
|
|
+ * @date 10:05 2021/8/23
|
|
|
+ * @param path:
|
|
|
+ * @param t:
|
|
|
+ * @param excelName:
|
|
|
+ * @param data:
|
|
|
+ * @return java.lang.String
|
|
|
+ **/
|
|
|
public static String excelWrite(String path,Class t, String excelName,List data) {
|
|
|
String filePath = "";
|
|
|
String fileName = DateUtil.format(new Date(), "yyyy/MM/dd") + File.separator;
|
|
@@ -59,17 +114,19 @@ public class EasyExcelUtil {
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
- public static void excelWrite(HttpServletResponse response,Class t, String excelName, List data) throws Exception {
|
|
|
- String fileName = excelName +"-"+ DateUtil.format(new Date(), "yyyyMMddHHmmss") ;
|
|
|
- fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
- response.setContentType("application/vnd.ms-excel");
|
|
|
- response.setCharacterEncoding("utf8");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
|
|
|
- response.setHeader("Pragma", "public");
|
|
|
- response.setHeader("Cache-Control", "no-store");
|
|
|
- response.addHeader("Cache-Control", "max-age=0");
|
|
|
-
|
|
|
- EasyExcel.write(response.getOutputStream(), t)
|
|
|
+ /**
|
|
|
+ * 直接导出
|
|
|
+ * @author Andy
|
|
|
+ * @date 15:03 2021/8/23
|
|
|
+ * @param response:
|
|
|
+ * @param obj:
|
|
|
+ * @param excelName:
|
|
|
+ * @param data:
|
|
|
+ * @return void
|
|
|
+ **/
|
|
|
+ public static void excelWrite(HttpServletResponse response,Class obj, String excelName, List data) throws Exception {
|
|
|
+ setHeader(excelName, response);
|
|
|
+ EasyExcel.write(response.getOutputStream(), obj)
|
|
|
.excelType(ExcelTypeEnum.XLSX)
|
|
|
.registerWriteHandler(new CustemhandlerUtils())
|
|
|
.registerWriteHandler(EasyExcelUtil.getStyleStrategy())
|
|
@@ -131,6 +188,7 @@ public class EasyExcelUtil {
|
|
|
String pathFileName = uploadDir + "/" + fileName;
|
|
|
return ToolUtil.path(pathFileName);
|
|
|
}
|
|
|
+
|
|
|
private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
|
|
|
File desc = new File(uploadDir + File.separator + fileName);
|
|
|
|
|
@@ -174,6 +232,15 @@ public class EasyExcelUtil {
|
|
|
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private static void setHeader(String excelName, HttpServletResponse response) throws UnsupportedEncodingException {
|
|
|
+ String fileName = excelName +"-"+ DateUtil.format(new Date(), "yyyyMMddHHmmss") ;
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
|
|
|
+ response.setHeader("Pragma", "public");
|
|
|
+ response.setHeader("Cache-Control", "no-store");
|
|
|
+ response.addHeader("Cache-Control", "max-age=0");
|
|
|
+ }
|
|
|
|
|
|
}
|