package com.moral.api.utils;
|
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.ExcelWriter;
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
import com.alibaba.fastjson.JSON;
|
import com.google.common.net.HttpHeaders;
|
import org.apache.poi.ss.formula.functions.T;
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
public class EasyExcelUtils {
|
//不创建对象的导出
|
public void noModleWrite(@RequestBody NoModelWriteData data, HttpServletResponse response) throws IOException {
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
try {
|
// response.setContentType("application/vnd.ms-excel");
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
response.setCharacterEncoding("utf-8");
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
String fileName = URLEncoder.encode(data.getFileName(), "UTF-8");
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
// 这里需要设置不关闭流
|
EasyExcel.write(response.getOutputStream()).head(head(data.getHeadMap())).sheet(data.getFileName()).doWrite(dataList(data.getDataList(), data.getDataStrMap()));
|
} catch (Exception e) {
|
// 重置response
|
response.reset();
|
response.setContentType("application/json");
|
response.setCharacterEncoding("utf-8");
|
Map<String, String> map = new HashMap<String, String>();
|
map.put("status", "failure");
|
map.put("message", "下载文件失败" + e.getMessage());
|
response.getWriter().println(JSON.toJSONString(map));
|
}
|
}
|
|
//创建对象的导出
|
public <T> void simpleWrite(@RequestBody SimpleWriteData data,Class<T> clazz, HttpServletResponse response) throws IOException {
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
// response.setContentType("application/vnd.ms-excel");
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
response.setCharacterEncoding("utf-8");
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
String fileName = URLEncoder.encode(data.getFileName(), "UTF-8");
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
EasyExcel.write(response.getOutputStream(), clazz).sheet(data.getFileName()).doWrite(data.getDataList());
|
}
|
|
//设置表头
|
private List<List<String>> head(String[] headMap) {
|
List<List<String>> list = new ArrayList<List<String>>();
|
|
for (String head : headMap) {
|
List<String> headList = new ArrayList<String>();
|
headList.add(head);
|
list.add(headList);
|
}
|
return list;
|
}
|
|
//设置导出的数据内容
|
private List<List<Object>> dataList(List<Map<String, Object>> dataList, String[] dataStrMap) {
|
List<List<Object>> list = new ArrayList<List<Object>>();
|
for (Map<String, Object> map : dataList) {
|
List<Object> data = new ArrayList<Object>();
|
for (int i = 0; i < dataStrMap.length; i++) {
|
data.add(map.get(dataStrMap[i]));
|
}
|
list.add(data);
|
}
|
return list;
|
}
|
}
|