週末的時候 將excel導出相關的功能整理了一下,發佈成一個開源工具,看下方介紹:java
將excel中的行數據,導入成List<VO>,主要應對n多列導出代碼維護、開發方便的問題git
kit 模塊:基礎工具類工具
kit-excel 模塊:excel導入導出工具類測試
git clone https://git.oschina.net/duololo/epf.gitui
導入到IntelliJ Idea中this
運行com.zfer.kit.excel.importor.ExcelTemplateImportorTestspa
相關測試方法.net
下面是一個測試示例:excel
1.StudentNullAnnoVO.javacode
public class StudentNullAnnoVO { private int id; private String name; private int age; private String clazz; private String company; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } ....
2.excel模板
標示哪一列表明哪一個字段(能夠到項目中尋找student_template.xls具體查看)
| A | B | C | D | E | | id | name | age | clazz | company | | 序號 | 姓名 | 年齡 | 班級 | 公司 | | required | required | required | required |
3.調用
@Test public void testImportExcel2003() throws Exception { ExcelAbstractImportor<StudentNullAnnoVO> util = new ExcelTemplateImportor<StudentNullAnnoVO>();// 建立excel導入工具類 util.setExcelEntityClass(StudentNullAnnoVO.class);//設置導出的實體類型 util.setTemplateExcelInputStream(templateFis);//設置模板 util.importExcel("學生信息0", fis2003);// 導入 List<StudentNullAnnoVO> list = util.getExcelRightDataList();//獲取校驗正確的數據 List<StudentNullAnnoVO> allList = util.getExcelAllDataList();//獲取全部的數據 assertEquals( list.toString(), "[StudentVO [id=1, name=柳波, company=2009-10-09, age=18, clazz=五期提升班], StudentVO [id=2, name=柳波, company=2016-08-09, age=29, clazz=五期提升班]]"); ExcelImportSheetErrorMsg error = util.getExcelImportSheetErrorMsg(); assertEquals("{}", error.getExcelImportRowErrorMsgMap().toString()); }