From 4dc5992ebe61ba55194f63d47ca316ee5a7c58a6 Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Thu, 11 Jul 2019 22:00:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5before=20after=20=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/ExcelWriter.java | 33 ++++--- .../excel/analysis/BaseSaxAnalyser.java | 40 ++------- .../excel/analysis/ExcelAnalyserImpl.java | 4 +- .../excel/context/WriteContextImpl.java | 90 ++++++++++++------- .../converters/ConverterRegistryCenter.java | 3 +- .../converters/DefaultConverterBuilder.java | 38 ++++++++ .../excel/event/ModelBuildEventListener.java | 40 +++++---- .../alibaba/excel/metadata/BaseRowModel.java | 37 ++++++++ .../com/alibaba/excel/metadata/Workbook.java | 15 ++++ .../holder/ConfigurationSelector.java | 2 +- .../com/alibaba/excel/util/WorkBookUtil.java | 6 ++ .../write/builder/ExcelWriterBuilder.java | 30 ++++++- .../handler/DefaultWriteHandlerBuilder.java | 38 ++++++++ .../write/handler/SheetWriteHandler.java | 7 +- .../write/handler/WorkbookWriteHandler.java | 4 +- .../easyexcel/test/model/WriteModel.java | 1 - 16 files changed, 283 insertions(+), 105 deletions(-) create mode 100644 src/main/java/com/alibaba/excel/converters/DefaultConverterBuilder.java create mode 100644 src/main/java/com/alibaba/excel/metadata/BaseRowModel.java create mode 100644 src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerBuilder.java diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java index 3a7edbf6..85677d7f 100644 --- a/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/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 null(the 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> 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> 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> 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> 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); + } /** diff --git a/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java index 453e26fe..8db4a89b 100644 --- a/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java +++ b/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 getConverters() { - return converters.values(); + public Map getConverters() { + return converters; } @Override @@ -130,7 +108,7 @@ public abstract class BaseSaxAnalyser implements ConverterRegistryCenter, Analys } } - private void buildExcelHeadProperty(Class clazz, List headOneRow) { + private void buildExcelHeadProperty(Class clazz, List headOneRow) { ExcelHeadProperty excelHeadProperty = ExcelHeadProperty.buildExcelHeadProperty(this.analysisContext.getExcelHeadProperty(), clazz, headOneRow); this.analysisContext.setExcelHeadProperty(excelHeadProperty); diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 42e40a0d..3e03efba 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/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 getConverters() { + public Map getConverters() { return saxAnalyser.getConverters(); } }; diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index f8fad84c..252612d7 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/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 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 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 converterMap = loadDefaultConverter(); + Map 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 loadDefaultHandler() { - List handlerList = new ArrayList(); - 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())); - handlerList.add(new SimpleColumnWidthStyleStrategy(20)); - return handlerList; - } - - private Map loadDefaultConverter() { - Map converterMap = new HashMap(); - 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 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 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)); } } diff --git a/src/main/java/com/alibaba/excel/converters/ConverterRegistryCenter.java b/src/main/java/com/alibaba/excel/converters/ConverterRegistryCenter.java index 7020ba7f..4905fb19 100644 --- a/src/main/java/com/alibaba/excel/converters/ConverterRegistryCenter.java +++ b/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 getConverters(); + Map getConverters(); } diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterBuilder.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterBuilder.java new file mode 100644 index 00000000..b74752ce --- /dev/null +++ b/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 loadDefaultWriteConverter() { + Map converterMap = new HashMap(); + 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 loadDefaultReadConverter() { + Map converterMap = new HashMap(); + return converterMap; + } +} diff --git a/src/main/java/com/alibaba/excel/event/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/event/ModelBuildEventListener.java index 193af9d2..8a4c8358 100644 --- a/src/main/java/com/alibaba/excel/event/ModelBuildEventListener.java +++ b/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 { - private final Collection converters; - public ModelBuildEventListener(Collection converters) { + private final Map converters; + public ModelBuildEventListener(Map converters) { this.converters = converters; } @@ -46,10 +46,14 @@ public class ModelBuildEventListener extends AnalysisEventListener { 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 { 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 diff --git a/src/main/java/com/alibaba/excel/metadata/BaseRowModel.java b/src/main/java/com/alibaba/excel/metadata/BaseRowModel.java new file mode 100644 index 00000000..35f3b275 --- /dev/null +++ b/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 cellStyleMap = new HashMap(); + + public void addStyle(Integer row, CellStyle cellStyle) { + cellStyleMap.put(row, cellStyle); + } + + public CellStyle getStyle(Integer row) { + return cellStyleMap.get(row); + } + + public Map getCellStyleMap() { + return cellStyleMap; + } + + public void setCellStyleMap(Map cellStyleMap) { + this.cellStyleMap = cellStyleMap; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/Workbook.java b/src/main/java/com/alibaba/excel/metadata/Workbook.java index f0ea98cf..6f53efa6 100644 --- a/src/main/java/com/alibaba/excel/metadata/Workbook.java +++ b/src/main/java/com/alibaba/excel/metadata/Workbook.java @@ -61,6 +61,13 @@ public class Workbook { * Custom type handler override the default */ private List customWriteHandlerList = new ArrayList(); + /** + * 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 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; + } } diff --git a/src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java b/src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java index 1de3db0e..1ae33843 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/ConfigurationSelector.java @@ -20,7 +20,7 @@ public interface ConfigurationSelector { * * @return */ - List writeHandlerList(); + Map, List> writeHandlerMap(); /** * What converter does the currently operated cell need to execute diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index 987972b6..3b9109a7 100644 --- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/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; + } } diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index 09cbb0da..4b6ae8c6 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/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 customWriteHandlerList = new ArrayList(); + /** + * 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); } } diff --git a/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerBuilder.java b/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerBuilder.java new file mode 100644 index 00000000..74a47c14 --- /dev/null +++ b/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 loadDefaultHandler() { + List handlerList = new ArrayList(); + 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())); + handlerList.add(new SimpleColumnWidthStyleStrategy(20)); + return handlerList; + } + +} diff --git a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java index 02a98a2d..d6342384 100644 --- a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java +++ b/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); } diff --git a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java index 9d1e5cc2..9edb0182 100644 --- a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java +++ b/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); } diff --git a/src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java b/src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java index 8fa493e7..c3e306b0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java +++ b/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;