【springboot+easypoi】一行代码搞定excel导入导…
2019-10-29 16:01:29来源:博客园 阅读 ()
【springboot+easypoi】一行代码搞定excel导入导出
easyPoi 官方API
http://easypoi.mydoc.io/
pom引入
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>
实体类
@Data public class Person { @Excel(name = "姓名", orderNum = "0") private String name; @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1") private String sex; @Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2") private Date birthday; public Person(String name, String sex, Date birthday) { this.name = name; this.sex = sex; this.birthday = birthday; } }
导入导出公共方法
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){ ExportParams exportParams = new ExportParams(title, sheetName); exportParams.setCreateHeadRows(isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){ defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){ defaultExport(list, fileName, response); } private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list); if (workbook != null); downLoadExcel(fileName, response, workbook); } private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { throw new NormalException(e.getMessage()); } } private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); if (workbook != null); downLoadExcel(fileName, response, workbook); } public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){ if (StringUtils.isBlank(filePath)){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }catch (NoSuchElementException e){ throw new NormalException("模板不能为空"); } catch (Exception e) { e.printStackTrace(); throw new NormalException(e.getMessage()); } return list; } public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){ if (file == null){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); }catch (NoSuchElementException e){ throw new NormalException("excel文件不能为空"); } catch (Exception e) { throw new NormalException(e.getMessage()); } return list; }
测试
@RequestMapping("export") public void export(HttpServletResponse response){ //模拟从数据库获取需要导出的数据 List<Person> personList = new ArrayList<>(); Person person1 = new Person("路飞","1",new Date()); Person person2 = new Person("娜美","2", DateUtils.addDate(new Date(),3)); Person person3 = new Person("索隆","1", DateUtils.addDate(new Date(),10)); Person person4 = new Person("小狸猫","1", DateUtils.addDate(new Date(),-10)); personList.add(person1); personList.add(person2); personList.add(person3); personList.add(person4); //导出操作 FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response); } @RequestMapping("importExcel") public void importExcel(){ String filePath = "F:\\海贼王.xls"; //解析excel, List<Person> personList = FileUtil.importExcel(filePath,1,1,Person.class); //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入 System.out.println("导入数据一共【"+personList.size()+"】行"); //TODO 保存数据库 }
————————————————
原文链接:https://blog.csdn.net/zhefudexiaojiahuo/article/details/83586498
原文链接:https://www.cnblogs.com/hazzZ/p/11757001.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 项目经理说这种代码必须重构,我同意了,这代码是写的是有多 2020-06-11
- idea为代码添加标签清除标签 2020-06-11
- 代码对比工具,我就用这 6 个! 2020-06-10
- 2020最新IDEA插件大集合,一款能帮助你写代码的工具是多么重 2020-06-09
- 为什么阿里巴巴Java开发手册中不允许魔法值出现在代码中? 2020-06-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash