diff --git a/src/main/java/com/alibaba/excel/EasyExcelFactory.java b/src/main/java/com/alibaba/excel/EasyExcelFactory.java index aa6cfa1e..d6229bbf 100644 --- a/src/main/java/com/alibaba/excel/EasyExcelFactory.java +++ b/src/main/java/com/alibaba/excel/EasyExcelFactory.java @@ -81,7 +81,8 @@ public class EasyExcelFactory { */ @Deprecated public static ExcelWriter getWriter(OutputStream outputStream) { - return writerBuilder().outputFile(outputStream).autoCloseStream(Boolean.FALSE).build(); + return writerBuilder().outputFile(outputStream).autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE) + .build(); } /** @@ -99,7 +100,7 @@ public class EasyExcelFactory { @Deprecated public static ExcelWriter getWriter(OutputStream outputStream, ExcelTypeEnum typeEnum, boolean needHead) { return writerBuilder().outputFile(outputStream).excelType(typeEnum).needHead(needHead) - .autoCloseStream(Boolean.FALSE).build(); + .autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE).build(); } /** @@ -119,7 +120,7 @@ public class EasyExcelFactory { public static ExcelWriter getWriterWithTemp(InputStream temp, OutputStream outputStream, ExcelTypeEnum typeEnum, boolean needHead) { return writerBuilder().withTemplate(temp).outputFile(outputStream).excelType(typeEnum).needHead(needHead) - .autoCloseStream(Boolean.FALSE).build(); + .autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE).build(); } /** @@ -142,7 +143,7 @@ public class EasyExcelFactory { public static ExcelWriter getWriterWithTempAndHandler(InputStream temp, OutputStream outputStream, ExcelTypeEnum typeEnum, boolean needHead, WriteHandler handler) { return writerBuilder().withTemplate(temp).outputFile(outputStream).excelType(typeEnum).needHead(needHead) - .registerWriteHandler(handler).autoCloseStream(Boolean.FALSE).build(); + .registerWriteHandler(handler).autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE).build(); } public static ExcelWriterBuilder writerBuilder() { diff --git a/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java index 8db4a89b..678cdfc5 100644 --- a/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/BaseSaxAnalyser.java @@ -1,6 +1,5 @@ package com.alibaba.excel.analysis; -import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -11,14 +10,11 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKey; import com.alibaba.excel.converters.ConverterRegistryCenter; -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.converters.DefaultConverterLoader; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventRegistryCenter; import com.alibaba.excel.event.AnalysisFinishEvent; -import com.alibaba.excel.metadata.ExcelHeadProperty; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.Sheet; /** @@ -56,7 +52,7 @@ public abstract class BaseSaxAnalyser implements ConverterRegistryCenter, Analys } private void registerDefaultConverters() { - converters.putAll(DefaultConverterBuilder.loadDefaultReadConverter()); + converters.putAll(DefaultConverterLoader.loadDefaultReadConverter()); } @Override diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index 21353113..275d9337 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -1,15 +1,16 @@ package com.alibaba.excel.analysis.v07.handlers; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_FORMULA_TAG; +import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_INLINE_STRING_VALUE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG; -import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG_1; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; import java.util.Arrays; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; import com.alibaba.excel.analysis.v07.XlsxCellHandler; @@ -48,7 +49,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder @Override public boolean support(String name) { - return CELL_VALUE_TAG.equals(name) || CELL_FORMULA_TAG.equals(name) || CELL_VALUE_TAG_1.equals(name) + return CELL_VALUE_TAG.equals(name) || CELL_FORMULA_TAG.equals(name) || CELL_INLINE_STRING_VALUE_TAG.equals(name) || CELL_TAG.equals(name); } @@ -61,13 +62,14 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder int nextRow = PositionUtils.getRow(currentCellIndex); if (nextRow > curRow) { curRow = nextRow; - // endRow(ROW_TAG); } analysisContext.setCurrentRowNum(curRow); curCol = PositionUtils.getCol(currentCellIndex); // t="s" ,it's means String + // t="inlineStr" ,it's means String // t="b" ,it's means Boolean + // t="e" ,it's means Error // t is null ,it's means Empty or Number CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(CELL_VALUE_TYPE_TAG)); currentCellData = new CellData(type); @@ -89,6 +91,13 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } curRowContent[curCol] = currentCellData; } + // This is a special form of string + if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { + ensureSize(); + XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue()); + currentCellData.setStringValue(richTextString.toString()); + curRowContent[curCol] = currentCellData; + } } private void ensureSize() { diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelColumnNum.java b/src/main/java/com/alibaba/excel/annotation/ExcelColumnNum.java deleted file mode 100644 index eb56fe38..00000000 --- a/src/main/java/com/alibaba/excel/annotation/ExcelColumnNum.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.alibaba.excel.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by jipengfei on 17/3/19. - * Field column num at excel head - * - * @author jipengfei - */ -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -@Inherited -public @interface ExcelColumnNum { - - /** - * col num - * @return - */ - int value(); - - /** - * - * Default @see com.alibaba.excel.util.TypeUtil - * if default is not meet you can set format - * - * @return - */ - String format() default ""; -} diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelIgnore.java b/src/main/java/com/alibaba/excel/annotation/ExcelIgnore.java new file mode 100644 index 00000000..19666a65 --- /dev/null +++ b/src/main/java/com/alibaba/excel/annotation/ExcelIgnore.java @@ -0,0 +1,17 @@ +package com.alibaba.excel.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Ignore convert excel + * + * @author zhuangjiaju + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface ExcelIgnore {} diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java index 7b1cb273..a977aa28 100644 --- a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java +++ b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java @@ -14,20 +14,32 @@ import java.lang.annotation.Target; @Inherited public @interface ExcelProperty { - /** - * @return - */ - String[] value() default {""}; - /** - * @return - */ - int index() default 99999; - /** - * - * default @see com.alibaba.excel.util.TypeUtil - * if default is not meet you can set format - * - * @return - */ - String format() default ""; + /** + * The name of the sheet header. + * + *
8*256
+ *
+ * + * -1 mean the auto set width + */ + int value() default -1; +} diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/ContentRowHeight.java b/src/main/java/com/alibaba/excel/annotation/write/style/ContentRowHeight.java new file mode 100644 index 00000000..cf3be9bd --- /dev/null +++ b/src/main/java/com/alibaba/excel/annotation/write/style/ContentRowHeight.java @@ -0,0 +1,27 @@ +package com.alibaba.excel.annotation.write.style; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Set the height of each table + * + * @author zhuangjiaju + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface ContentRowHeight { + + /** + * Set the content height + *
+ * -1 mean the auto set height + * + * @return + */ + short value() default -1; +} diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/ContentStyle.java b/src/main/java/com/alibaba/excel/annotation/write/style/ContentStyle.java new file mode 100644 index 00000000..73fbfc93 --- /dev/null +++ b/src/main/java/com/alibaba/excel/annotation/write/style/ContentStyle.java @@ -0,0 +1,31 @@ +package com.alibaba.excel.annotation.write.style; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.apache.poi.ss.usermodel.IndexedColors; + +/** + * Convert number format. + * + *
+ * -1 mean the auto set height + * + * @return + */ + short value() default -1; +} diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java b/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java new file mode 100644 index 00000000..f717e932 --- /dev/null +++ b/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java @@ -0,0 +1,27 @@ +package com.alibaba.excel.annotation.write.style; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.apache.poi.ss.usermodel.IndexedColors; + +/** + * 配置 + * + * @author zhuangjiaju + */ +@Target({ElementType.FIELD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface HeadStyle { + String fontName() default "宋体"; + + short fontHeightInPoints() default (short)14; + + boolean bold() default true; + + IndexedColors indexedColors() default IndexedColors.GREY_25_PERCENT; +} diff --git a/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java b/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java index b3e7c200..55d522fa 100644 --- a/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java +++ b/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java @@ -10,10 +10,12 @@ public class ExcelXmlConstants { public static final String ROW_TAG = "row"; public static final String CELL_TAG = "c"; + public static final String CELL_VALUE_TYPE_TAG = "t"; public static final String CELL_FORMULA_TAG = "f"; public static final String CELL_VALUE_TAG = "v"; - public static final String CELL_VALUE_TYPE_TAG = "t"; - - public static final String CELL_VALUE_TAG_1 = "t"; + /** + * When the data is "inlineStr" his tag is "t" + */ + public static final String CELL_INLINE_STRING_VALUE_TAG = "t"; } diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java index 74e3782d..6025d24f 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java @@ -4,7 +4,7 @@ import java.io.InputStream; import com.alibaba.excel.converters.ConverterRegistryCenter; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.excel.metadata.ExcelHeadProperty; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index b7f721e4..b5e6a5de 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -5,7 +5,7 @@ import java.io.InputStream; import com.alibaba.excel.converters.ConverterRegistryCenter; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.metadata.ExcelHeadProperty; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 44757eeb..df28d9f9 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -2,6 +2,7 @@ package com.alibaba.excel.context; import java.io.IOException; import java.util.List; +import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -11,13 +12,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.excel.exception.ExcelGenerateException; -import com.alibaba.excel.metadata.ExcelHeadProperty; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Table; import com.alibaba.excel.metadata.holder.ConfigurationSelector; 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.metadata.property.ExcelHeadProperty; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler; @@ -184,9 +185,6 @@ public class WriteContextImpl implements WriteContext { } currentSheetHolder.setSheet(currentSheet); // Initialization head - currentSheetHolder - .setExcelHeadProperty(new ExcelHeadProperty(currentSheetHolder.getClazz(), currentSheetHolder.getHead())); - // Initialization head initHead(currentSheetHolder.getExcelHeadProperty()); } @@ -203,7 +201,7 @@ public class WriteContextImpl implements WriteContext { beforeRowCreate(rowIndex, relativeRowIndex); Row row = WorkBookUtil.createRow(currentSheetHolder.getSheet(), i); afterRowCreate(row, relativeRowIndex); - addOneRowOfHeadDataToExcel(row, excelHeadProperty.getHeadList(), relativeRowIndex); + addOneRowOfHeadDataToExcel(row, excelHeadProperty.getHeadMap(), relativeRowIndex); } } @@ -237,18 +235,18 @@ public class WriteContextImpl implements WriteContext { } private void addMergedRegionToCurrentSheet(ExcelHeadProperty excelHeadProperty, int rowIndex) { - for (com.alibaba.excel.metadata.CellRange cellRangeModel : excelHeadProperty.getCellRangeModels()) { + for (com.alibaba.excel.metadata.CellRange cellRangeModel : excelHeadProperty.headCellRangeList()) { currentSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + rowIndex, cellRangeModel.getLastRow() + rowIndex, cellRangeModel.getFirstCol(), cellRangeModel.getLastCol())); } } - private void addOneRowOfHeadDataToExcel(Row row, List
headList, int relativeRowIndex) { - for (int i = 0; i < headList.size(); i++) { - Head head = headList.get(i); - beforeCellCreate(row, headList.get(i), relativeRowIndex); - Cell cell = WorkBookUtil.createCell(row, i, head.getHeadName(i)); - afterCellCreate(headList.get(i), cell, relativeRowIndex); + private void addOneRowOfHeadDataToExcel(Row row, Map