1.本次使用 Easypoi 进行导入导出 方便快捷
1.1 介绍 - Powered by MinDoc 这是 Easypoi 的官方api 文档
2.导入Maven 依赖
<!-- excel 依赖-->
<!--excle 导出导出-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
3.最重要的一步 resources文件夹下 创建文件目录 template 导入 自定义的excel 文件
3.自定义模板语法
4.自定义excel 模板
@Override
public void export(int id, HttpServletRequest request, HttpServletResponse response) {
LambdaUpdateWrapper<GoodsOrder> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(GoodsOrder::getId, id);
GoodsOrder order = orderMapper.selectOne(lambdaUpdateWrapper);
LambdaUpdateWrapper<OrderDetails> O_lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
O_lambdaUpdateWrapper.eq(OrderDetails::getOrderId, order.getId());
List<OrderDetails> orderDetails = orderDetailsMapper.selectList(O_lambdaUpdateWrapper);
HashMap<String, Object> map = new HashMap();
if (orderDetails.size() > 0) {
map.put("company", order.getCompany());
map.put("order_time", order.getOrderTime());
map.put("tel", orderDetails.get(0).getCompanyTel());
map.put("nums", order.getNum());
map.put("amount", order.getAmount());
map.put("carrier", orderDetails.get(0).getCarrier());
map.put("delivertel", orderDetails.get(0).getDeliverTel());
map.put("receiving", orderDetails.get(0).getReceiving());
//用于循环
map.put("goodsList", orderDetails);
}
try {
Workbook workbook = WorkbookFactory.create(getClass().getClassLoader().getResourceAsStream("template/zhien.xls"));
TemplateExportParams params = new TemplateExportParams();
params.setTemplateWb(workbook);
params.setScanAllsheet(true);
ExcelExportUtil.exportExcel(params, map);
OutputStream outputStream = response.getOutputStream();
// workbook.setSheetName(0, "sheet");
// CellStyle cellStyle = workbook.createCellStyle();
// cellStyle.setBorderRight(BorderStyle.THIN);
//设置编码格式
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//设置内容类型
response.setContentType("application/octet-stream");
//设置头及文件命名。
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("销售单.xls", StandardCharsets.UTF_8.name()));
workbook.write(outputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
5.前端Vue 进行导出
1.vue 前端 点击导出事情
const download = (id: any) => {
get_data_download(id).then((res: any) => {
if (res.size == 76) {
ElMessage.error("查询的时间段 没有数据");
} else {
ElMessage.success("下载完成,正在打开");
// console.log(res);
const blob = new Blob([res]);
const element = document.createElement("a");
// 配置文件名,默认是 txt,可以自定义,比如:操作日志.csv
element.download = "销售单" + ".xls";
element.style.display = "none";
element.href = URL.createObjectURL(blob);
document.body.append(element);
element.click();
URL.revokeObjectURL(element.href);
document.body.removeChild(element);
}
});
};
2.如果路由有 请求和响应拦截器 记得放行
/ 添加请求拦截器
service.interceptors.request.use(
(config) => {
// 在发送请求之前做些什么 token
const sign = Session.get('sign');
config.headers['sign'] = sign
if (config.url === '/order/export'|| config.url === '/orderDetails/get_data_download') {
config.responseType = 'blob'
}
let data = config.data || (config['data'] = []);
config.data = qs.stringify(data)
return config;
},
(error) => {
// 对请求错误做些什么
return Promise.reject(error);
}
);
// 添加响应拦截器
let isReFreshPadding = true;
service.interceptors.response.use(
(response) => {
// 对响应数据做点什么
const res = response.data;
if (response.config.url === '/order/export'|| response.config.url === '/orderDetails/get_data_download') {
return res
}
if (isReFreshPadding) {
if (response.data.code == -9999 || response.data.code == -9998) {
isReFreshPadding = false;
ElMessage.error(res["message"])
Session.remove("token")
router.push({
path: "/login"
})
localStorage.clear()
window.location.reload();
}
}
if (res.code == -1) {
isReFreshPadding = true;
ElMessage.error(res["msg"])
return res
}
if (res.code == 0) {
isReFreshPadding = true;
return res
}
},
(error) => {
// 对响应错误做点什么
if (error.message.indexOf('timeout') != -1) {
ElMessage.error('网络超时');
} else if (error.message == 'Network Error') {
ElMessage.error('网络连接错误');
}
return Promise.reject(error);
}
);