excel匯入匯出通用工具類
阿新 • • 發佈:2019-01-08
背景
本人在上家公司有段時間負責報表的事情,頻繁的需要使用excel的匯入匯出,於是寫了一套公用程式碼,
寫作時間是2015年6月,現在有空分享出來,供大家參考
特性
匯入模板具有以下特性:1、列格式化和列值校驗,是否允許空判斷
2、可指定列提取
3、提供回撥函式,進行額外欄位填充和業務邏輯
4、報錯機制,報錯提示批量、準確具體行列
匯出模板具有以下特性:
1、可生成序列號
2、可指定列長度
此套匯入和匯出開發模板簡單易用,可讀性強,維護方便,讓程式設計師避開復雜的程式碼操作,專注業務。基礎類
AbstractCellValueVerify:抽象列值校驗器
AbstractCellVerify:抽象列校驗器,格式化和校驗列值,可根據建構函式拓展,比較校驗是否允許為null。AbstractCellValueVerify和AbstractCellVerify關係是AbstractCellVerify使用到了AbstractCellValueVerify。
AbstractVerifyBuidler:抽象校驗構建器,用來構建實體對應所有列的校驗器
下面都是具體格式的列校驗器,對列格式化為對應格式,並對列值進行校驗
BigDecimalVerify、DateTimeVerify、DoubleVerify、IntegerVerify、StringToIntegerVerify、StringVerify。
特殊說明一下的是StringToIntegerVerify:針對字串轉int型別的情況,比如借款型別
ParseSheetCallback:解析Sheet回撥函式
匯入示例:
/** * 匯入使用者校驗類 * @author Administrator * */ public class MemberVerifyBuilder extends AbstractVerifyBuidler { private static MemberVerifyBuilder builder = new MemberVerifyBuilder(); public static MemberVerifyBuilder getInstance() { return builder; } /** * 定義列校驗實體:提取的欄位、提取列、校驗規則 */ private MemberVerifyBuilder() { cellEntitys.add(new CellVerifyEntity("name", "A", new StringVerify("姓名", true))); cellEntitys.add(new CellVerifyEntity("age", "B", new IntegerVerify("年齡", true))); cellEntitys.add(new CellVerifyEntity("country", "D", new StringToIntegerVerify("國家", new AbstractCellValueVerify() { @Override public Object verify(Object fileValue) { // TODO 轉換:從excel中得到string轉成需要的integer return 1; } }, true))); cellEntitys.add(new CellVerifyEntity("date", "F", new DateTimeVerify("建立日期", "yyyy/MM/dd", true))); // 必須呼叫 super.init(); } }
public class MainClass { public static void main(String[] args) throws Exception { parseSheet(); parseSheetWithCallback(); } /** * 解析excel * @throws Exception */ public static void parseSheet() throws Exception { Workbook wb = WorkbookFactory.create(new FileInputStream("src/test/java/example/imp/import.xlsx")); Sheet sheet = wb.getSheetAt(0); // parseSheet List<Member> list = ExcelImportUtils.parseSheet( Member.class, MemberVerifyBuilder.getInstance(), sheet, 1); System.out.println(JSON.toJSONString(list)); } /** * 解析excel帶回調函式:做一些額外欄位填充 * @throws Exception */ public static void parseSheetWithCallback() throws Exception { Workbook wb = WorkbookFactory.create(new FileInputStream("src/test/java/example/imp/import.xlsx")); Sheet sheet = wb.getSheetAt(0); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // parseSheet List<Member> list = ExcelImportUtils.parseSheet( Member.class, MemberVerifyBuilder.getInstance(), sheet, 1, new ParseSheetCallback<Member>() { @Override public void callback(Member t, int rowNum) throws Exception { t.setDateDesc(sdf.format(t.getDate())); } }); System.out.println(JSON.toJSONString(list)); } }
匯入示例:
public class MainClass {
/**
* 定義:excel標題,提取資料的欄位,佔用列長度
*/
private static final Object[][] fields = new Object[][]{
{"姓名","name",POIConstant.TIME},
{"年齡","age",POIConstant.NUMBER},
{"國家","country",POIConstant.NAME},
{"建立日期","dateDesc",POIConstant.TIME},
};
private static final Object[][] fieldsWithNum = new Object[][]{
{"序號","",POIConstant.NUMBER},
{"姓名","name",POIConstant.TIME},
{"年齡","age",POIConstant.NUMBER},
{"國家","country",POIConstant.NAME},
{"建立日期","dateDesc",POIConstant.TIME},
};
public static void main(String[] args) throws Exception {
exportBean();
exportBeanWithNum();
exportMap();
}
/**
* 1、匯出物件
* @throws Exception
*/
public static void exportBean() throws Exception{
List<Member> list = new ArrayList<>();
list.add(new Member("張三", 28, 1, "2016-10-19"));
list.add(new Member("李四", 25, 2, "2016-10-19"));
Workbook bean = ExcelExportUtils.createWorkbook(list, fields);
bean.write(new FileOutputStream("src/test/java/example/exp/exportBean.xlsx"));
}
/**
* 2、匯出物件帶序列號
* @throws Exception
*/
public static void exportBeanWithNum() throws Exception{
List<Member> list = new ArrayList<>();
list.add(new Member("張三", 28, 1, "2016-10-19"));
list.add(new Member("李四", 25, 2, "2016-10-19"));
Workbook bean = ExcelExportUtils.createWorkbook(list, fieldsWithNum, true);
bean.write(new FileOutputStream("src/test/java/example/exp/exportBeanWithNum.xlsx"));
}
/**
* 匯出map
* @throws Exception
*/
public static void exportMap() throws Exception{
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("name", "張三");
map.put("age", 28);
map.put("country", 1);
map.put("dateDesc", "2016-10-19");
list.add(map);
map = new HashMap<>();
map.put("name", "張三");
map.put("age", 28);
map.put("country", 1);
map.put("dateDesc", "2016-10-19");
list.add(map);
Workbook bean = ExcelExportUtils.createWorkbook(list, fields);
bean.write(new FileOutputStream("src/test/java/example/exp/exportMap.xlsx"));
}
}