当前位置:网站首页>Excel import / export convenience tool class
Excel import / export convenience tool class
2022-06-28 11:38:00 【Mi Qi】
Preface
in the light of Excel operation ,alanpoi To make the operation simpler , Develop more efficient tools , Developers don't need to care too much about logic , You only need to deal with the parts related to your business ; Change numerous for brief , The principle of "from simplification to refinement"
Project use :
<dependency>
<groupId>com.alanpoi</groupId>
<artifactId>alanpoi-analysis</artifactId>
<version>1.3.4</version>
</dependency>Function is introduced
Api document
adopt Api doc Learn more about it !
One . 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 ,alanpoi All for , And return exactly what you need , Reduce developer workload
- At 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 A. valid: Method parameters return excel All the data , Users can perform self verification B. error: Import errors will call back C. 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
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
Export using annotation mode
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)
@Data
public 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);边栏推荐
猜你喜欢

Packaging and publishing application of jetpack compose desktop version

day29 js笔记 2021.09.23

Word、PDF、TXT文件实现全文内容检索需要用什么方法?

Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush

携手Cigent:群联为SSD主控固件引入高级网络安全防护特性

Day30 JS notes BOM and DOM 2021.09.24

Docker modifies the user name and password of MySQL

js中this的默认指向及如何修改指向 2021.11.09

JS foundation 1-js introduction and operator

Day31 JS notes DOM 2021.09.26
随机推荐
Mysql使用max函数查询不到最大值
GDB简介
day30 js笔记 BOM和DOM 2021.09.24
Day33 JS note event (Part 2) September 28, 2021
It is safer for individuals to choose which securities company to open an account for buying floor funds
Industry analysis - quick intercom, building intercom
面试步骤的面试技巧
Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help
毕业季 新的开始
soapui的菜鸟教程
赛尔号抽奖模拟求期望
Gee: mcd64a1 based globfire daily fire data set
[sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
功能真花哨,价格真便宜!长安全新SUV真实力到底怎样?
2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
day39 原型链及页面烟花效果 2021.10.13
时间戳和date转换「建议收藏」