- 准备工作把自己需要需要导出的表格
- 第一行为一个一对多的数据结构 在需要填入值的框里 {属性名}
- 第三行以后对应的为一个数组 {.属性名}
准备工作做好以后把表格(.xlxs)放入静态资源文件夹里面
代码如下
@Override
public ResultDTO downloadOfferList(OfferDownlod bc31) throws Exception {
List<OfferDownlod> dataset = null;
//一对一的数据
Map<String, Object> map = new HashMap<>();
map.put("Businesstype", "C3-人民币跨行代付(多对多)");
map.put("Outaccount",bc31.getBab084());//转出账号
map.put("Bizhon", "CNY-人民币");
map.put("BusinessSummary", "EV-工资");
String fileName="";
//数组的数据来源
dataset = bc31Mapper.queryBpBc31List(bc31);
//刚刚准备好的表格文件
fileName="/file/hangwai.xlsx";
if(!StringUtils.isEmpty(dataset)){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = sra.getResponse();
response.setContentType("multipart/form-data;charset=UTF-8");
response.setHeader("Accept-Language", "zh-cn");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("基础信息.xlsx", "UTF-8"));
OutputStream os = response.getOutputStream();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(inputStream).build();
//创建第一个sheet
WriteSheet bc09Sheet = EasyExcel.writerSheet("Sheet1").build();
//填充
excelWriter.fill(dataset, fillConfig, bc09Sheet);
excelWriter.fill(map, bc09Sheet);
excelWriter.finish();
return new ResultDTO(0, "查询成功");
}else{
return new ResultDTO(0,"查询成功,暂无数据");
}
}
下载文件得