<!--easypoi依赖,excel导入导出-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
通过@Exce
注解设置标头名字和单元格宽度;
导出实体设置:
@Data
public class ExportOrder implements Serializable {
/**
* 订单号
*/
@Excel(name = "订单号", width = 10.0)
private String orderId;
/**
* 产品名称
*/
@Excel(name = "产品名称", width = 10.0)
private String productName;
/**
* 品牌
*/
@Excel(name = "品牌", width = 10.0)
private String brandName;
/**
* 分类
*/
@Excel(name = "分类", width = 10.0)
private String type;
/**
* 数量
*/
@Excel(name = "数量", width = 10.0)
private Long orderNumber;
/**
* 付款时间
*/
@Excel(name = "付款时间", width = 10.0)
private String payTime;
/**
* 付款金额
*/
@Excel(name = "付款金额", width = 10.0)
private BigDecimal payAmount;
/**
* 订单状态
*/
@Excel(name = "订单状态", width = 10.0)
private String status;
/**
* 操作
*/
@Excel(name = "操作", width = 10.0)
private String orderOpt;
/**
* 下单时间
*/
@Excel(name = "下单时间", width = 10.0)
private String createTime;
}
Controller层
@GetMapping("export")
public void exportUsers(OrderEntity orderEntity, HttpServletResponse response) {
orderManagerService.exportOrders(orderEntity,response);
}
Service层
@Override
public void exportOrders(OrderEntity orderEntity,HttpServletResponse response) {
try {
//从数据库查询到数据
List<OrderDao> orderDaoList = orderMapper.selectOrders(orderEntity);
List<SupplyChainOrdersVo> supplyChainOrdersVos = null;
if (CollectionUtil.isEmpty(orderDaoList)) {
log.error("order表中未查询到对应数据");
return;
}
supplyChainOrdersVos = new ArrayList<>(orderDaoList.size());
.......
//设置信息头,告诉浏览器内容为excel类型
response.setHeader("content-Type", "application/vnd.ms-excel");
//sheet名称
String sheetName = "订单";
response.setCharacterEncoding("UTF-8");
//设置下载名称
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("订单表.xls","UTF-8"));
//字节流输出
ServletOutputStream out = response.getOutputStream();
//设置excel参数
ExportParams params = new ExportParams();
//设置sheet名
params.setSheetName(sheetName);
//设置标题
params.setTitle("订单表");
//转成对应的类型;要不然会报错,虽然也可以导出成功。
List<ExportOrder> exportUsers = changeType(supplyChainOrdersVos);
//导入excel
Workbook workbook = ExcelExportUtil.exportExcel(params, ExportOrder.class,exportUsers);
//写入
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
导出效果
postman数据导出设置
%E8%AE%A2%E5%8D%95%E8%A1%A8.xls
导出的文件是转义的utf8编码,在浏览器上下载是正常的中文格式.
问题
以上设置完成文件还是有问题的话,大概率是前端的问题,可以找他对线了。
下载的文件打不开,需要前端设置一下请求的响应的格式:
responseType=“blob”