用poi导出Excel表格,需要配置很多东西,也比较麻烦,这里使用poi的封装easypoi,可以快速配置,实现Excel或者word文件的导出。这里我们结合SpringMVC开发easypoi。
1,导入以下3个.jar包:这里是springMVC和easypoi所需的jar包,主要是easypoi-base和easypoi-web,其它都是关联所需的jar包,我们需要commons-lang3.jar包,开始使用commons-lang2.6版本会出现错误。
2,spring-mvc.xml配置如下:
1.ExcelExportUtil Excel导出(普通导出,模板导出) 2.ExcelImportUtil Excel导入 3.WordExportUtil Word导出(只支持docx ,doc版本poi
3 Controller
导入方式,可以根据映射关系,通过注解,字段名符合导入,如下例
public class RechannelreportEntity implements java.io.Serializable { /**id*/ private java.lang.Integer id; /**amount*/ @Excel(name="amount") private java.lang.Integer amount; /**amounts*/ @Excel(name="amounts") private java.lang.Integer amounts; /**counts*/ @Excel(name="counts") private java.lang.Integer counts; /**date*/ @Excel(name="date",format = "yyyy-MM-dd") private java.util.Date date; /**username*/ .......
@SuppressWarnings("unchecked") @RequestMapping(params = "importExcel", method = RequestMethod.POST) @ResponseBody public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MapfileMap = multipartRequest.getFileMap(); for (Map.Entry entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 ImportParams params = new ImportParams(); params.setTitleRows(2); params.setHeadRows(1); params.setNeedSave(true); try { List listRechannelreportEntitys = ExcelImportUtil.importExcel(file.getInputStream(),RechannelreportEntity.class,params); for (RechannelreportEntity rechannelreport : listRechannelreportEntitys) { rechannelreportService.save(rechannelreport); } j.setMsg("文件导入成功!"); } catch (Exception e) { j.setMsg("文件导入失败!"); logger.error(ExceptionUtil.getExceptionMessage(e)); }finally{ try { file.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } } } return j;
导出方式一,可以自定义输出字段,并赋值
@RequestMapping(params = "exportXls2") public String string2(RechannelreportEntity rechannelreport,HttpServletRequest request,HttpServletResponse response , DataGrid dataGrid,ModelMap modelMap) { //"&&searchColums=&undefined=&sqlbuilder=&username=CL007&date_begin=2017-01-17&date_end=2017-01-17" ListentityList = new ArrayList (); entityList.add(new ExcelExportEntity("日期", "cou1", 15)); entityList.add(new ExcelExportEntity("渠道号", "cou2", 15)); entityList.add(new ExcelExportEntity("原始金额", "cou3", 15)); entityList.add(new ExcelExportEntity("扣量金额", "cou4", 15)); entityList.add(new ExcelExportEntity("结算金额", "cou5", 15)); //这边的getData2 方法是对 MapExcelConstants.MAP_LIST进行赋值 List
导出方式二,根据实体映射现实字段
@ExcelTarget("courseEntity") public class CourseEntity implements java.io.Serializable { /** 主键 */ private String id; /** 课程名称 */ @Excel(name = "课程名称", orderNum = "1", needMerge = true) private String name; /** 老师主键 */ @ExcelEntity(id = "yuwen") @ExcelVerify() private TeacherEntity teacher; /** 老师主键 */ @ExcelEntity(id = "shuxue") private TeacherEntity shuxueteacher; @ExcelCollection(name = "选课学生", orderNum = "4") private Liststudents;
@RequestMapping(params = "exportXls") public String exportXls(CourseEntity course,HttpServletRequest request,HttpServletResponse response , DataGrid dataGrid,ModelMap map) { CriteriaQuery cq = new CriteriaQuery(CourseEntity.class, dataGrid); org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, course, request.getParameterMap()); Listcourses = this.courseService.getListByCriteriaQuery(cq,false); map.put(NormalExcelConstants.FILE_NAME,"用户信息"); map.put(NormalExcelConstants.CLASS,CourseEntity.class); map.put(NormalExcelConstants.PARAMS,new ExportParams("课程列表", "导出人:Jeecg", "导出信息")); map.put(NormalExcelConstants.DATA_LIST,courses); return NormalExcelConstants.JEECG_EXCEL_VIEW; }
参考案例:http://www.oschina.net/p/easypoi_excel/
http://blog.csdn.net/w410589502/article/details/54093341