1. 实体类字段上加 @Excel(name = "xxx"), 表示要导出的字段
@Excel(name = "订单号")
private String orderNo;
2. controller (get请求)
/**
* 导出订单列表
*/
@ApiOperation("导出订单列表")
@GetMapping("/export")
public void export(HttpServletResponse response, LsOrder lsOrder)
{
List<LsOrder> list = lsOrderService.selectLsOrderList(lsOrder);
ExcelUtil<LsOrder> util = new ExcelUtil<LsOrder>(LsOrder.class);
util.exportExcel(response, list, "订单数据");
}
3. 调用exportExcel方法, 输出到浏览器 (导出的所有文件名都是export.xlsx, 不是我想要的)
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @return 结果
*/
public void exportExcel(HttpServletResponse response)
{
try
{
writeSheet();
wb.write(response.getOutputStream());
}
catch (Exception e)
{
log.error("导出Excel异常{}", e.getMessage());
}
finally
{
IOUtils.closeQuietly(wb);
}
}
4. 修改exportExcel方法, 浏览器输出文件, 动态文件名
新增一行
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("文件名称", "utf-8") + ".xlsx");
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @return 结果
*/
public void exportExcel(HttpServletResponse response)
{
try
{
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(sheetName+"-"+System.currentTimeMillis(), "utf-8") + ".xlsx");
writeSheet();
wb.write(response.getOutputStream());
}
catch (Exception e)
{
log.error("导出Excel异常{}", e.getMessage());
}
finally
{
IOUtils.closeQuietly(wb);
}
}