本人在上家公司有段时间负责报表的事情,频繁的需要使用excel的导入导出,于是写了一套公用代码,
写作时间是2015年6月,现在有空分享出来,供大家参考
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 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 list = ExcelImportUtils.parseSheet(
Member.class, MemberVerifyBuilder.getInstance(), sheet, 1, new ParseSheetCallback() {
@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 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 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