Browse Source

加入before after 事件

developing
zhuangjiaju 5 years ago
parent
commit
4dc5992ebe
  1. 33
      src/main/java/com/alibaba/excel/ExcelWriter.java
  2. 40
      src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java
  3. 4
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  4. 90
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  5. 3
      src/main/java/com/alibaba/excel/converters/ConverterRegistryCenter.java
  6. 38
      src/main/java/com/alibaba/excel/converters/DefaultConverterBuilder.java
  7. 40
      src/main/java/com/alibaba/excel/event/ModelBuildEventListener.java
  8. 37
      src/main/java/com/alibaba/excel/metadata/BaseRowModel.java
  9. 15
      src/main/java/com/alibaba/excel/metadata/Workbook.java
  10. 2
      src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java
  11. 6
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  12. 30
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  13. 38
      src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerBuilder.java
  14. 7
      src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java
  15. 4
      src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java
  16. 1
      src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java

33
src/main/java/com/alibaba/excel/ExcelWriter.java

@ -33,7 +33,7 @@ public class ExcelWriter {
/**
* Create new writer
*
*
* @param templateInputStream
* Append value after a POI file ,Can be nullthe template POI filesystem that contains the Workbook
* stream)
@ -57,6 +57,15 @@ public class ExcelWriter {
excelBuilder = new ExcelBuilderImpl(workbook);
}
/**
* Create new writer
*
* @param workbook
*/
public ExcelWriter(Workbook workbook) {
excelBuilder = new ExcelBuilderImpl(workbook);
}
/**
* Create new writer
*
@ -184,9 +193,8 @@ public class ExcelWriter {
* @deprecated please use {@link ExcelWriter#write(List, Sheet)}
*/
@Deprecated
public ExcelWriter write1(List<List<Object>> data, Sheet sheet) {
excelBuilder.addContent(data, sheet);
return this;
public ExcelWriter write1(List data, Sheet sheet) {
return write(data, sheet);
}
/**
@ -199,9 +207,8 @@ public class ExcelWriter {
* @deprecated please use {@link ExcelWriter#write(List, Sheet)}
*/
@Deprecated
public ExcelWriter write0(List<List<String>> data, Sheet sheet) {
excelBuilder.addContent(data, sheet);
return this;
public ExcelWriter write0(List data, Sheet sheet) {
return write(data, sheet);
}
/**
@ -216,9 +223,9 @@ public class ExcelWriter {
* @deprecated please use {@link ExcelWriter#write(List, Sheet,Table)}
*/
@Deprecated
public ExcelWriter write0(List<List<String>> data, Sheet sheet, Table table) {
excelBuilder.addContent(data, sheet, table);
return this;
public ExcelWriter write0(List data, Sheet sheet, Table table) {
return write(data, sheet, table);
}
/**
@ -233,9 +240,9 @@ public class ExcelWriter {
* @deprecated please use {@link ExcelWriter#write(List, Sheet,Table)}
*/
@Deprecated
public ExcelWriter write1(List<List<Object>> data, Sheet sheet, Table table) {
excelBuilder.addContent(data, sheet, table);
return this;
public ExcelWriter write1(List data, Sheet sheet, Table table) {
return write(data, sheet, table);
}
/**

40
src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java

@ -8,21 +8,16 @@ import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.BooleanConverter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKey;
import com.alibaba.excel.converters.ConverterRegistryCenter;
import com.alibaba.excel.converters.DateConverter;
import com.alibaba.excel.converters.Double2Converter;
import com.alibaba.excel.converters.DoubleConverter;
import com.alibaba.excel.converters.FloatConverter;
import com.alibaba.excel.converters.IntegerConverter;
import com.alibaba.excel.converters.LongConverter;
import com.alibaba.excel.converters.StringConverter;
import com.alibaba.excel.converters.DefaultConverterBuilder;
import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.event.AnalysisEventRegistryCenter;
import com.alibaba.excel.event.AnalysisFinishEvent;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.ExcelHeadProperty;
import com.alibaba.excel.metadata.Sheet;
@ -52,7 +47,7 @@ public abstract class BaseSaxAnalyser implements ConverterRegistryCenter, Analys
@Override
public void register(Converter converter) {
converters.put(converter.getName(), converter);
converters.put(new ConverterKey(converter.supportJavaTypeKey(),converter.supportExcelTypeKey()), converter);
}
@Override
@ -61,29 +56,12 @@ public abstract class BaseSaxAnalyser implements ConverterRegistryCenter, Analys
}
private void registerDefaultConverters() {
Double2Converter double2Converter = new Double2Converter();
converters.put(ConverterKey.buildConverterKey(double2Converter.supportJavaTypeKey(),
double2Converter.supportExcelTypeKey()), double2Converter);
StringConverter s = new StringConverter();
converters.put(s.getName(), s);
DateConverter d = new DateConverter(this.analysisContext);
converters.put(d.getName(), d);
IntegerConverter i = new IntegerConverter();
converters.put(i.getName(), i);
DoubleConverter dc = new DoubleConverter();
converters.put(dc.getName(), dc);
LongConverter l = new LongConverter();
converters.put(l.getName(), l);
FloatConverter f = new FloatConverter();
converters.put(f.getName(), f);
BooleanConverter b = new BooleanConverter();
converters.put(b.getName(), b);
converters.putAll(DefaultConverterBuilder.loadDefaultReadConverter());
}
@Override
public Collection<Converter> getConverters() {
return converters.values();
public Map<ConverterKey, Converter> getConverters() {
return converters;
}
@Override
@ -130,7 +108,7 @@ public abstract class BaseSaxAnalyser implements ConverterRegistryCenter, Analys
}
}
private void buildExcelHeadProperty(Class<? extends BaseRowModel> clazz, List<String> headOneRow) {
private void buildExcelHeadProperty(Class clazz, List<String> headOneRow) {
ExcelHeadProperty excelHeadProperty =
ExcelHeadProperty.buildExcelHeadProperty(this.analysisContext.getExcelHeadProperty(), clazz, headOneRow);
this.analysisContext.setExcelHeadProperty(excelHeadProperty);

4
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -3,12 +3,14 @@ package com.alibaba.excel.analysis;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.analysis.v03.XlsSaxAnalyser;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKey;
import com.alibaba.excel.converters.ConverterRegistryCenter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
@ -34,7 +36,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
}
@Override
public Collection<Converter> getConverters() {
public Map<ConverterKey, Converter> getConverters() {
return saxAnalyser.getConverters();
}
};

90
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -10,7 +10,6 @@ import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
@ -18,14 +17,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.converters.DefaultConverterBuilder;
import com.alibaba.excel.event.NotRepeatExecutor;
import com.alibaba.excel.event.Order;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.ExcelHeadProperty;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.metadata.TableStyle;
@ -34,10 +31,12 @@ import com.alibaba.excel.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.TableHolder;
import com.alibaba.excel.metadata.holder.WorkbookHolder;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.handler.DefaultWriteHandlerBuilder;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
/**
* A context is the main anchorage point of a excel writer.
@ -73,16 +72,42 @@ public class WriteContextImpl implements WriteContext {
LOGGER.debug("Begin to Initialization 'WriteContextImpl'");
}
initCurrentWorkbookHolder(workbook);
beforeWorkbookCreate();
try {
currentWorkbookHolder.setWorkbook(WorkBookUtil.createWorkBook(workbook));
} catch (IOException e) {
throw new ExcelGenerateException("Create workbook failure", e);
}
afterWorkbookCreate();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'WriteContextImpl' complete");
}
}
private void beforeWorkbookCreate() {
List<WriteHandler> handlerList = currentConfigurationSelector.writeHandlerMap().get(WorkbookWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate();
}
}
}
private void afterWorkbookCreate() {
List<WriteHandler> handlerList = currentConfigurationSelector.writeHandlerMap().get(WorkbookWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(currentWorkbookHolder);
}
}
}
private void initCurrentWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) {
currentWorkbookHolder = new WorkbookHolder();
currentWorkbookHolder.setWorkbookParam(workbook);
@ -102,9 +127,9 @@ public class WriteContextImpl implements WriteContext {
if (workbook.getCustomWriteHandlerList() != null && !workbook.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(workbook.getCustomWriteHandlerList());
}
handlerList.addAll(loadDefaultHandler());
handlerList.addAll(DefaultWriteHandlerBuilder.loadDefaultHandler());
currentWorkbookHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList));
Map<Class, Converter> converterMap = loadDefaultConverter();
Map<Class, Converter> converterMap = DefaultConverterBuilder.loadDefaultWriteConverter();
if (workbook.getCustomConverterMap() != null && !workbook.getCustomConverterMap().isEmpty()) {
converterMap.putAll(workbook.getCustomConverterMap());
}
@ -150,28 +175,6 @@ public class WriteContextImpl implements WriteContext {
return result;
}
private List<WriteHandler> loadDefaultHandler() {
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();
CellStyle headCellStyle = new CellStyle();
Font font = new Font();
headCellStyle.setFont(font);
font.setFontName("宋体");
font.setBold(true);
headCellStyle.setIndexedColors(IndexedColors.GREY_25_PERCENT);
handlerList.add(new RowCellStyleStrategy(headCellStyle, new ArrayList<CellStyle>()));
handlerList.add(new SimpleColumnWidthStyleStrategy(20));
return handlerList;
}
private Map<Class, Converter> loadDefaultConverter() {
Map<Class, Converter> converterMap = new HashMap<Class, Converter>();
DateStringConverter dateStringConverter = new DateStringConverter();
converterMap.put(dateStringConverter.supportJavaTypeKey(), dateStringConverter);
BigDecimalNumberConverter bigDecimalNumberConverter = new BigDecimalNumberConverter();
converterMap.put(bigDecimalNumberConverter.supportJavaTypeKey(), bigDecimalNumberConverter);
return converterMap;
}
/**
* @param sheet
*/
@ -195,9 +198,36 @@ public class WriteContextImpl implements WriteContext {
}
return;
}
initCurrentSheetHolder(sheet);
beforeSheetCreate();
// Initialization current sheet
initSheet(sheet);
afterSheetCreate();
}
private void beforeSheetCreate() {
List<WriteHandler> handlerList = currentConfigurationSelector.writeHandlerMap().get(SheetWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).beforeSheetCreate(currentWorkbookHolder, currentSheetHolder);
}
}
}
private void afterSheetCreate() {
List<WriteHandler> handlerList = currentConfigurationSelector.writeHandlerMap().get(SheetWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).afterSheetCreate(currentWorkbookHolder, currentSheetHolder);
}
}
}
private void initCurrentSheetHolder(com.alibaba.excel.metadata.Sheet sheet) {
@ -313,7 +343,7 @@ public class WriteContextImpl implements WriteContext {
for (int i = 0; i < headList.size(); i++) {
Head head = headList.get(i);
// TODO 创建cell
Cell cell = WorkBookUtil.createCell(row, i, null, head.getHeadName(i));
Cell cell = WorkBookUtil.createCell(row, i, head.getHeadName(i));
}
}

3
src/main/java/com/alibaba/excel/converters/ConverterRegistryCenter.java

@ -1,8 +1,9 @@
package com.alibaba.excel.converters;
import java.util.Collection;
import java.util.Map;
public interface ConverterRegistryCenter {
void register(Converter converter);
Collection<Converter> getConverters();
Map<ConverterKey, Converter> getConverters();
}

38
src/main/java/com/alibaba/excel/converters/DefaultConverterBuilder.java

@ -0,0 +1,38 @@
package com.alibaba.excel.converters;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.date.DateStringConverter;
/**
* Build default handler
*
* @author zhuangjiaju
*/
public class DefaultConverterBuilder {
/**
* Load default wirte converter
*
* @return
*/
public static Map<Class, Converter> loadDefaultWriteConverter() {
Map<Class, Converter> converterMap = new HashMap<Class, Converter>();
DateStringConverter dateStringConverter = new DateStringConverter();
converterMap.put(dateStringConverter.supportJavaTypeKey(), dateStringConverter);
BigDecimalNumberConverter bigDecimalNumberConverter = new BigDecimalNumberConverter();
converterMap.put(bigDecimalNumberConverter.supportJavaTypeKey(), bigDecimalNumberConverter);
return converterMap;
}
/**
* Load default read converter
*
* @return
*/
public static Map<ConverterKey, Converter> loadDefaultReadConverter() {
Map<ConverterKey, Converter> converterMap = new HashMap<ConverterKey, Converter>();
return converterMap;
}
}

40
src/main/java/com/alibaba/excel/event/ModelBuildEventListener.java

@ -1,18 +1,18 @@
package com.alibaba.excel.event;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.converters.ConverterKey;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.ExcelColumnProperty;
import com.alibaba.excel.metadata.ExcelHeadProperty;
import com.alibaba.excel.util.TypeUtil;
import net.sf.cglib.beans.BeanMap;
@ -20,8 +20,8 @@ import net.sf.cglib.beans.BeanMap;
* @author jipengfei
*/
public class ModelBuildEventListener extends AnalysisEventListener<Object> {
private final Collection<Converter> converters;
public ModelBuildEventListener(Collection<Converter> converters) {
private final Map<ConverterKey, Converter> converters;
public ModelBuildEventListener(Map<ConverterKey, Converter> converters) {
this.converters = converters;
}
@ -46,10 +46,14 @@ public class ModelBuildEventListener extends AnalysisEventListener<Object> {
return resultModel;
}
Map map = new HashMap();
for (int i = 0; i < stringList.size(); i++) {
for (int i = 0; i < cellDataList.size(); i++) {
ExcelColumnProperty columnProperty = excelHeadProperty.getExcelColumnProperty(i);
if (columnProperty != null) {
Object value = convertValue(stringList.get(i), columnProperty);
CellData cellData = cellDataList.get(i);
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
continue;
}
Object value = convertValue(cellDataList.get(i), columnProperty.getField().getClass(), columnProperty);
if (value != null) {
map.put(columnProperty.getField().getName(), value);
}
@ -59,19 +63,17 @@ public class ModelBuildEventListener extends AnalysisEventListener<Object> {
return resultModel;
}
private Object convertValue(CellData cellData, ExcelColumnProperty columnProperty) {
// columnProperty.getField().getClass(), cellData.getType();
for (Converter c : converters) {
// c.convertToJavaData(cellData,columnProperty);
if (c.support(columnProperty)) {
return c.convert(value, columnProperty);
}
private Object convertValue(CellData cellData, Class clazz, ExcelColumnProperty columnProperty) {
Converter converter = converters.get(ConverterKey.buildConverterKey(clazz, cellData.getType()));
if (converter == null) {
throw new ExcelDataConvertException(
"Converter not found, converte " + cellData.getType() + " to " + clazz.getName());
}
try {
return converter.convertToJavaData(cellData, columnProperty);
} catch (Exception e) {
throw new ExcelDataConvertException("Convert data " + cellData + " to " + clazz + " error ", e);
}
return null;
}
@Override

37
src/main/java/com/alibaba/excel/metadata/BaseRowModel.java

@ -0,0 +1,37 @@
package com.alibaba.excel.metadata;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellStyle;
/**
* Excel基础模型
*
* @author jipengfei
* @deprecated Now you don't need to extend any classes
*/
@Deprecated
public class BaseRowModel {
/**
* 每列样式
*/
private Map<Integer, CellStyle> cellStyleMap = new HashMap<Integer, CellStyle>();
public void addStyle(Integer row, CellStyle cellStyle) {
cellStyleMap.put(row, cellStyle);
}
public CellStyle getStyle(Integer row) {
return cellStyleMap.get(row);
}
public Map<Integer, CellStyle> getCellStyleMap() {
return cellStyleMap;
}
public void setCellStyleMap(Map<Integer, CellStyle> cellStyleMap) {
this.cellStyleMap = cellStyleMap;
}
}

15
src/main/java/com/alibaba/excel/metadata/Workbook.java

@ -61,6 +61,13 @@ public class Workbook {
* Custom type handler override the default
*/
private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>();
/**
* Write handler
*
* @deprecated please use {@link WriteHandler}
*/
@Deprecated
private com.alibaba.excel.event.WriteHandler writeHandler;
public ExcelTypeEnum getExcelType() {
return excelType;
@ -141,4 +148,12 @@ public class Workbook {
public void setCustomWriteHandlerList(List<WriteHandler> customWriteHandlerList) {
this.customWriteHandlerList = customWriteHandlerList;
}
public com.alibaba.excel.event.WriteHandler getWriteHandler() {
return writeHandler;
}
public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) {
this.writeHandler = writeHandler;
}
}

2
src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java

@ -20,7 +20,7 @@ public interface ConfigurationSelector {
*
* @return
*/
List<WriteHandler> writeHandlerList();
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap();
/**
* What converter does the currently operated cell need to execute

6
src/main/java/com/alibaba/excel/util/WorkBookUtil.java

@ -51,4 +51,10 @@ public class WorkBookUtil {
cell.setCellValue(cellValue);
return cell;
}
public static Cell createCell(Row row, int colNum, String cellValue) {
Cell cell = row.createCell(colNum);
cell.setCellValue(cellValue);
return cell;
}
}

30
src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -14,6 +14,7 @@ import java.util.Map;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.Workbook;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.WriteHandler;
@ -47,6 +48,13 @@ public class ExcelWriterBuilder {
* Custom type handler override the default
*/
private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>();
/**
* Write handler
*
* @deprecated please use {@link WriteHandler}
*/
@Deprecated
private com.alibaba.excel.event.WriteHandler writeHandler;
public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) {
this.excelType = excelType;
@ -113,8 +121,26 @@ public class ExcelWriterBuilder {
return this;
}
/**
* Write handler
*
* @deprecated please use {@link WriteHandler}
*/
@Deprecated
public ExcelWriterBuilder registerWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) {
this.writeHandler = writeHandler;
return this;
}
public ExcelWriter build() {
return new ExcelWriter(templateInputStream, outputStream, excelType, needHead, customConverterMap,
customWriteHandlerList);
Workbook workbook = new Workbook();
workbook.setTemplateInputStream(templateInputStream);
workbook.setOutputStream(outputStream);
workbook.setExcelType(excelType);
workbook.setNeedHead(needHead);
workbook.setCustomConverterMap(customConverterMap);
workbook.setCustomWriteHandlerList(customWriteHandlerList);
workbook.setWriteHandler(writeHandler);
return new ExcelWriter(workbook);
}
}

38
src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerBuilder.java

@ -0,0 +1,38 @@
package com.alibaba.excel.write.handler;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.IndexedColors;
import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
/**
* Build default handler
*
* @author zhuangjiaju
*/
public class DefaultWriteHandlerBuilder {
/**
* Load default handler
*
* @return
*/
public static List<WriteHandler> loadDefaultHandler() {
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();
CellStyle headCellStyle = new CellStyle();
Font font = new Font();
headCellStyle.setFont(font);
font.setFontName("宋体");
font.setBold(true);
headCellStyle.setIndexedColors(IndexedColors.GREY_25_PERCENT);
handlerList.add(new RowCellStyleStrategy(headCellStyle, new ArrayList<CellStyle>()));
handlerList.add(new SimpleColumnWidthStyleStrategy(20));
return handlerList;
}
}

7
src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java

@ -1,8 +1,7 @@
package com.alibaba.excel.write.handler;
import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.WorkbookHolder;
/**
* intercepts handle sheet creation
@ -16,12 +15,12 @@ public interface SheetWriteHandler extends WriteHandler {
*
* @param writeContext
*/
void beforeSheetCreate(Workbook workbook, SheetHolder sheetHolder);
void beforeSheetCreate(WorkbookHolder workbookHolder, SheetHolder sheetHolder);
/**
* called after the sheet is created
*
* @param writeContext
*/
void afterSheetCreate(Workbook workbook, SheetHolder sheetHolder);
void afterSheetCreate(WorkbookHolder workbookHolder, SheetHolder sheetHolder);
}

4
src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java

@ -1,6 +1,6 @@
package com.alibaba.excel.write.handler;
import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.metadata.holder.WorkbookHolder;
/**
* intercepts handle Workbook creation
@ -21,5 +21,5 @@ public interface WorkbookWriteHandler extends WriteHandler {
*
* @param writeContext
*/
void afterWorkbookCreate(Workbook workbook);
void afterWorkbookCreate(WorkbookHolder workbookHolder);
}

1
src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java

@ -1,7 +1,6 @@
package com.alibaba.easyexcel.test.model;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import java.math.BigDecimal;
import java.util.Date;

Loading…
Cancel
Save