当前位置:网站首页>几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
2022-06-26 12:32:00 【hello-java-maker】
今日推荐
减少 try-catch ,这样做才叫优雅!
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
SpringBoot实现人脸识别功能
相信我,使用 Stream 真的可以让代码更优雅!
全网最详细的线程池 ThreadPoolExecutor 解读!
利用多线程批量拆分 List 导入数据库,效率杠杠的!
功能介绍
IMPORT
ExcelHandle
核心处理器ExcelWorkbookManage
excel所有工作表管理ExcelInitConfig
配置文件初始化AbstractFileParser
文件转换类
alanpoi import有何优势?
用户不需要额外引入poi等繁琐的jar
毫秒级解析大文件,支持一键解析多sheet页签,不需要自己按照一定的格式循环匹配解析所有数据
不管你的系统多么复杂,有多少个导入,
alanpoi
全部支持,而且准确返回你需要的对象,减轻开发者工作量目前外界业务越来越复杂,对各个功能要求也越来越严格,当然导入也不例外,alanpoi支持错误一键回写到excel,对应到每一行
alanpoi
灵活可扩展,提供了ExcelConsumeInterface
接口,可继承它,实现valid、error、end
三个方法编写自己的业务valid: 方法参数返回excel所有数据,用户可进行自我校验
error: 导入错误会回调
end: 方法参数返回校验成功的数据,valid校验失败的数据不会返回,用户可以自己操作持久化或者其他业务
怎么使用alanpoi实现导入
项目中使用:
<dependency>
<groupId>com.alanpoi</groupId>
<artifactId>alanpoi-analysis</artifactId>
<version>1.3.0</version>
</dependency>
简单一句话:一配置一继承一调用
一配置
在项目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);
代码已经开源,地址:
https://github.com/alan-et/alanpoi/tree/develop/alanpoi-analysis
来源:blog.csdn.net/weixin_43225813/
article/details/108995011
最后,给大家推荐一个我的知识星球,现在加入,前 100 名,只需要 25 元即可,非常优惠。
边栏推荐
- Research on the current situation of China's modified engineering plastics market and demand forecast analysis report 2022-2028
- SQL injection in Pikachu shooting range
- PHP unit conversion
- CG bone animation
- Why is password salt called "salt"? [Close] - why is a password salt called a "salt"? [closed]
- Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
- 7-2 摘花生
- Function collapse and expansion shortcut keys in vscode (latest and correct)
- 2016年四川省TI杯电子设计竞赛B题
- Common problems and Thoughts on member operation management
猜你喜欢
MS17_ 010 utilization summary
Vscode solves the problem of Chinese garbled code
Fengshentai old shooting range Kali series
环形队列php
4. N queen problem
nvm安装教程
Deep thinking from senior member managers
Introduction to the four major FPGA manufacturers abroad
Implementing mixins scheme in applet
Microservice governance (nocas)
随机推荐
What software is flush? Is online account opening safe?
Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files
Introduction to the strongest swarm cluster one click deployment + hydrogen bomb level container management tool
[solved] data duplication or data loss after laravel paginate() paging
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
Deep thinking from senior member managers
KITTI Tracking dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
Realize microservice load balancing (ribbon)
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
How to do well in member marketing three steps to teach you to understand member management
Polarismesh series articles - concept series (I)
Omni channel member link - tmall member link 3: preparation of member operation content
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
The laravel dingo API returns a custom error message
详细实操分享,下班刷了两小时的搞笑视频,一个月收益7000多
2022 China smart bathroom cabinet Market Research and investment Competitiveness Analysis Report
JS how to judge when data contains integer and floating-point types. Floating-point decimals retain two digits after the decimal point
Ubuntu安装配置PostgreSQL(18.04)
洛谷P3426 [POI2005]SZA-Template 题解
2022 edition of China's medical robot industry investment status investigation and prospect dynamic analysis report