项目中使用:
com.alanpoi
alanpoi-analysis
1.3.0
简单一句话:一配置一继承一调用
在项目resources目录中新建excel-config.xml文件,cosume中配置自己的消费类路径,继承ExcelConsumeInterface接口,sheet中的vo是把当前sheet序列化的对象路径,column中当然就是配置vo中的属性了。
其中name可选字段,填了就是按照这个匹配excel列名,不填就是按照offset顺序;导入包含多个sheet就配置多个
companyCode
companyName
bankAccount
bankName
consume类继承ExcelConsumeInterface接口,实现方法
/**
* when error will 调用
*
* @param excelError
*/
void error(ExcelError excelError);
/**
* custom valid data
*
* @param workbookId
* @param sheetDataList
*/
void validData(String workbookId, List sheetDataList, Map excelParam);
/**
* @param sheetDataList return success data
*/
void end(List sheetDataList, Map excelParam);
用户调用ExcelExportUtil类的customImportData即可,参数excelId就是excel-conifg.xml中配置的id
能够用第一行代码实现绝不用第二行,如果一行不行,那就再加一行!
注解模式导出:
样例:
@ExcelSheet(name = "测试", backColor = AlanColors.GREEN, font = "宋体", fontSize = 25)
@Data
public class ExportVO {
@ExcelColumn(name = "名称", width = 32, link = "${url}")
private String name;
@ExcelColumn(name = "值")
private String value;
@ExcelColumn(name = "金额")
@NumFormat(value = "0000.00##")
private BigDecimal amount;
@ExcelColumn(name = "时间格式化")
@DateFormat(value = "yyyy-MM-dd hh:mm:ss")
private Date dateTime;
@DateFormat
@ExcelColumn(name = "日期格式化")
private java.sql.Date date;
@ExcelColumn(isExist = false)
private String url;
}
方式一. 直接导出到浏览器
ExcelExportUtil.export(Colletion>,Class,HttpServletRequest,HttpServletResponse,fileName);
方式二. 调用getWorkbook获取工作表,自行处理workbook
ExcelExportUtil.getWorkbook(Collection> singleSheetData, Class> c)
示例一:导出指定列(动态导出列)
List 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 colList = new ArrayList<>();
//按照顺序仅导出add的列
colList.add("name");
colList.add("value");
//调用获取workbook对象;也可以直接调用exportSpecifyCol方法导出到浏览器
Workbook workbook = ExcelExportUtil.getWorkbookSpecifyCol(list, ExportVO.class, colList);
示例二:多sheet页签导出
List list = new ArrayList<>();
List 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, Collection>> map = new HashMap<>();
map.put(ExportVO.class, list);
map.put(Export2VO.class, list2);
//调用获取workbook对象;也可以直接调用exportByMultiSheet方法导出到浏览器
Workbook workbook = ExcelExportUtil.getWorkbookByMultiSheet(map);
代码已经开源,地址:
https://github.com/alan-et/alanpoi/tree/develop/alanpoi-analysis