一,用框架生成增删改查模块
二,在实体类entity 需要导入导出的字段上加上注解@Excel
三,在controller类上继承jeecgboot通用controller
JeecgController
并且在JeecgController里增加导出模板的方法
/**
* 导出excel空模板
*
* @param request
*/
protected ModelAndView exportTemplateXls(HttpServletRequest request, T object, Class<T> clazz, String title) {
// Step.1 组装查询条件
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
// Step.2 获取空模板
List<T> exportList = new ArrayList<T>();
// Step.3 AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(NormalExcelConstants.FILE_NAME, title);
mv.addObject(NormalExcelConstants.CLASS, clazz);
//update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
ExportParams exportParams=new ExportParams(title + "电价配置导入模板表", "导出人:" + sysUser.getRealname(), title);
exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
//update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
mv.addObject(NormalExcelConstants.PARAMS,exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
return mv;
}
四,在Controller实现导入导出的接口 把POJO替换成加了@Excel的entity实体类
/**
* 导出excel
*
* @param request
* @param
*/
@RequestMapping(value = "/exportXls")
@ApiOperation("导出excel")
public ModelAndView exportXls(HttpServletRequest request, POJO pojo) {
return super.exportXls(request, POJO , POJO.class, "表的名字");
}
/**
* 导出excel模板
*
* @param request
* @param tbElectrovalence
*/
@RequestMapping(value = "/exportTemplateXls")
@ApiOperation("导出excel模板")
public ModelAndView exportTemplateXls(HttpServletRequest request, POJO pojo) {
return super.exportTemplateXls(request, POJO , POJO.class, "表的名字");
}
/**
* 通过excel导入数据
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
@ApiOperation("通过excel导入数据")
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
return super.importExcel(request, response, POJO.class);
}
五,测试导入导出成功