EasyPOI简单用例,简单有效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EasyPOI简单用例,简单有效相关的知识,希望对你有一定的参考价值。
用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配置如下:
<!-- Bean解析器,级别高于默认解析器,寻找bean对象进行二次处理 --> <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"> </bean> <!-- Excel 处理 根据用户输入进行对象处理 --> <bean id="jeecgExcelView" class="org.jeecgframework.poi.excel.view.JeecgSingleExcelView" /> <bean id="jeecgTemplateExcelView" class="org.jeecgframework.poi.excel.view.JeecgTemplateExcelView" /> <bean id="jeecgTemplateWordView" class="org.jeecgframework.poi.excel.view.JeecgTemplateWordView" /> <bean id="jeecgMapExcelView" class="org.jeecgframework.poi.excel.view.JeecgMapExcelView" />
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; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 ImportParams params = new ImportParams(); params.setTitleRows(2); params.setHeadRows(1); params.setNeedSave(true); try { List<RechannelreportEntity> 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" List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>(); 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<Map<String, String>> dataResult = getData2(getDateGrid(rechannelreport, request)); modelMap.put(MapExcelConstants.ENTITY_LIST, entityList); modelMap.put(MapExcelConstants.MAP_LIST, dataResult); modelMap.put(MapExcelConstants.FILE_NAME, DateUtils.formatDate(new Date()) + "数据"); Date now = new Date(); modelMap.put(NormalExcelConstants.PARAMS, new ExportParams(rechannelreport.getUsername() + "对账单详情", "导出日期:" + DateUtils.formatDate(new Date()) + " ", "导出信息")); return MapExcelConstants.JEECG_MAP_EXCEL_VIEW; }
导出方式二,根据实体映射现实字段
@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 List<StudentEntity> students;
@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()); List<CourseEntity> courses = 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
本文出自 “12056352” 博客,转载请与作者联系!
以上是关于EasyPOI简单用例,简单有效的主要内容,如果未能解决你的问题,请参考以下文章
spring boot + easypoi两行代码excel导入导出