use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.
the class AbstractWriteHolder method dealStyle.
private void dealStyle(List<WriteHandler> handlerList) {
WriteHandler styleStrategy = new AbstractVerticalCellStyleStrategy() {
@Override
public int order() {
return OrderConstant.ANNOTATION_DEFINE_STYLE;
}
@Override
protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
Head head = context.getHeadData();
if (head == null) {
return null;
}
return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
}
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(), excelContentProperty.getContentFontProperty());
}
};
handlerList.add(styleStrategy);
}
use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.
the class AbstractWriteHolder method sortAndClearUpHandler.
protected void sortAndClearUpHandler(List<WriteHandler> handlerList, boolean runOwn) {
// sort
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<>();
for (WriteHandler handler : handlerList) {
int order = handler.order();
if (orderExcelWriteHandlerMap.containsKey(order)) {
orderExcelWriteHandlerMap.get(order).add(handler);
} else {
List<WriteHandler> tempHandlerList = new ArrayList<>();
tempHandlerList.add(handler);
orderExcelWriteHandlerMap.put(order, tempHandlerList);
}
}
// clean up
Set<String> alreadyExistedHandlerSet = new HashSet<>();
List<WriteHandler> cleanUpHandlerList = new ArrayList<>();
for (Map.Entry<Integer, List<WriteHandler>> entry : orderExcelWriteHandlerMap.entrySet()) {
for (WriteHandler handler : entry.getValue()) {
if (handler instanceof NotRepeatExecutor) {
String uniqueValue = ((NotRepeatExecutor) handler).uniqueValue();
if (alreadyExistedHandlerSet.contains(uniqueValue)) {
continue;
}
alreadyExistedHandlerSet.add(uniqueValue);
}
cleanUpHandlerList.add(handler);
}
}
// build chain
if (!runOwn) {
this.writeHandlerList = new ArrayList<>();
}
for (WriteHandler writeHandler : cleanUpHandlerList) {
buildChain(writeHandler, runOwn);
}
}
use of com.alibaba.excel.write.handler.WriteHandler in project easyexcel by alibaba.
the class AbstractWriteHolder method dealColumnWidth.
private void dealColumnWidth(List<WriteHandler> handlerList) {
WriteHandler columnWidthStyleStrategy = new AbstractHeadColumnWidthStyleStrategy() {
@Override
protected Integer columnWidth(Head head, Integer columnIndex) {
if (head == null) {
return null;
}
if (head.getColumnWidthProperty() != null) {
return head.getColumnWidthProperty().getWidth();
}
return null;
}
};
handlerList.add(columnWidthStyleStrategy);
}
use of com.alibaba.excel.write.handler.WriteHandler in project diboot by dibo-software.
the class ExcelHelper method writeData.
/**
* 简单将数据写入excel文件
* <p>默认列宽自适应数据长度、写入单元格下拉选项, 可自定义</p>
*
* @param filePath
* @param sheetName
* @param dataList
* @param <T>
* @return
*/
@Deprecated
public static <T extends BaseExcelModel> boolean writeData(String filePath, String sheetName, List<T> dataList, WriteHandler... writeHandlers) throws Exception {
try {
if (V.isEmpty(dataList)) {
return writeDynamicData(filePath, sheetName, Collections.emptyList());
}
Class<T> tClass = (Class<T>) dataList.get(0).getClass();
ExcelWriterBuilder write = EasyExcel.write(filePath, tClass);
write = write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
for (WriteHandler handler : writeHandlers) {
write = write.registerWriteHandler(handler);
}
write.sheet(sheetName).doWrite(dataList);
return true;
} catch (Exception e) {
log.error("数据写入excel文件失败", e);
return false;
}
}
use of com.alibaba.excel.write.handler.WriteHandler in project diboot by dibo-software.
the class ExcelHelper method buildWriteSheet.
/**
* 构建WriteSheet
* <p>
* 默认:自列适应宽、单元格下拉选项(验证)写入,批注写入
*
* @param sheetName 可指定sheetName
* @param columnNameList 需要导出的ExcelModel列字段名称列表,为空时导出所有列
* @param consumer
* @param writeHandlers
*/
public static <T> void buildWriteSheet(String sheetName, Collection<String> columnNameList, BiConsumer<CommentWriteHandler, WriteSheet> consumer, WriteHandler... writeHandlers) {
ExcelWriterSheetBuilder writerSheet = EasyExcel.writerSheet().sheetName(sheetName);
CommentWriteHandler commentWriteHandler = new CommentWriteHandler();
writerSheet.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
writerSheet.registerWriteHandler(new OptionWriteHandler());
writerSheet.registerWriteHandler(commentWriteHandler);
for (WriteHandler handler : writeHandlers) {
writerSheet.registerWriteHandler(handler);
}
if (V.notEmpty(columnNameList)) {
writerSheet.includeColumnFiledNames(columnNameList);
}
consumer.accept(commentWriteHandler, writerSheet.build());
}
Aggregations