当前位置:网站首页>A few lines of code can realize complex excel import and export. This tool class is really powerful!
A few lines of code can realize complex excel import and export. This tool class is really powerful!
2022-06-28 20:05:00 【Java technology stack】
source :blog.csdn.net/weixin_43225813/article/details/108995011
Function is introduced
IMPORT
- ExcelHandle Core processor
- ExcelWorkbookManage excel All sheet management
- ExcelInitConfig Configuration file initialization
- AbstractFileParser File conversion class
alanpoi import What are the advantages ?
The user does not need to introduce poi And so on jar
Parsing large files in milliseconds , Support one key parsing for multiple sheet Tab , You don't need to match and parse all the data in a certain format
No matter how complex your system is , How many imports ,
alanpoiAll for , And return exactly what you need , Reduce developer workloadAt present, the external business is becoming more and more complex , The requirements for various functions are becoming more and more strict , Of course, import is no exception ,alanpoi Support error one click write back excel, Corresponding to each line
alanpoi Flexible and scalable , Provides ExcelConsumeInterface Interface , You can inherit it , Realization valid、error、end Three ways to write your own business
- valid: Method parameters return excel All the data , Users can perform self verification
- error: Import errors will call back
- end: The method parameter returns the data successfully verified ,valid Data that fails the verification will not be returned , Users can operate persistence or other businesses by themselves
How do you use it? alanpoi Implement import
Project use :
<dependency> <groupId>com.alanpoi</groupId> <artifactId>alanpoi-analysis</artifactId> <version>1.3.0</version></dependency>In a word : One configuration, one inheritance, one invocation
I. configuration
In the project resources Create a new excel-config.xml file ,cosume Configure your own consumption class path in , Inherit ExcelConsumeInterface Interface ,sheet Medium vo It's the present sheet Serialized object path ,column Configuration is, of course vo The property of .
among name Optional fields , Fill it in and match it with this excel Name , If you don't fill it out, you will follow offset The order ; The import contains multiple sheet Configure multiple
<?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=" company / Supplier No " offset="1">companyCode</column> <column name=" company / Name of supplier " offset="2">companyName</column> <column name=" Bank Account " offset="3">bankAccount</column> <column name=" Bank of deposit " offset="4">bankName</column> </sheet> </excel></exg>I. inheritance
consume Class inheritance ExcelConsumeInterface Interface , Implementation method
/** * when error will call * * @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);One call
The user calls ExcelExportUtil Class customImportData that will do , Parameters excelId Namely excel-conifg.xml Configured in id
Export
describe
It can be implemented in one line of code without the second line , If I can't , Then add another line !
Pattern
Annotation mode export :
- ExcelSheet annotation : Used to import classes , You can make sheet name , The color of the column header 、 typeface 、 Height 、 Width
- ExcelColum annotation : Used on the properties of the import class , You can specify the name of the column header , Cell style
- DateFormat annotation : Used on the properties of the import class , You can output to... In the specified format excel, Default "
yyyy/MM/dd" - NumFormat annotation : Used on the properties of the import class , You can output to... In the specified format excel, Default "
00.00"
Examples :
@ExcelSheet(name = " test ", backColor = AlanColors.GREEN, font = " Song style ", fontSize = 25)@Datapublic class ExportVO { @ExcelColumn(name = " name ", width = 32, link = "${url}") private String name; @ExcelColumn(name = " value ") private String value; @ExcelColumn(name = " amount of money ") @NumFormat(value = "0000.00##") private BigDecimal amount; @ExcelColumn(name = " Time format ") @DateFormat(value = "yyyy-MM-dd hh:mm:ss") private Date dateTime; @DateFormat @ExcelColumn(name = " Date formatting ") private java.sql.Date date; @ExcelColumn(isExist = false) private String url;}Use
Mode one . Export directly to browser
ExcelExportUtil.export(Colletion<?>,Class,HttpServletRequest,HttpServletResponse,fileName); Mode two . call getWorkbook Get worksheet , Do it yourself workbook
ExcelExportUtil.getWorkbook(Collection<?> singleSheetData, Class<?> c)Advanced use
Example 1 : Export the specified column ( Dynamically export Columns )
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<>();// Export only... In order add The column of colList.add("name");colList.add("value");// Call get workbook object ; It can also be called directly exportSpecifyCol Method to export to the browser Workbook workbook = ExcelExportUtil.getWorkbookSpecifyCol(list, ExportVO.class, colList);Example 2 : many sheet Tab export
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);// Call get workbook object ; It can also be called directly exportByMultiSheet Method to export to the browser Workbook workbook = ExcelExportUtil.getWorkbookByMultiSheet(map);Recent hot article recommends :
1.1,000+ Avenue Java Arrangement of interview questions and answers (2022 The latest version )
2. Explode !Java Xie Cheng is coming ...
3.Spring Boot 2.x course , It's too complete !
4. Don't write about the explosion on the screen , Try decorator mode , This is the elegant way !!
5.《Java Development Manual ( Song Mountain version )》 The latest release , Download it quickly !
I think it's good , Don't forget to like it + Forward !
边栏推荐
- Tcwind mode setting
- 2280.Cupboards
- odoo15 Module operations are not possible at this time, please try again later or contact your syste
- return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};
- 数字经济专家高泽龙:映客更名映宇宙,元宇宙会成为映客下一个增长引擎吗?
- Server configuration estimation of core IOT Bluetooth AOA positioning system
- Compression and decompression commands
- return new int[]{i + 1, mid + 1};return {i + 1, mid + 1};
- 2342
- head、tail查看文件
猜你喜欢

Nanopc-t4 (rk3399) Game1 OLED (I2C) display time weather temperature

28 rounds of interviews with 10 companies in two and a half years

Leetcode week 299

Industry analysis - quick intercom, building intercom

数字经济专家高泽龙:映客更名映宇宙,元宇宙会成为映客下一个增长引擎吗?

Variational graph auto-encoders (VGAE)

How does redis implement inventory deduction? How to prevent oversold?

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation

Why is it not enough to declare the structure alias when using the structure variable of other files in C language, and the proper name must be used? (cannot add struct when using alias)

Number theory -- detailed proof of Euler function, sieve method for Euler function, Euler theorem and Fermat theorem
随机推荐
修复一次flutter 无法选中模拟器
集合之ArrayList
Fontawesome icon for color gradient
Kaggle肠胃道图像分割比赛baseline
jvm内存结构
Markdown mermaid种草(1)_ mermaid简介
Database learning notes (sql04)
Rsync remote synchronization
【Go语言刷题篇】Go从0到入门5:Map综合复习、条件语句、循环语句练习
Figure introduction to neural networks (GNN, GCN)
类加载机制与对象的创建
管道 | 与重定向 >
internship:术语了解及着手写接口
easypoi
Tcwind mode setting
Xiaobai's e-commerce business is very important to choose the right mall system!
TcWind 模式設定
Parallax JS special effect JS carousel map plug-in
Time series forecasting based on trend and seasonality
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions