七库下载 手游攻略 手游攻略 springboot集成easyui,springboot es api

springboot集成easyui,springboot es api

时间:2024-04-08 23:46:04 来源:头条 浏览:0

开发时导入或导出Excel时,使用jxl或poi jar包需要编写大量代码,而Easypoi封装了poi,只是在导出的实体类上添加注解。 1、pom.xml添加依赖创建springboot后,为Easypoi添加依赖本文使用的springboot版本为2.2.0.RELEASE。

cn.afterturn easypoi-spring-boot-starter 3.3.0 启动时

************************** 应用程序无法启动****************** ** *** ********描述: 无法注册在类路径资源[cn/afterturn/easypoi/configuration/EasyPoiAutoConfiguration.class] 中定义的bean“beanNameViewResolver”。具有该名称的bean 已在类路径资源[org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class] 中定义,并且覆盖被禁用。 Action: 考虑重命名其中一个bean 或设置spring.main.allow-bean-definition 以启用覆盖。将-overriding=true 添加到您的application.properties 文件

spring.main.allow-bean-definition-overriding=true2、创建实体类、创建人实体类(导出)

@Datapublic class People 是可序列化的{ @Excel(name='Name' ,height=20, width=30) private String name; @Excel(name='Age') private Integerage; @Excel(name='Birthday', exportFormat='yyyy-MM-dd') private Stringbirthday;}@Excel应用于filed,是Excel列描述。您可以设置长度、宽度和日期格式等属性。请参阅文档了解更多详细信息。还有@ExcelCollection@ExcelEntity@ExcelIgnore@ExcelTarget等注解。 3.导出工具

@Slf4jpublic class ExcelUtil { /** * Excel 导出* * @param 导出数据列表* @param Sheet name Sheet 名称* @param pojoClass Pojo 类型* @param fileName 文件名* @param Response */public static void exportExcel (List list, String SheetName, Class pojoClass, String fileName, HttpServletResponse response) throws IOException { ExportParams exportParams=new ExportParams();//导出主要使用ExportParams 对象进行参数设置。 exportParams.setSheetName(sheetName);//sheetName exportParams.setType( ExcelType.vnd.ms-excel'); response.setHeader('Content-Disposition', 'attachment;filename=' + URLEncoder .encode(fileName + ' .xlsx ', 'UTF-8'));//上面的ExcelType,根据XSSF设置来设置,相当于ExcelType.XSSF。 xlsx.ExcelType.HSSF is .xls workbook.write(response.getOutputStream()); } catch (Exception e) { log.error('error {}',e.getMessage()); throw new IOException(e.getMessage ()); } }4. 导出示例

@GetMapping('export') public void export(HttpServletResponse response){ List peopleList=new ArrayList(); 人物data1=new People(); 人物data2=new People(); data1.setName('隔壁老王'); } data1.setAge(30); data1.setBirthday('1997-10-01 00:00:00'); data2.setName('钻石王'); data2.setAge(40); data2.setBirthday('1997-10-01 00:00:00 ' ); peopleList.add(data1); peopleList.add(data2); try { ExcelUtil.exportExcel(peopleList, '个人信息', People.class, '个人信息', response ); } catch (IOException e) { log . error('Export failed'); } }5. 导入的实体如果要验证导入的数据,easypoi 有自己的接口ExcelModel 来获取错误信息,IExcelDataModel 来获取错误信息所在的行号。创建您自己的类来实现这两个接口。

公共类ExcelVerifyInfoimplements IExcelModel,IExcelDataModel { 私有字符串errorMsg; 私有int rowNum; @Override public int getRowNum() { return rowNum; } @Override public void setRowNum(int rowNum) { this.rowNum=rowNum; } @Override public String getErrorMsg ( ) { return errorMsg; } @Override public void setErrorMsg(String errorMsg) { this.errorMsg=errorMsg; }} 为了验证导入的Excel,我们需要向该实体添加一些注释

@Datapublic class People extends ExcelVerifyInfoimplements Serialized { @Excel(name='姓名' ,height=20, width=30) @NotNull(message='姓名不能为空') private String name; @Excel(name='年龄' ) @Max(value=100, message='最大年龄不能超过100' ) @NotNull(message='年龄不能为空') //@Pattern(regexp='[\u4E00 -\u9FA5]*', message=' not Chinese') 或者通常的检查private Integer Age; @Excel(name='birthday', exportFormat='yyyy-MM-dd') private Stringbirthday;} @NotNull, @Max, @Min, @Pattern 对这些消息进行注释包含您要输出的错误消息的属性。 6.导入工具

/** * Excel 导入* * @param headerRows 标题行* @param needVerfiy 是否检查Excel 的内容* @param pojoClass pojo type* @param * @return */public static ExcelImportResult importExcel(MultipartFile file, Integer headerRows, boolean needVerfiy, Class pojoClass) throws IOException { if (file==null) { log.info('文件为空'); return null; } ImportParams params=new ImportParams(); params.setHeadRows(headerRows); //标题行被忽略的行数params.setNeedVerfiy(needVerfiy); //启用验证? try { return ExcelImportUtil.importExcelMore(file.getInputStream(), pojoClass, params); } catch (Exception e) { log.error('importExcel IOException { } ' ,e.getMessage()); throw new IOException(e.getMessage()); } }ExcelImportResult 是导入返回的类。它有很多属性。我们将讨论常用的。

/** * 结果集*/private List list; /** * 失败数据*/private List failedList; /** * 验证失败状态*/private boolean verfiyFail; 7. 导入示例

@PostMapping('/import') public void importExcel(MultipartFile file){ if (file==null){ log.info('文件没有数据'); return; } ExcelImportResult result=null; try { result=ExcelUtil . importExcel(file, 1, true, People.class); } catch (IOException e) { e.printStackTrace(); } List failedList=result.getFailList();//获取失败数据if (failList.size() 0) { for (People people : failedList) { log.info('{}th row,{}', people.getRowNum(), people.getErrorMsg());//输出失败行和失败信息} } //不是如果存在则出错,因此您可以将其保存到文件服务系统或数据库。这里我们只打印数据。 List list=result.getList(); log.info('数据导入成功{}',JSON.toJSONString(list) ); }附easypoi文档:http://easypoi.mydoc.io/

标题:springboot集成easyui,springboot es api
链接:https://www.7kxz.com/news/gl/20367.html
版权:文章转载自网络,如有侵权,请联系删除!
资讯推荐
更多
天地劫幽城再临归真4-5攻略:第四章归真4-5八回合图文通关教学

天地劫幽城再临归真4-5攻略:第四章归真4-5八回合图文通关教学[多图],天地劫幽城再临归真4-5怎么样八回合内通

2024-04-08
航海王热血航线艾尼路怎么玩?艾尼路加点连招攻略大全

航海王热血航线艾尼路怎么玩?艾尼路加点连招攻略大全[多图],航海王热血航线艾尼路怎么加点?艾尼路怎么连招?关

2024-04-08
坎公骑冠剑国际服怎么玩?国际服新手攻略

坎公骑冠剑国际服怎么玩?国际服新手攻略[多图],坎公骑冠剑国际服的玩法是什么样的?关于游戏中的一些新手玩法

2024-04-08
王者荣耀鸿运6+1地狱之眼怎么抽?鸿运抽奖地狱之眼概率获取攻略

王者荣耀鸿运6+1地狱之眼怎么抽?鸿运抽奖地狱之眼概率获取攻略[多图],王者荣耀鸿运抽奖活动的奖池中还有传说

2024-04-08