springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

上一篇:

springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

POI 工具类,Excel的快速导入导出,Excel模板导出,Word模板导出,可以仅仅5行代码就可以完成Excel的导入导出,修改导出格式简单粗暴,快速有效,easypoi值得你尝试
目前来说,Easypoi确实方便,官网也提供了三种不同的版本,它在开源中国还 , 还是非常出名的,用的人非常多,也是对他的一个认可 。
【springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?】

springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

小编目前的项目,也是用这个来做,今天我们来做个excel的导入导出例子,看看怎么使用?

包体引入

目前官方提供最新版本是4.2.0,但是我在使用过程中,总是报错 , 时间关系就没怎么去查找,有兴趣的同学可以呀研究一下,类找不到,这个是apache的一个类,估计是新版本需要引入别的包,没去仔细追究 。
java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
org.springframework.bootspring-boot-starter-webcn.afterturneasypoi-spring-boot-starter4.1.0commons-fileuploadcommons-fileupload1.4

编写导入导出工具类

/** * Excel枚举类型 * @author:溪云阁 * @date:2020年5月29日 */public enum ExcelTypeEnum {XLS("xls"), XLSX("xlsx");private String value;private ExcelTypeEnum(String value) {this.value = https://www.itzhengshu.com/excel/value;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}
/** * Excel导出工具类 * @author:溪云阁 * @date:2020年5月29日 */@Componentpublic class ExcelExportUtils {@Autowiredprivate HttpServletResponse response;/*** 导出excel* @author 溪云阁* @param list 泛型数据* @param title 标题* @param sheetName sheet的名称* @param pojoClass 需要导出的对象* @param fileName 文件名称* @param isCreateHeader 是否创建表头* @throws IOException void*/public void exportExcel(List list, Class pojoClass, String title, String sheetName, String fileName,boolean isCreateHeader) throws IOException {final ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);exportParams.setCreateHeadRows(isCreateHeader);baseExport(list, pojoClass, fileName, exportParams);}/*** 导出excel* @author 溪云阁* @param list 泛型数据* @param title 标题* @param sheetName sheet的名称* @param pojoClass 需要导出的对象* @param fileName 文件名称* @param response* @throws IOException void*/public void exportExcel(List list, Class pojoClass, String title, String sheetName, String fileName)throws IOException {baseExport(list, pojoClass, fileName, new ExportParams(title, sheetName, ExcelType.XSSF));}/*** 导出excel* @author 溪云阁* @param list 泛型数据* @param pojoClass 需要导出的对象* @param fileName 文件名称* @param exportParams 文件书香* @param response* @throws IOException void*/public void exportExcel(List list, Class pojoClass, String fileName, ExportParams exportParams)throws IOException {baseExport(list, pojoClass, fileName, exportParams);}/*** 多个sheet导出* @author 溪云阁* @param list* @param fileName* @throws IOException void*/public void exportExcel(List list, String fileName) throws IOException {baseExport(list, fileName);}/*** 最基础的对象导出* @author 溪云阁* @param list 数据列表* @param pojoClass 导出对象* @param fileName 文件名称* @param exportParams 导出文件属性* @throws IOException void*/private void baseExport(List list, Class pojoClass, String fileName, ExportParams exportParams)throws IOException {final Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);downLoadExcel(fileName, workbook);}/*** 最基础的多sheet导出* @author 溪云阁* @param list 多个不同数据对象的列表* @param fileName 文件名称* @throws IOException void*/private void baseExport(List list, String fileName) throws IOException {final Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);downLoadExcel(fileName, workbook);}/*** 文件下载* @author 溪云阁* @param fileName 文件名称* @param workbook exce对象* @throws IOException void*/private void downLoadExcel(String fileName, Workbook workbook) throws IOException {ServletOutputStream output = null;try {final String downloadName = URLEncoder.encode(fileName"."ExcelTypeEnum.XLSX.getValue(), "UTF-8");response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;filename="downloadName);output = response.getOutputStream();workbook.write(output);}catch (final Exception e) {throw new IOException(e.getMessage());}finally {if (output != null) {output.flush();output.close();}}}}
/** * Excel导入工具类 * @author:溪云阁 * @date:2020年5月29日 */@Componentpublic class ExcelImportUtils {/*** 从指定位置获取文件后进行导入* @author 溪云阁* @param filePath 文件路径* @param titleRows 表格标题行数,默认0* @param headerRows 表头行数,默认1* @param pojoClass 上传后需要转化的对象* @return* @throws IOException List*/public List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass)throws Exception {if (Strings.isEmpty(filePath)) {return null;} else {final ImportParams params = new ImportParams();// 表格标题行数,默认0params.setTitleRows(titleRows);// 表头行数,默认1params.setHeadRows(headerRows);// 是否需要保存上传的Excelparams.setNeedSave(true);// 保存上传的Excel目录params.setSaveUrl("/excel/");return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);}}/*** 上传文件导入* @author 溪云阁* @param file* @param titleRows 标题行* @param headerRows 表头行* @param needVerfiy 是否检验excel内容* @param pojoClass 导入的对象* @return* @throws Exception List*/public List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, boolean needVerfiy,Class pojoClass) throws Exception {if (file == null) {return null;} else {return baseImport(file.getInputStream(), titleRows, headerRows, needVerfiy, pojoClass);}}/*** 最基础导入* @author 溪云阁* @param inputStream* @param titleRows 表格标题行数,默认0* @param headerRows 表头行数,默认1* @param needVerify 是否需要检测excel* @param pojoClass 导入的对象* @return* @throws IOException List*/private List baseImport(InputStream inputStream, Integer titleRows, Integer headerRows,boolean needVerify, Class pojoClass) throws Exception {if (inputStream == null) {return null;} else {final ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);params.setSaveUrl("/excel/");params.setNeedSave(true);params.setNeedVerify(needVerify);return ExcelImportUtil.importExcel(inputStream, pojoClass, params);}}}


编写导入导出对象

这里,为了覆盖更全一点,我分别用了不同的类型来做实验 , 数字类型采用NumberFormat进行格式化操作 。
/** * 用户信息 * @author:溪云阁 * @date:2020年5月29日 */public class User implements Serializable {// 数字格式化private NumberFormat nf = NumberFormat.getNumberInstance();private static final long serialVersionUID = 1L;@Excel(name = "用户id", orderNum = "0", width = 15)@Setter@Getterprivate long userId;@Excel(name = "性别", orderNum = "1", width = 15, replace = { "男_1", "女_2" }, suffix = "孩")@Setter@Getterprivate int sex;@Excel(name = "金钱", orderNum = "2", width = 15)@Setterprivate double money;public String getMoney() {return nf.format(money);}@Excel(name = "用户信息", orderNum = "3", width = 15)@Setter@Getterprivate String userName;@Excel(name = "价格", orderNum = "4", width = 15)@Setter@Getterprivate float price;@Excel(name = "时间", orderNum = "5", width = 15, format = "yyyy-MM-dd")@Setter@Getterprivate Date now;}

编写测试方法

/** * excel导入导出 * @author:溪云阁 * @date:2020年5月29日 */@Api(tags = { "APP服务:数据接口" })@RestController@RequestMapping("view/ie")public class ImportExportController {@Autowiredprivate ExcelExportUtils excelExportUtils;@Autowiredprivate ExcelImportUtils excelImportUtils;/*** 导出用户信息* @author 溪云阁 void*/@ApiOperation(value = "https://www.itzhengshu.com/excel/导出excel")@GetMapping(value = "https://www.itzhengshu.com/exportExcel")public void exportExcel() throws Exception {final List userList = new ArrayList<>();for (int i = 0; i < 10; i) {final User user = new User();user.setUserId(i);user.setSex(1);user.setMoney(12332123i);user.setUserName("小明"i);user.setPrice(23.1fi);user.setNow(new Date());userList.add(user);}excelExportUtils.exportExcel(userList, User.class, "用户信息", "员工信息的sheet", "用户信息表");}/*** 导入用户信息* @author 溪云阁* @param file* @return* @throws IOException Object*/@ApiOperation(value = "https://www.itzhengshu.com/excel/导入excel")@GetMapping(value = "https://www.itzhengshu.com/importExcel")public ResponseMsg> importExcel(@RequestParam("file") MultipartFile file) throws Exception {final List userList = excelImportUtils.importExcel(file, 1, 1, false, User.class);return MsgUtils.buildSuccessMsg(userList);}}

导出结果

在导出中,直接在浏览器输入地址接口 , 结果如截图所示
其中,金钱,时间上,我们分别进行了格式化
springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

导入结果

把刚刚导出来的文件,直接导入进去,这里采用postMan进行操作,其中要注意的点,我已经用红色的圈圈标出来 。
从实验结果上看,已经可以导入进去,并且把数据返回来


springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

问题

在导入进去的构成中,这里留下一个问题给同学自行解决,导入的金额是0,可自行研究解决


--END--
作者:@溪云阁
原创作品 , 抄袭必究 , 转载注明出处
如需要源码,转发 , 关注后私信我
部分图片或代码来源网络,如侵权请联系删除 , 谢谢!

相关经验推荐