后台代码
中间层请求
@PostMapping("/orderExportExcel")
public void orderExportExcel(HttpServletResponse response, @RequestBody OrdDetailParamDTO ordDetailParamDTO) throws Exception {
List<OrdDetailExportDTO> queryList = orderOpurchaseClient.orderExportExcel(ordDetailParamDTO);
EasyExcelUtils.writeExcel(response, queryList, "订单明细列表", "订单明细列表");
}
controller层
@PostMapping("/exportExcel")
public List<OrdDetailExportDTO> exportDataToExcel(@RequestBody OrdDetailParamDTO ordDetailParamDTO) {
log.info("导出订单明细参数", JSON.toJSONString(ordDetailParamDTO));
List<OrdDetailExportDTO> queryList = new ArrayList<>();
try {
queryList = ordBBo.queryExportDetail(ordDetailParamDTO);
} catch (Exception e) {
e.printStackTrace();
log.error("导出竞价产品失败,{}", e);
}
return queryList;
}
工具类:EasyExcelUtils
package cn.hsa.tps.utils;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.beans.BeanUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class EasyExcelUtils {
protected static FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHH:mm:ss");
public static <T extends BaseRowModel> void baseExportExcel(String fileName, List cameraReportStopList, Class<T> entityModel, HttpServletResponse response) throws InstantiationException, IllegalAccessException {
List<BaseRowModel> modelList = new ArrayList<>();
if (StringUtils.isEmpty(cameraReportStopList)) {
return;
}
for (Object cameraReportStopVo : cameraReportStopList) {
BaseRowModel cameraReportStopModel = entityModel.newInstance();
BeanUtils.copyProperties(cameraReportStopVo, cameraReportStopModel);
modelList.add(cameraReportStopModel);
}
try {
writeExcel(response, modelList, fileName + fastDateFormat.format(new Date()), "第一页");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeExcel(HttpServletResponse response, List<? extends BaseRowModel> list,
String fileName, String sheetName) throws Exception {
ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX);
Class clazz = null;
if (list.size() > 0) {
clazz = list.get(0).getClass();
} else {
clazz = BaseRowModel.class;
}
Sheet sheet = new Sheet(1, 0, clazz);
sheet.setSheetName(sheetName);
writer.write(list, sheet);
writer.finish();
}
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
try {
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");
return response.getOutputStream();
} catch (IOException e) {
throw new Exception("导出excel表格失败!", e);
}
}
}
前台代码
exportData() {
let that = this;
axios({
method: "post",
url: process.env.VUE_APP_BASE_API + "/orderExportExcel",
responseType: "blob",
headers: {
Authorization: `${getToken()}`,
userid: Cookies.get("userid")
},
async: false,
data: that.queryData
}).then((res) => {
if (!res) {
return;
}
if (res.code == 666) {
console.log(res);
}
let blob = new Blob([res.data], {
type: "application/vnd.ms-excel"
});
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.setAttribute("download", decodeURI("订单明细一键导出表-" + Date.now()) + ".xlsx");
link.style.display = "none";
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
this.gridLoading = false;
}).catch((error) => {
that.gridLoading = false;
that.msgError("导出失败!");
return;
});
}