当前位置:网站首页>Excel导入导出便捷工具类
Excel导入导出便捷工具类
2022-06-28 10:54:00 【芈亓】
前言
针对Excel操作,alanpoi是为了实现一个操作更加简单,开发效率更加高的工具,开发者不需要关心太多的逻辑,只需要处理和自己业务相关的部分; 化繁为简,由简变精的原则
项目中使用:
<dependency>
<groupId>com.alanpoi</groupId>
<artifactId>alanpoi-analysis</artifactId>
<version>1.3.4</version>
</dependency>功能介绍
Api document
通过 Api doc 更加深入了解它!
一. IMPORT
- ExcelHandle 核心处理器
- ExcelWorkbookManage excel所有工作表管理
- ExcelInitConfig 配置文件初始化
- AbstractFileParser 文件转换类
alanpoi import有何优势?
- 用户不需要额外引入poi等繁琐的jar
- 毫秒级解析大文件,支持一键解析多sheet页签,不需要自己按照一定的格式循环匹配解析所有数据
- 不管你的系统多么复杂,有多少个导入,alanpoi全部支持,而且准确返回你需要的对象,减轻开发者工作量
- 目前外界业务越来越复杂,对各个功能要求也越来越严格,当然导入也不例外,alanpoi支持错误一键回写到excel,对应到每一行
- alanpoi灵活可扩展,提供了ExcelConsumeInterface接口,可继承它,实现valid、error、end三个方法编写自己的业务 A. valid: 方法参数返回excel所有数据,用户可进行自我校验 B. error: 导入错误会回调 C. end: 方法参数返回校验成功的数据,valid校验失败的数据不会返回,用户可以自己操作持久化或者其他业务
怎么使用alanpoi实现导入
简单一句话:一配置一继承一调用
一配置
在项目resources目录中新建excel-config.xml文件,cosume中配置自己的消费类路径,继承ExcelConsumeInterface接口,sheet中的vo是把当前sheet序列化的对象路径,column中当然就是配置vo中的属性了, 其中name可选字段,填了就是按照这个匹配excel列名,不填就是按照offset顺序;导入包含多个sheet就配置多个
<?xml version = "1.0" encoding = "GB2312"?>
<exg name="excelId" version="1.0" file-type="excel">
<excel id="ACCOUNT" consume="com.xxx.FinAccountImportHandler">
<sheet index="0" row-start="1" column-start="0"
vo="com.xxx.vo.FinAccountImportVO">
<column name="公司/供应商编号" offset="1">companyCode</column>
<column name="公司/供应商名称" offset="2">companyName</column>
<column name="银行账号" offset="3">bankAccount</column>
<column name="开户银行" offset="4">bankName</column>
</sheet>
</excel>
</exg>一继承
consume类继承ExcelConsumeInterface接口,实现方法
/**
* when error will 调用
*
* @param excelError
*/
void error(ExcelError excelError);
/**
* custom valid data
*
* @param workbookId
* @param sheetDataList
*/
void validData(String workbookId, List<ExcelSheetData> sheetDataList, Map<Serializable, Object> excelParam);
/**
* @param sheetDataList return success data
*/
void end(List<ExcelSheetData> sheetDataList, Map<Serializable, Object> excelParam);一调用
用户调用ExcelExportUtil类的customImportData即可,参数excelId就是excel-conifg.xml中配置的id
Export
描叙
能够用一行代码实现绝不用第二行,如果一行不行,那就再加一行!
模式
使用注解模式导出
ExcelSheet注解:用于导入类上,可制定sheet名,列头的颜色、字体、高度、宽度 ExcelColum注解: 用于导入类的属性上,可指定列头的名称,单元格的样式 DateFormat注解: 用于导入类的属性上, 可以按照指定格式输出到excel,默认"yyyy/MM/dd" NumFormat注解: 用于导入类的属性上,可以按照指定格式输出到excel,默认"00.00"
样例:
@ExcelSheet(name = "测试", backColor = AlanColors.GREEN, font = "宋体", fontSize = 25)
@Data
public class ExportVO {
@ExcelColumn(name = "名称", width = 32, link = "${url}")
private String name;
@ExcelColumn(name = "值")
private String value;
@ExcelColumn(name = "金额")
@NumFormat(value = "0000.00##")
private BigDecimal amount;
@ExcelColumn(name = "时间格式化")
@DateFormat(value = "yyyy-MM-dd hh:mm:ss")
private Date dateTime;
@DateFormat
@ExcelColumn(name = "日期格式化")
private java.sql.Date date;
@ExcelColumn(isExist = false)
private String url;
}使用
方式一. 直接导出到浏览器 ExcelExportUtil.export(Colletion,Class,HttpServletRequest,HttpServletResponse,fileName); 方式二. 调用getWorkbook获取工作表,自行处理workbook ExcelExportUtil.getWorkbook(Collection singleSheetData, Class<?> c)
高级使用
示例一:导出指定列(动态导出列)
List<ExportVO> list = new ArrayList<>();
for (int i = 0; i < 500; i++) {
ExportVO exportVO = new ExportVO();
exportVO.setName("name" + i);
exportVO.setValue(new BigDecimal(123.11 + i * 0.09));
exportVO.setAmount(new BigDecimal(6666.666 + i * 10));
exportVO.setDate(new Date(132324343 + i * 100));
exportVO.setDateTime(new java.util.Date());
list.add(exportVO);
}
List<String> colList = new ArrayList<>();
//按照顺序仅导出add的列
colList.add("name");
colList.add("value");
//调用获取workbook对象;也可以直接调用exportSpecifyCol方法导出到浏览器
Workbook workbook = ExcelExportUtil.getWorkbookSpecifyCol(list, ExportVO.class, colList);示例二:多sheet页签导出
List<ExportVO> list = new ArrayList<>();
List<Export2VO> list2 = new ArrayList<>();
for (int i = 0; i < 500; i++) {
ExportVO exportVO = new ExportVO();
exportVO.setName("name" + i);
exportVO.setValue(new BigDecimal(123.11 + i * 0.09));
exportVO.setAmount(new BigDecimal(6666.666 + i * 10));
exportVO.setDate(new Date(132324343 + i * 100));
exportVO.setDateTime(new java.util.Date());
list.add(exportVO);
Export2VO export2VO = new Export2VO();
export2VO.setName("name" + i);
export2VO.setValue("value" + i);
export2VO.setAmount(new BigDecimal(6666.666 + i * 10));
export2VO.setDate(new Date(132324343 + i * 100));
export2VO.setDateTime(new java.util.Date());
list2.add(export2VO);
}
Map<Class<?>, Collection<?>> map = new HashMap<>();
map.put(ExportVO.class, list);
map.put(Export2VO.class, list2);
//调用获取workbook对象;也可以直接调用exportByMultiSheet方法导出到浏览器
Workbook workbook = ExcelExportUtil.getWorkbookByMultiSheet(map);边栏推荐
- [monkey] Introduction to monkey test
- Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
- Makefile简介
- 动态库(共享库)的制作和使用
- Installing MySQL database (CentOS) in Linux source code
- windows 10下载安装mysql5.7
- Pop up and push in sequence of stack < difficulty coefficient >
- 如何利用k线图做技术分析
- JS基础8
- JSON module, hashlib, Base64
猜你喜欢

JS基础8
![[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)](/img/e8/21d8d81a3d7b544687d6adc06ad4b1.png)
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)

Yann LeCun新论文:构建自动智能体之路

Katalon framework tests web (XX) custom keywords and upload pop-up operations

How to use output in katalon

线程和线程池

阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

MySQL (I)

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!

Set up your own website (11)
随机推荐
Realization of a springboard machine
[leetcode daily question] [December 19, 2021] 997 Find the town judge
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
Remote connection of raspberry pie in VNC viewer mode without display
【monkey】monkey测试入门
Day 6 script and animation system
Six fusion positioning technologies in wireless communication application of Internet of things
ICMP协议的作用,Ping of Death攻击的原理是什么?
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
一种跳板机的实现思路
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
flink1.15,支持mysql视图吗?我这边在table-name处配置视图名保存,找不到表。想
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
Katalon framework tests a web page operation example code
datetime与logging模块
爱可可AI前沿推介(6.28)
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
JS基础3
JS foundation 2
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!