赞
踩
项目中用到EasyExcel读写Excel,用到了一下功能,这里做个笔记:
EasyExcel 版本:3.0.x
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
List<List<String>> heads = new ArrayList<>(); List<String> head0 = new ArrayList<>(); // 固定列 head0.add("ID"); heads.add(head0); List<String> head1 = new ArrayList<>(); head1.add("Name"); heads.add(head1); // 动态列 for (xxx) { List<String> dnys = new ArrayList<>(); dnys.add("Colxx"); heads.add(.); } List<String> end = new ArrayList<>(); end.add("End"); heads.add(end); // 设置列 EasyExcel.write(out).head(heads)
.registerWriteHandler( new SheetWriteHandler() { @Override public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { Sheet sheet = writeSheetHolder.getSheet(); DataValidationHelper helper = sheet.getDataValidationHelper(); // 设置下拉列表单元格的位置首行 末行 首列 末列 CellRangeAddressList rangeList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 设置下拉列表的值:strArr 为字符串数组,下拉框的值 DataValidationConstraint constraint = helper.createExplicitListConstraint(strArr); // 设置错误提示 DataValidation validation = helper.createValidation(constraint, rangeList); // 非下拉选项的值不能输入 validation.setErrorStyle(DataValidation.ErrorStyle.STOP); validation.setSuppressDropDownArrow(true); validation.setShowErrorBox(true); validation.createErrorBox("提示", "请选择/输入下拉选项中的值"); sheet.addValidationData(validation); } } )
.registerWriteHandler(new CellWriteHandler() {
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
// 设置隐藏列
context.getWriteSheetHolder().getSheet().setColumnHidden(0, true);
for (WriteCellData<?> writeCellData : context.getCellDataList()) {
// 设置文本格式
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setIndex((short) 49);
writeCellData.getOrCreateStyle().setDataFormatData(dataFormatData);
}
}
})
最后指定sheet名称,写入数据即可。
.sheet("导入").doWrite(datas)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。