Browse Source

加入测试案例 和部分转换

pull/507/head
zhuangjiaju 5 years ago
parent
commit
17e20fb99f
  1. 3
      src/main/java/com/alibaba/excel/annotation/ExcelProperty.java
  2. 11
      src/main/java/com/alibaba/excel/annotation/FieldType.java
  3. 32
      src/main/java/com/alibaba/excel/converters/Converter.java
  4. 35
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java
  5. 44
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java
  6. 35
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java
  7. 43
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java
  8. 35
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java
  9. 35
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java
  10. 43
      src/main/java/com/alibaba/excel/converters/doubleconverter/ByteBooleanConverter.java
  11. 35
      src/main/java/com/alibaba/excel/converters/doubleconverter/ByteNumberConverter.java
  12. 34
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java
  13. 6
      src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java
  14. 18
      src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java
  15. 73
      src/main/java/com/alibaba/excel/util/IndexValueConverter.java
  16. 10
      src/main/java/com/alibaba/excel/util/RowUtil.java
  17. 9
      src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java
  18. 29
      src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java
  19. 39
      src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java
  20. 12
      src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java

3
src/main/java/com/alibaba/excel/annotation/ExcelProperty.java

@ -18,13 +18,10 @@ public @interface ExcelProperty {
* @return * @return
*/ */
String[] value() default {""}; String[] value() default {""};
/** /**
* @return * @return
*/ */
int index() default 99999; int index() default 99999;
/** /**
* *
* default @see com.alibaba.excel.util.TypeUtil * default @see com.alibaba.excel.util.TypeUtil

11
src/main/java/com/alibaba/excel/annotation/FieldType.java

@ -1,11 +0,0 @@
package com.alibaba.excel.annotation;
/**
*
* @author jipengfei
*/
public enum FieldType {
STRING, INT, LONG, DATE, BOOLEAN, DOUBLE,EMPTY;
}

32
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.NotNull;
import com.sun.istack.internal.Nullable; import com.sun.istack.internal.Nullable;
/**
* Convert between Java objects and excel objects
*
* @author Dan Zheng
* @param <T>
*/
public interface Converter<T> { public interface Converter<T> {
/**
* Back to object types in Java
*
* @return
*/
Class supportJavaTypeKey(); Class supportJavaTypeKey();
/**
* Back to object enum in excel
*
* @return
*/
CellDataTypeEnum supportExcelTypeKey(); 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; 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; CellData convertToExcelData(@NotNull T value, @Nullable ExcelColumnProperty columnProperty) throws Exception;
} }

35
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<Boolean> {
@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);
}
}

44
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<Boolean> {
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);
}
}

35
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<Boolean> {
@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());
}
}

43
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<Byte> {
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);
}
}

35
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<Byte> {
@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);
}
}

35
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<Byte> {
@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());
}
}

43
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<Byte> {
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);
}
}

35
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<Byte> {
@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);
}
}

34
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<Double> {
@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());
}
}

6
src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java

@ -11,10 +11,15 @@ import com.alibaba.excel.util.StringUtils;
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public enum CellDataTypeEnum { public enum CellDataTypeEnum {
/** /**
* string * string
*/ */
STRING, STRING,
/**
* inlineString
*/
INLINE_STRING,
/** /**
* number * number
*/ */
@ -35,6 +40,7 @@ public enum CellDataTypeEnum {
private static final Map<String, CellDataTypeEnum> TYPE_ROUTING_MAP = new HashMap<String, CellDataTypeEnum>(8); private static final Map<String, CellDataTypeEnum> TYPE_ROUTING_MAP = new HashMap<String, CellDataTypeEnum>(8);
static { static {
TYPE_ROUTING_MAP.put("s", STRING); TYPE_ROUTING_MAP.put("s", STRING);
TYPE_ROUTING_MAP.put("inlineStr", INLINE_STRING);
TYPE_ROUTING_MAP.put("e", ERROR); TYPE_ROUTING_MAP.put("e", ERROR);
TYPE_ROUTING_MAP.put("b", BOOLEAN); TYPE_ROUTING_MAP.put("b", BOOLEAN);
} }

18
src/main/java/com/alibaba/excel/metadata/ExcelColumnProperty.java

@ -30,6 +30,24 @@ public class ExcelColumnProperty implements Comparable<ExcelColumnProperty> {
private Boolean use1904windowing; private Boolean use1904windowing;
private TimeZone timeZone; 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() { public Boolean getUse1904windowing() {
return use1904windowing; return use1904windowing;

73
src/main/java/com/alibaba/excel/util/IndexValueConverter.java

@ -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<String> converter(List<IndexValue> i_list) {
List<String> tem = new ArrayList<String>();
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<Character> currentStack = new Stack<Character>();
Stack<Character> berforStack = new Stack<Character>();
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;
}
}

10
src/main/java/com/alibaba/excel/util/RowUtil.java

@ -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){
//
//}
}

9
src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java

@ -17,7 +17,16 @@ public class FileUtil {
File file = new File(getPath() + pathName); File file = new File(getPath() + pathName);
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
} }
}
return file; return file;
} }
public static File createNewWriteFile(String pathName) {
return createNewFile("wirte/" + pathName);
}
} }

29
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;
}
}

39
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<OrderData> createData(int count) {
List<OrderData> list = new ArrayList<OrderData>();
for (int i = 0; i < count; i++) {
OrderData orderData = new OrderData();
orderData.setIndex1("排序1:" + i);
orderData.setIndex10("排序10:" + i);
list.add(orderData);
}
return list;
}
}

12
src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java

@ -19,13 +19,23 @@ public class SimpleData07Test {
@Test @Test
public void simple() { 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(); .head(SimpleData.class).build();
Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build(); Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build();
writer.write(createData(10), sheet); writer.write(createData(10), sheet);
writer.finish(); 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<SimpleData> createData(int count) { private List<SimpleData> createData(int count) {
List<SimpleData> list = new ArrayList<SimpleData>(); List<SimpleData> list = new ArrayList<SimpleData>();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {

Loading…
Cancel
Save