diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java index a063a327..7b1cb273 100644 --- a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java +++ b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java @@ -18,13 +18,10 @@ public @interface ExcelProperty { * @return */ String[] value() default {""}; - - /** * @return */ int index() default 99999; - /** * * default @see com.alibaba.excel.util.TypeUtil diff --git a/src/main/java/com/alibaba/excel/annotation/FieldType.java b/src/main/java/com/alibaba/excel/annotation/FieldType.java deleted file mode 100644 index 73729959..00000000 --- a/src/main/java/com/alibaba/excel/annotation/FieldType.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.alibaba.excel.annotation; - -/** - * - * @author jipengfei - */ -public enum FieldType { - - STRING, INT, LONG, DATE, BOOLEAN, DOUBLE,EMPTY; - -} diff --git a/src/main/java/com/alibaba/excel/converters/Converter.java b/src/main/java/com/alibaba/excel/converters/Converter.java index 5443cbd5..d61938c1 100644 --- a/src/main/java/com/alibaba/excel/converters/Converter.java +++ b/src/main/java/com/alibaba/excel/converters/Converter.java @@ -6,13 +6,45 @@ import com.alibaba.excel.metadata.ExcelColumnProperty; import com.sun.istack.internal.NotNull; import com.sun.istack.internal.Nullable; +/** + * Convert between Java objects and excel objects + * + * @author Dan Zheng + * @param + */ public interface Converter { + /** + * Back to object types in Java + * + * @return + */ Class supportJavaTypeKey(); + /** + * Back to object enum in excel + * + * @return + */ CellDataTypeEnum supportExcelTypeKey(); + /** + * Convert excel objects to Java objects + * + * @param cellData + * @param columnProperty + * @return + * @throws Exception + */ T convertToJavaData(@NotNull CellData cellData, @Nullable ExcelColumnProperty columnProperty) throws Exception; + /** + * Convert Java objects to excel objects + * + * @param value + * @param columnProperty + * @return + * @throws Exception + */ CellData convertToExcelData(@NotNull T value, @Nullable ExcelColumnProperty columnProperty) throws Exception; } diff --git a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java new file mode 100644 index 00000000..fb8188c7 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.booleanconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Boolean and boolean converter + * + * @author zhuangjiaju + */ +public class BooleanBooleanConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Boolean.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.BOOLEAN; + } + + @Override + public Boolean convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return cellData.getBooleanValue(); + } + + @Override + public CellData convertToExcelData(Boolean value, ExcelColumnProperty columnProperty) { + return new CellData(value); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java new file mode 100644 index 00000000..800b63cc --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java @@ -0,0 +1,44 @@ +package com.alibaba.excel.converters.booleanconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Boolean and number converter + * + * @author zhuangjiaju + */ +public class BooleanNumberConverter implements Converter { + + private static final Double ONE = 1.0; + private static final Double ZERO = 0.0; + + @Override + public Class supportJavaTypeKey() { + return Boolean.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public Boolean convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + if (ONE.equals(cellData.getDoubleValue())) { + return Boolean.TRUE; + } + return Boolean.FALSE; + } + + @Override + public CellData convertToExcelData(Boolean value, ExcelColumnProperty columnProperty) { + if (value) { + return new CellData(ONE); + } + return new CellData(ZERO); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java new file mode 100644 index 00000000..b9fd8bc4 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.booleanconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Boolean and string converter + * + * @author zhuangjiaju + */ +public class BooleanStringConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Boolean.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public Boolean convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return Boolean.valueOf(cellData.getStringValue()); + } + + @Override + public CellData convertToExcelData(Boolean value, ExcelColumnProperty columnProperty) { + return new CellData(value.toString()); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java new file mode 100644 index 00000000..c41a584a --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java @@ -0,0 +1,43 @@ +package com.alibaba.excel.converters.byteconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Byte and boolean converter + * + * @author zhuangjiaju + */ +public class ByteBooleanConverter implements Converter { + private static final Byte ONE = (byte)1; + private static final Byte ZERO = (byte)0; + + @Override + public Class supportJavaTypeKey() { + return Byte.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.BOOLEAN; + } + + @Override + public Byte convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + if (cellData.getBooleanValue()) { + return ONE; + } + return ZERO; + } + + @Override + public CellData convertToExcelData(Byte value, ExcelColumnProperty columnProperty) { + if (ONE.equals(value)) { + return new CellData(Boolean.TRUE); + } + return new CellData(Boolean.FALSE); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java new file mode 100644 index 00000000..0d54d720 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.byteconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Byte and number converter + * + * @author zhuangjiaju + */ +public class ByteNumberConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Byte.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public Byte convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return cellData.getDoubleValue().byteValue(); + } + + @Override + public CellData convertToExcelData(Byte value, ExcelColumnProperty columnProperty) { + return new CellData((double)value); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java new file mode 100644 index 00000000..9cd02276 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.byteconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Byte and string converter + * + * @author zhuangjiaju + */ +public class ByteStringConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Byte.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public Byte convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return Byte.valueOf(cellData.getStringValue()); + } + + @Override + public CellData convertToExcelData(Byte value, ExcelColumnProperty columnProperty) { + return new CellData(value.toString()); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteBooleanConverter.java new file mode 100644 index 00000000..2f6e1da6 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteBooleanConverter.java @@ -0,0 +1,43 @@ +package com.alibaba.excel.converters.doubleconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Byte and boolean converter + * + * @author zhuangjiaju + */ +public class ByteBooleanConverter implements Converter { + private static final Byte ONE = (byte)1; + private static final Byte ZERO = (byte)0; + + @Override + public Class supportJavaTypeKey() { + return Byte.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.BOOLEAN; + } + + @Override + public Byte convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + if (cellData.getBooleanValue()) { + return ONE; + } + return ZERO; + } + + @Override + public CellData convertToExcelData(Byte value, ExcelColumnProperty columnProperty) { + if (ONE.equals(value)) { + return new CellData(Boolean.TRUE); + } + return new CellData(Boolean.FALSE); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteNumberConverter.java b/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteNumberConverter.java new file mode 100644 index 00000000..db01aa7c --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/doubleconverter/ByteNumberConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.doubleconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Byte and number converter + * + * @author zhuangjiaju + */ +public class ByteNumberConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Byte.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public Byte convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return cellData.getDoubleValue().byteValue(); + } + + @Override + public CellData convertToExcelData(Byte value, ExcelColumnProperty columnProperty) { + return new CellData((double)value); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java new file mode 100644 index 00000000..e6620837 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java @@ -0,0 +1,34 @@ +package com.alibaba.excel.converters.doubleconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.ExcelColumnProperty; + +/** + * Double and string converter + * + * @author zhuangjiaju + */ +public class DoubleStringConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Double.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public Double convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { + return Double.valueOf(cellData.getStringValue()); + } + + @Override + public CellData convertToExcelData(Double value, ExcelColumnProperty columnProperty) { + return new CellData(value.toString()); + } +} diff --git a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java index 0081c2f9..d0c5edfd 100644 --- a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java +++ b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java @@ -11,10 +11,15 @@ import com.alibaba.excel.util.StringUtils; * @author zhuangjiaju */ public enum CellDataTypeEnum { + /** * string */ STRING, + /** + * inlineString + */ + INLINE_STRING, /** * number */ @@ -35,6 +40,7 @@ public enum CellDataTypeEnum { private static final Map TYPE_ROUTING_MAP = new HashMap(8); static { TYPE_ROUTING_MAP.put("s", STRING); + TYPE_ROUTING_MAP.put("inlineStr", INLINE_STRING); TYPE_ROUTING_MAP.put("e", ERROR); TYPE_ROUTING_MAP.put("b", BOOLEAN); } diff --git a/src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java b/src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java index bfecc6df..3e1fb4bd 100644 --- a/src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java @@ -30,6 +30,24 @@ public class ExcelColumnProperty implements Comparable { private Boolean use1904windowing; private TimeZone timeZone; + private int scale; + private int roundingMode; + + public int getScale() { + return scale; + } + + public void setScale(int scale) { + this.scale = scale; + } + + public int getRoundingMode() { + return roundingMode; + } + + public void setRoundingMode(int roundingMode) { + this.roundingMode = roundingMode; + } public Boolean getUse1904windowing() { return use1904windowing; diff --git a/src/main/java/com/alibaba/excel/util/IndexValueConverter.java b/src/main/java/com/alibaba/excel/util/IndexValueConverter.java deleted file mode 100644 index a0c1529c..00000000 --- a/src/main/java/com/alibaba/excel/util/IndexValueConverter.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.alibaba.excel.util; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -import com.alibaba.excel.metadata.IndexValue; - -/** - * 去除空Cell - * @author jipengfei - */ -public class IndexValueConverter { - public static List converter(List i_list) { - - List tem = new ArrayList(); - - char[] start = {'@'}; - int j = 0; - for (; j < i_list.size(); j++) { - IndexValue currentIndexValue = i_list.get(j); - char[] currentIndex = currentIndexValue.getV_index().replaceAll("[0-9]", "").toCharArray(); - if (j > 0) { - start = i_list.get(j - 1).getV_index().replaceAll("[0-9]", "").toCharArray(); - } - int deep = subtraction26(currentIndex, start); - int k = 0; - for (; k < deep - 1; k++) { - tem.add(null); - } - tem.add(currentIndexValue.getV_value()); - } - return tem; - } - - private static int subtraction26(char[] currentIndex, char[] beforeIndex) { - int result = 0; - - Stack currentStack = new Stack(); - Stack berforStack = new Stack(); - - for (int i = 0; i < currentIndex.length; i++) { - currentStack.push(currentIndex[i]); - } - for (int i = 0; i < beforeIndex.length; i++) { - berforStack.push(beforeIndex[i]); - } - int i = 0; - char beforechar = '@'; - while (!currentStack.isEmpty()) { - char currentChar = currentStack.pop(); - if (!berforStack.isEmpty()) { - beforechar = berforStack.pop(); - } - int n = currentChar - beforechar; - if(n<0){ - n = n+26; - if(!currentStack.isEmpty()){ - char borrow = currentStack.pop(); - char newBorrow =(char)(borrow -1); - currentStack.push(newBorrow); - } - } - - - result += n * Math.pow(26, i); - i++; - beforechar='@'; - } - - return result; - } -} diff --git a/src/main/java/com/alibaba/excel/util/RowUtil.java b/src/main/java/com/alibaba/excel/util/RowUtil.java deleted file mode 100644 index dc5ea013..00000000 --- a/src/main/java/com/alibaba/excel/util/RowUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.alibaba.excel.util; - -import org.apache.poi.ss.usermodel.Workbook; - -public class RowUtil { - - //public static int computeNextRow(Workbook workbook,int startRow){ - // - //} -} diff --git a/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java b/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java index 72e34a51..27d0a9a2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java +++ b/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java @@ -17,7 +17,16 @@ public class FileUtil { File file = new File(getPath() + pathName); if (file.exists()) { file.delete(); + } else { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } } + return file; } + + public static File createNewWriteFile(String pathName) { + return createNewFile("wirte/" + pathName); + } } diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java new file mode 100644 index 00000000..29e814b8 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java @@ -0,0 +1,29 @@ +package com.alibaba.easyexcel.test.wirte.order; + +import com.alibaba.excel.annotation.ExcelProperty; + +/** + * @author zhuangjiaju + */ +public class OrderData { + @ExcelProperty(value = "第一个", index = 1) + private String index1; + @ExcelProperty(value = "第10个", index = 10) + private String index10; + + public String getIndex1() { + return index1; + } + + public void setIndex1(String index1) { + this.index1 = index1; + } + + public String getIndex10() { + return index10; + } + + public void setIndex10(String index10) { + this.index10 = index10; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java new file mode 100644 index 00000000..6f9dad2c --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java @@ -0,0 +1,39 @@ +package com.alibaba.easyexcel.test.wirte.order; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.FileUtil; +import com.alibaba.excel.EasyExcelFactory; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.metadata.Sheet; + +/** + * Order data test + * + * @author zhuangjiaju + */ +public class OrderData07Test { + + @Test + public void simple() { + ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("order07.xlsx")) + .head(OrderData.class).build(); + Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); + writer.write(createData(10000 * 100), sheet); + writer.finish(); + } + + private List createData(int count) { + List list = new ArrayList(); + for (int i = 0; i < count; i++) { + OrderData orderData = new OrderData(); + orderData.setIndex1("排序1:" + i); + orderData.setIndex10("排序10:" + i); + list.add(orderData); + } + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java index 994721e8..80322349 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java @@ -19,13 +19,23 @@ public class SimpleData07Test { @Test public void simple() { - ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewFile("simple07.xlsx")) + ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("simple07.xlsx")) .head(SimpleData.class).build(); Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build(); writer.write(createData(10), sheet); writer.finish(); } + @Test + public void repeatWrite() { + ExcelWriter writer = EasyExcelFactory.writerBuilder() + .outputFile(FileUtil.createNewWriteFile("repeatWrite07.xlsx")).head(SimpleData.class).build(); + Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build(); + writer.write(createData(10), sheet); + writer.write(createData(10), sheet); + writer.finish(); + } + private List createData(int count) { List list = new ArrayList(); for (int i = 0; i < count; i++) {