diff --git a/src/main/java/com/wb/excel/api/datatable/WCell.java b/src/main/java/com/wb/excel/api/WCell.java similarity index 96% rename from src/main/java/com/wb/excel/api/datatable/WCell.java rename to src/main/java/com/wb/excel/api/WCell.java index 946cd46..db5b1ad 100644 --- a/src/main/java/com/wb/excel/api/datatable/WCell.java +++ b/src/main/java/com/wb/excel/api/WCell.java @@ -1,4 +1,4 @@ -package com.wb.excel.api.datatable; +package com.wb.excel.api; import com.wb.excel.api.enumeration.Status; diff --git a/src/main/java/com/wb/excel/api/datatable/WColumn.java b/src/main/java/com/wb/excel/api/WColumn.java similarity index 86% rename from src/main/java/com/wb/excel/api/datatable/WColumn.java rename to src/main/java/com/wb/excel/api/WColumn.java index e4b1424..427f66b 100644 --- a/src/main/java/com/wb/excel/api/datatable/WColumn.java +++ b/src/main/java/com/wb/excel/api/WColumn.java @@ -1,7 +1,7 @@ -package com.wb.excel.api.datatable; +package com.wb.excel.api; -import com.wb.excel.api.enumeration.DataType; +import com.wb.excel.api.converter.Converter; import com.wb.excel.api.util.StringUtil; import java.io.Serializable; @@ -34,9 +34,9 @@ public class WColumn implements Serializable { */ private String description; /** - * 该列的数据类型 + * 列转换器 */ - private DataType dataType; + private Converter converter; private Field field; @@ -46,7 +46,6 @@ public class WColumn implements Serializable { this.isHidden = false; this.isRequired = false; this.description = ""; - this.dataType = DataType.STRING; } public WColumn(String name) { @@ -55,7 +54,6 @@ public class WColumn implements Serializable { this.isHidden = false; this.isRequired = false; this.description = ""; - this.dataType = DataType.STRING; } //----------- getter & setter -------------- @@ -110,11 +108,11 @@ public class WColumn implements Serializable { this.description = description; } - public DataType getDataType() { - return dataType; + public Converter getConverter() { + return converter; } - public void setDataType(DataType dataType) { - this.dataType = dataType; + public void setConverter(Converter converter) { + this.converter = converter; } } diff --git a/src/main/java/com/wb/excel/api/WExcel.java b/src/main/java/com/wb/excel/api/WExcel.java deleted file mode 100644 index e86c56a..0000000 --- a/src/main/java/com/wb/excel/api/WExcel.java +++ /dev/null @@ -1,317 +0,0 @@ -package com.wb.excel.api; - -import com.wb.excel.api.annotation.ColumnDescription; -import com.wb.excel.api.datatable.WCell; -import com.wb.excel.api.datatable.WColumn; -import com.wb.excel.api.datatable.WSheet; -import com.wb.excel.api.enumeration.DataType; -import com.wb.excel.api.enumeration.Status; -import com.wb.excel.api.style.*; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.*; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashSet; -import java.util.Set; - -/** - * Excel帮助类. - *
- * Created on 2014/9/27. - * - * @author - * @since 0.1.0 - */ -public class WExcel { - /** - * Excel表格 - */ - private XSSFWorkbook workbook; - - /** - * 表头样式 - */ - private CellStyle headCellStyle; - - /** - * 错误单元格的样式 - */ - private CellStyle errorCellStyle; - - /** - * 错误的数字单元格的样式(数字靠右) - */ - private CellStyle errorNumberCellStyle; - - /** - * 普通单元格的样式 - */ - private CellStyle normalCellStyle; - - /** - * 普通数字单元格的样式 - */ - private CellStyle normalNumberCellStyle; - - /** - * 检查消息单元格的样式(最右边一列) - */ - private CellStyle checkMessageCellStyle; - - /** - * 检查结果失败的单元格的样式 - */ - private CellStyle checkFailureCellStyle; - - /** - * 检查结果通过的单元格的样式 - */ - private CellStyle checkSuccessCellStyle; - - /** - * 正常的文字 - */ - private Font normalFont; - - /** - * 红色文字 - */ - private Font redFont; - - /** - * 初始化一个Excel文件. - */ - public void init() { - workbook = new XSSFWorkbook(); - - //---------- 创建样式 ------------------ - headCellStyle = new HeadCellStyle(workbook).getStyle(); - errorCellStyle = new ErrorCellStyle(workbook).getStyle(); - errorNumberCellStyle = new ErrorNumberCellStyle(workbook).getStyle(); - normalCellStyle = new NormalCellStyle(workbook).getStyle(); - normalNumberCellStyle = new NormalNumberCellStyle(workbook).getStyle(); - checkMessageCellStyle = new CheckMessageCellStyle(workbook).getStyle(); - checkFailureCellStyle = new CheckFailureCellStyle(workbook).getStyle(); - checkSuccessCellStyle = new CheckSuccessCellStyle(workbook).getStyle(); - - //----------- 创建字体 ---------------- - normalFont = new NormalFont(workbook).getFont(); - redFont = new RedFont(workbook).getFont(); - } - - /** - * 默认无参构造方法. - */ - public WExcel() { - init(); - } - - /** - * 将一个DataTable转换为Excel对象,不显示检查结果和错误消息栏.
@@ -29,23 +27,24 @@ public class StringUtils {
*/
public static boolean isEmpty(String value) {
int strLen;
- if(value == null || (strLen = value.length()) == 0) {
+ if (value == null || (strLen = value.length()) == 0) {
return true;
}
- for(int i = 0; i < strLen; i++) {
- if((Character.isWhitespace(value.charAt(i)) == false)) {
+ for (int i = 0; i < strLen; i++) {
+ if ((Character.isWhitespace(value.charAt(i)) == false)) {
return false;
}
}
return true;
}
+
public static boolean isNotEmpty(String value) {
int strLen;
- if(value == null || (strLen = value.length()) == 0) {
+ if (value == null || (strLen = value.length()) == 0) {
return false;
}
- for(int i = 0; i < strLen; i++) {
- if((Character.isWhitespace(value.charAt(i)) == false)) {
+ for (int i = 0; i < strLen; i++) {
+ if ((Character.isWhitespace(value.charAt(i)) == false)) {
return true;
}
}
@@ -56,20 +55,20 @@ public class StringUtils {
* 检查对象是否为数字型字符串,包含负数开头的。
*/
public static boolean isNumeric(Object obj) {
- if(obj == null) {
+ if (obj == null) {
return false;
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
- if(length < 1)
+ if (length < 1)
return false;
int i = 0;
- if(length > 1 && chars[0] == '-')
+ if (length > 1 && chars[0] == '-')
i = 1;
- for(; i < length; i++) {
- if(!Character.isDigit(chars[i])) {
+ for (; i < length; i++) {
+ if (!Character.isDigit(chars[i])) {
return false;
}
}
@@ -81,10 +80,10 @@ public class StringUtils {
*/
public static boolean areNotEmpty(String... values) {
boolean result = true;
- if(values == null || values.length == 0) {
+ if (values == null || values.length == 0) {
result = false;
} else {
- for(String value : values) {
+ for (String value : values) {
result &= !isEmpty(value);
}
}
@@ -96,8 +95,8 @@ public class StringUtils {
*/
public static String unicodeToChinese(String unicode) {
StringBuilder out = new StringBuilder();
- if(!isEmpty(unicode)) {
- for(int i = 0; i < unicode.length(); i++) {
+ if (!isEmpty(unicode)) {
+ for (int i = 0; i < unicode.length(); i++) {
out.append(unicode.charAt(i));
}
}
@@ -106,10 +105,10 @@ public class StringUtils {
public static String toUnderlineStyle(String name) {
StringBuilder newName = new StringBuilder();
- for(int i = 0; i < name.length(); i++) {
+ for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
- if(Character.isUpperCase(c)) {
- if(i > 0) {
+ if (Character.isUpperCase(c)) {
+ if (i > 0) {
newName.append("_");
}
newName.append(Character.toLowerCase(c));
diff --git a/src/main/java/com/wb/excel/api/util/TransferUtil.java b/src/main/java/com/wb/excel/api/util/TransferUtil.java
index 672d1f5..f5b0ec0 100644
--- a/src/main/java/com/wb/excel/api/util/TransferUtil.java
+++ b/src/main/java/com/wb/excel/api/util/TransferUtil.java
@@ -1,8 +1,5 @@
package com.wb.excel.api.util;
-
-import com.wb.excel.api.enumeration.YesNo;
-
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -63,17 +60,6 @@ public class TransferUtil {
}
}
- public static Boolean transferBoolean(String value) {
- String key = EnumUtil.getKey(YesNo.class, value);
- if (key.equals("Y")) {
- return true;
- } else if (key.equals("N")) {
- return false;
- } else {
- return null;
- }
- }
-
public static Long transferLong(String value) {
try {
Double d = Double.parseDouble(value);
diff --git a/src/main/java/com/wb/excel/api/util/ValidationUtil.java b/src/main/java/com/wb/excel/api/util/ValidationUtil.java
index e54c0b7..a06ef20 100644
--- a/src/main/java/com/wb/excel/api/util/ValidationUtil.java
+++ b/src/main/java/com/wb/excel/api/util/ValidationUtil.java
@@ -1,16 +1,12 @@
package com.wb.excel.api.util;
-import javax.validation.ConstraintViolation;
-import javax.validation.Validation;
import javax.validation.Validator;
-import javax.validation.ValidatorFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
-import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
diff --git a/src/test/java/ExampleTest.java b/src/test/java/ExampleTest.java
index 213e43f..1b6a4be 100644
--- a/src/test/java/ExampleTest.java
+++ b/src/test/java/ExampleTest.java
@@ -1,8 +1,5 @@
-import com.wb.excel.api.WExcel;
-import com.wb.excel.api.datatable.WSheet;
-import com.wb.excel.api.enumeration.YesNo;
+import com.wb.excel.api.WSheet;
import com.wb.excel.api.exception.TemplateNotMatchException;
-import jdk.nashorn.internal.parser.JSONParser;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
@@ -16,10 +13,20 @@ import java.util.List;
public class ExampleTest {
public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
-// testExport();
+ testTemplate();
+ testExport();
testImport();
}
+ public static void testTemplate() {
+ try {
+ WSheet WSheet = new WSheet<>(User.class);
+ output("user_template.xlsx", WSheet.getBytes());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
public static void testExport() {
try {
@@ -27,21 +34,24 @@ public class ExampleTest {
List