1.导入maven依赖
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi-web</artifactId>
<version>1.2.2</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
</exclusions>
</dependency>
2.新建测试实体类
User包含多个Hobby
public class User implements Serializable{
private static final long serialVersionUID = -1518670854283409543L;
@Excel(name="用户名",width = 20,needMerge=true)
private String name;
@Excel(name = "年龄",width = 10,needMerge=true)
private Integer age;
@ExcelCollection(name = "爱好信息")
private List<Hobby> hobbies;
//set,get方法省略
}
Hobby
public class Hobby implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4078668186442066291L;
@Excel(name = "爱好id", width = 10)
private Integer id;
@Excel(name = "爱好名称", width = 20)
private String name;
}
Controller
@RequestMapping("/excel")
public ModelAndView excel(HttpServletResponse response) throws IOException{
//新增集合保存多个爱好,模拟一对多(一个用户对应多个爱好)
List<Hobby> hobbies=new ArrayList<Hobby>();
Hobby hobby=new Hobby();
hobby.setId(1);
hobby.setName("打篮球");
hobbies.add(hobby);
Hobby hobby2=new Hobby();
hobby2.setId(2);
hobby2.setName("跑步");
hobbies.add(hobby2);
//保存用户
List<User> list = new ArrayList<User>();
User user = new User();
user.setName("张三");
user.setAge(19);
user.setHobbies(hobbies);
list.add(user);
User user2 = new User();
user2.setName("李四");
user2.setAge(23);
user2.setHobbies(hobbies);
list.add(user2);
//导出
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
// 导出文件名称
mv.addObject(NormalExcelConstants.FILE_NAME, "检查日志汇总");
// 注解对象Class
mv.addObject(NormalExcelConstants.CLASS, User.class);
// 自定义表格参数
//mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("检查日志汇总", "数量: " + 20 + " 时间: " + 12 + " 至 " + 10 + " 备注:0是通过,1是不通过 ", "检查日志"));
// 导出数据列表
mv.addObject(NormalExcelConstants.DATA_LIST, list);
return mv;
}
访问导出Excel结果
封装工具类
/**
* @author TANGSHUAI
* @date 2020年11月25日 下午4:11:27
* @Excel导出
*/
public class ExcelUtils {
/**
* 导出excel
*
* @param title 文件标题
* @param clazz 实体类型
* @param exportList 导出数据
* @param
* @return
*/
public static <T> ModelAndView export(String title, Class<T> clazz, List<T> exportList) {
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
mv.addObject(NormalExcelConstants.FILE_NAME, title);
mv.addObject(NormalExcelConstants.CLASS, clazz);
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, title));
mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
return mv;
}
}
Controller直接调用
@Operation(summary = "selectFixedAssetsPurchaseImport")
@PostMapping(value = "/selectFixedAssetsPurchaseImport", consumes = { "application/json" })
public ModelAndView selectFixedAssetsPurchaseImport(@RequestBody BaseSearchInputDto input) {
FixedAssetsPurchaseDto dto = new FixedAssetsPurchaseDto();
if (input != null) {
dto = JSONObject.parseObject(input.getFilter(), FixedAssetsPurchaseDto.class);
}
List<FixedAssetsPurchaseCostom> selectFixedAssetsPurchaseImport = this.fixedAssetsPurchaseService.selectFixedAssetsPurchaseImport(dto);
return ExcelUtils.export("固资采购", FixedAssetsPurchaseCostom.class, selectFixedAssetsPurchaseImport);
}