diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..bf0f001d0cf12ab894dc25d6ae78944cdc34bf88 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# Created by .ignore support plugin (hsz.mobi) +build +logs +bin +.DS_Store +*.log +*.gz +.gradle +.idea +out +*.iml + diff --git a/pom.xml b/pom.xml index 092f24a80b35935b7c805b1642f3b90b437a2686..fb018959e4e094e7c902f4463771c5881e9f8005 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.github.crab2died Excel4J - 2.1.4-Final2 + 2.1.5-Final jar Excel4J @@ -45,6 +45,13 @@ + + org.projectlombok + lombok + 1.14.8 + compile + + junit junit diff --git a/src/main/java/com/github/crab2died/ExcelUtils.java b/src/main/java/com/github/crab2died/ExcelUtils.java index b37329a84c83c457506556c5b50368e6089ee0ee..0d2351abfa2d1948fe56b39ec1b5ae9ca9e6843c 100644 --- a/src/main/java/com/github/crab2died/ExcelUtils.java +++ b/src/main/java/com/github/crab2died/ExcelUtils.java @@ -26,6 +26,7 @@ package com.github.crab2died; +import com.github.crab2died.constant.LanguageEnum; import com.github.crab2died.converter.DefaultConvertible; import com.github.crab2died.exceptions.Excel4JException; import com.github.crab2died.exceptions.Excel4jReadException; @@ -119,12 +120,21 @@ public final class ExcelUtils { * @throws InvalidFormatException 异常 * @author Crab2Died */ + public List readExcel2Objects(String excelPath, Class clazz, int offsetLine, + int limitLine, int sheetIndex, String language) + throws Excel4JException, IOException, InvalidFormatException { + + try (Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(excelPath)))) { + return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex, language); + } + } + public List readExcel2Objects(String excelPath, Class clazz, int offsetLine, int limitLine, int sheetIndex) throws Excel4JException, IOException, InvalidFormatException { try (Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(excelPath)))) { - return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex); + return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex, null); } } @@ -143,12 +153,21 @@ public final class ExcelUtils { * @throws InvalidFormatException 异常 * @author Crab2Died */ + public List readExcel2Objects(InputStream is, Class clazz, int offsetLine, + int limitLine, int sheetIndex, String language) + throws Excel4JException, IOException, InvalidFormatException { + + try (Workbook workbook = WorkbookFactory.create(is)) { + return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex, language); + } + } + public List readExcel2Objects(InputStream is, Class clazz, int offsetLine, int limitLine, int sheetIndex) throws Excel4JException, IOException, InvalidFormatException { try (Workbook workbook = WorkbookFactory.create(is)) { - return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex); + return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex, null); } } @@ -242,24 +261,26 @@ public final class ExcelUtils { } private List readExcel2ObjectsHandler(Workbook workbook, Class clazz, int offsetLine, - int limitLine, int sheetIndex) + int limitLine, int sheetIndex, String language) throws Excel4JException { Sheet sheet = workbook.getSheetAt(sheetIndex); Row row = sheet.getRow(offsetLine); List list = new ArrayList<>(); Map maps = Utils.getHeaderMap(row, clazz); - if (maps == null || maps.size() <= 0) + if (maps == null || maps.size() <= 0) { throw new Excel4jReadException( "The Excel format to read is not correct, and check to see if the appropriate rows are set" ); + } long maxLine = sheet.getLastRowNum() > ((long) offsetLine + limitLine) ? ((long) offsetLine + limitLine) : sheet.getLastRowNum(); for (int i = offsetLine + 1; i <= maxLine; i++) { row = sheet.getRow(i); - if (null == row) + if (null == row) { continue; + } T obj; try { obj = clazz.newInstance(); @@ -269,15 +290,16 @@ public final class ExcelUtils { for (Cell cell : row) { int ci = cell.getColumnIndex(); ExcelHeader header = maps.get(ci); - if (null == header) + if (null == header) { continue; + } String val = Utils.getCellValue(cell); Object value; String filed = header.getFiled(); // 读取转换器 if (null != header.getReadConverter() && header.getReadConverter().getClass() != DefaultConvertible.class) { - value = header.getReadConverter().execRead(val); + value = header.getReadConverter().execRead(val, language); } else { // 默认转换 value = Utils.str2TargetClass(val, header.getFiledClazz()); @@ -420,8 +442,9 @@ public final class ExcelUtils { for (int i = offsetLine; i <= maxLine; i++) { List rows = new ArrayList<>(); Row row = sheet.getRow(i); - if (null == row) + if (null == row) { continue; + } for (Cell cell : row) { String val = Utils.getCellValue(cell); rows.add(val); @@ -990,8 +1013,15 @@ public final class ExcelUtils { String sheetName, boolean isXSSF, String targetPath) throws Excel4JException, IOException { + exportObjects2Excel(data, clazz, isWriteHeader, sheetName, isXSSF, targetPath, LanguageEnum.CHINESE.getValue()); + } + + public void exportObjects2Excel(List data, Class clazz, boolean isWriteHeader, + String sheetName, boolean isXSSF, String targetPath, String language) + throws Excel4JException, IOException { + try (FileOutputStream fos = new FileOutputStream(targetPath); - Workbook workbook = exportExcelNoTemplateHandler(data, clazz, isWriteHeader, sheetName, isXSSF)) { + Workbook workbook = exportExcelNoTemplateHandler(data, clazz, isWriteHeader, sheetName, isXSSF, language)) { workbook.write(fos); } } @@ -1094,11 +1124,40 @@ public final class ExcelUtils { } } - // 单sheet数据导出 + /** + * 单shell 数据导出 + * + * @param data + * @param clazz + * @param isWriteHeader + * @param sheetName + * @param isXSSF + * @return + * @throws Excel4JException + */ private Workbook exportExcelNoTemplateHandler(List data, Class clazz, boolean isWriteHeader, String sheetName, boolean isXSSF) throws Excel4JException { + return exportExcelNoTemplateHandler(data, clazz, isWriteHeader, sheetName, isXSSF, LanguageEnum.CHINESE.getValue()); + } + + /** + * 单shell 数据导出 + * + * @param data + * @param clazz + * @param isWriteHeader + * @param sheetName + * @param isXSSF + * @param language 语言 + * @return + * @throws Excel4JException + */ + private Workbook exportExcelNoTemplateHandler(List data, Class clazz, boolean isWriteHeader, + String sheetName, boolean isXSSF, String language) + throws Excel4JException { + Workbook workbook; if (isXSSF) { workbook = new XSSFWorkbook(); @@ -1106,7 +1165,7 @@ public final class ExcelUtils { workbook = new HSSFWorkbook(); } - generateSheet(workbook, data, clazz, isWriteHeader, sheetName); + generateSheet(workbook, data, clazz, isWriteHeader, sheetName, language); return workbook; } @@ -1201,9 +1260,14 @@ public final class ExcelUtils { return workbook; } + private void generateSheet(Workbook workbook, List data, Class clazz, + boolean isWriteHeader, String sheetName) throws Excel4JException { + generateSheet(workbook, data, clazz, isWriteHeader, sheetName, null); + } + // 生成sheet数据 private void generateSheet(Workbook workbook, List data, Class clazz, - boolean isWriteHeader, String sheetName) + boolean isWriteHeader, String sheetName, String language) throws Excel4JException { Sheet sheet; @@ -1213,7 +1277,7 @@ public final class ExcelUtils { sheet = workbook.createSheet(); } Row row = sheet.createRow(0); - List headers = Utils.getHeaderList(clazz); + List headers = Utils.getHeaderList(clazz, language); if (isWriteHeader) { // 写标题 for (int i = 0; i < headers.size(); i++) { @@ -1221,14 +1285,13 @@ public final class ExcelUtils { } } // 写数据 - Object _data; + Object obj; for (int i = 0; i < data.size(); i++) { row = sheet.createRow(i + 1); - _data = data.get(i); + obj = data.get(i); for (int j = 0; j < headers.size(); j++) { - row.createCell(j).setCellValue(Utils.getProperty(_data, - headers.get(j).getFiled(), - headers.get(j).getWriteConverter())); + row.createCell(j).setCellValue(Utils.getProperty(obj, headers.get(j).getFiled(), + headers.get(j).getWriteConverter(), language)); } } diff --git a/src/main/java/com/github/crab2died/annotation/ExcelField.java b/src/main/java/com/github/crab2died/annotation/ExcelField.java index fe04fb91019f8558c5665ccc2d31cceeac2ed8f9..8ac5ebb59d49142409bf20838976621f1a2597b4 100644 --- a/src/main/java/com/github/crab2died/annotation/ExcelField.java +++ b/src/main/java/com/github/crab2died/annotation/ExcelField.java @@ -30,10 +30,7 @@ import com.github.crab2died.converter.DefaultConvertible; import com.github.crab2died.converter.ReadConvertible; import com.github.crab2died.converter.WriteConvertible; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import java.lang.annotation.*; /** * 功能说明: 用来在对象的属性上加入的annotation,通过该annotation说明某个属性所对应的标题 @@ -41,7 +38,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface ExcelField { - /** * 属性的标题名称 * diff --git a/src/main/java/com/github/crab2died/annotation/I18nField.java b/src/main/java/com/github/crab2died/annotation/I18nField.java new file mode 100644 index 0000000000000000000000000000000000000000..779df98410eef3908bb8f50b71ef867e0cc6ae98 --- /dev/null +++ b/src/main/java/com/github/crab2died/annotation/I18nField.java @@ -0,0 +1,19 @@ +package com.github.crab2died.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface I18nField { + + /** + * 国际化标题栏(例如: ["zh-cn|学生","en-us|student"]) + * + * @return + */ + String[] titles(); + +} diff --git a/src/main/java/com/github/crab2died/constant/LanguageEnum.java b/src/main/java/com/github/crab2died/constant/LanguageEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..2122c91df3bde513cd5a388ab61577b140d31fc0 --- /dev/null +++ b/src/main/java/com/github/crab2died/constant/LanguageEnum.java @@ -0,0 +1,33 @@ +package com.github.crab2died.constant; + +import lombok.Getter; +import lombok.Setter; + +/** + * @author junmingyang + */ +public enum LanguageEnum { + + /** + * 中文 + */ + CHINESE("中文", "zh-cn"), + + /** + * 英语 + */ + ENGLISH("英语", "en-us"); + + LanguageEnum(String name, String value) { + this.name = name; + this.value = value; + } + + @Setter + @Getter + private String name; + + @Setter + @Getter + private String value; +} diff --git a/src/main/java/com/github/crab2died/converter/DefaultConvertible.java b/src/main/java/com/github/crab2died/converter/DefaultConvertible.java index acfbef87cc9a8eca60a3de9616adf8918327548e..ee37dbb81efcc8c45b4cf2fa3f25282cdee89635 100644 --- a/src/main/java/com/github/crab2died/converter/DefaultConvertible.java +++ b/src/main/java/com/github/crab2died/converter/DefaultConvertible.java @@ -28,6 +28,8 @@ package com.github.crab2died.converter; /** * 抽象默认转换器, 实现了{@link WriteConvertible} 与 {@link ReadConvertible}接口 + * + * @author junmingyang */ public class DefaultConvertible implements WriteConvertible, ReadConvertible { @@ -36,8 +38,18 @@ public class DefaultConvertible implements WriteConvertible, ReadConvertible { throw new UnsupportedOperationException(); } + @Override + public Object execRead(String object, String language) { + throw new UnsupportedOperationException(); + } + @Override public Object execWrite(Object object) { throw new UnsupportedOperationException(); } + + @Override + public Object execWrite(Object object, String language) { + throw new UnsupportedOperationException(); + } } diff --git a/src/main/java/com/github/crab2died/converter/ReadConvertible.java b/src/main/java/com/github/crab2died/converter/ReadConvertible.java index bb174a4874fbdb1b34a1b4240ae3a3d511882037..1c437e5d5e363b9a5f94bef3c3f79f58a4fd46d6 100644 --- a/src/main/java/com/github/crab2died/converter/ReadConvertible.java +++ b/src/main/java/com/github/crab2died/converter/ReadConvertible.java @@ -27,6 +27,8 @@ package com.github.crab2died.converter; /** * 写入excel内容转换器 + * + * @author junmingyang */ public interface ReadConvertible { @@ -38,4 +40,8 @@ public interface ReadConvertible { * @see com.github.crab2died.annotation.ExcelField#readConverter() */ Object execRead(String object); + + Object execRead(String object, String language); + + } diff --git a/src/main/java/com/github/crab2died/converter/WriteConvertible.java b/src/main/java/com/github/crab2died/converter/WriteConvertible.java index cfbeb5635de6e9aa3af749f9c114b68cff7f3120..0bb6909938cfac7978b1193bee48c70c83a8e311 100644 --- a/src/main/java/com/github/crab2died/converter/WriteConvertible.java +++ b/src/main/java/com/github/crab2died/converter/WriteConvertible.java @@ -28,6 +28,8 @@ package com.github.crab2died.converter; /** * 写入excel内容转换器 + * + * @author junmingyang */ public interface WriteConvertible { @@ -39,4 +41,7 @@ public interface WriteConvertible { * @see com.github.crab2died.annotation.ExcelField#writeConverter() */ Object execWrite(Object object); + + Object execWrite(Object object, String language); + } diff --git a/src/main/java/com/github/crab2died/handler/ExcelHeader.java b/src/main/java/com/github/crab2died/handler/ExcelHeader.java index 1684782c605f4291e692a708f8c50acc2706d0de..eb62a135aec29553bbca17bf72dcf3d62c13eec1 100644 --- a/src/main/java/com/github/crab2died/handler/ExcelHeader.java +++ b/src/main/java/com/github/crab2died/handler/ExcelHeader.java @@ -28,10 +28,13 @@ package com.github.crab2died.handler; import com.github.crab2died.converter.ReadConvertible; import com.github.crab2died.converter.WriteConvertible; +import lombok.Data; /** * 功能说明: 用来存储Excel标题的对象,通过该对象可以获取标题和方法的对应关系 + * @author junmingyang */ +@Data public class ExcelHeader implements Comparable { /** @@ -64,53 +67,6 @@ public class ExcelHeader implements Comparable { */ private Class filedClazz; - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public int getOrder() { - return order; - } - - public void setOrder(int order) { - this.order = order; - } - - public WriteConvertible getWriteConverter() { - return writeConverter; - } - - public void setWriteConverter(WriteConvertible writeConverter) { - this.writeConverter = writeConverter; - } - - public ReadConvertible getReadConverter() { - return readConverter; - } - - public void setReadConverter(ReadConvertible readConverter) { - this.readConverter = readConverter; - } - - public String getFiled() { - return filed; - } - - public void setFiled(String filed) { - this.filed = filed; - } - - public Class getFiledClazz() { - return filedClazz; - } - - public void setFiledClazz(Class filedClazz) { - this.filedClazz = filedClazz; - } @Override public int compareTo(ExcelHeader o) { diff --git a/src/main/java/com/github/crab2died/handler/SheetTemplateHandler.java b/src/main/java/com/github/crab2died/handler/SheetTemplateHandler.java index 90df06fbf9177f4ca3d2d0cf769a1d07b27eb35c..93c69964758641fda1f1665ad711372467f49e77 100644 --- a/src/main/java/com/github/crab2died/handler/SheetTemplateHandler.java +++ b/src/main/java/com/github/crab2died/handler/SheetTemplateHandler.java @@ -80,7 +80,9 @@ public class SheetTemplateHandler { public static void loadTemplate(SheetTemplate template, int sheetIndex) { - if (sheetIndex < 0) sheetIndex = 0; + if (sheetIndex < 0) { + sheetIndex = 0; + } template.sheetIndex = sheetIndex; template.sheet = template.workbook.getSheetAt(sheetIndex); initModuleConfig(template); @@ -96,8 +98,9 @@ public class SheetTemplateHandler { for (Row row : template.sheet) { for (Cell c : row) { - if (c.getCellTypeEnum() != CellType.STRING) + if (c.getCellTypeEnum() != CellType.STRING) { continue; + } String str = c.getStringCellValue().trim().toLowerCase(); // 寻找序号列 if (HandlerConstant.SERIAL_NUMBER.equals(str)) { @@ -119,10 +122,12 @@ public class SheetTemplateHandler { * 初始化样式信息 */ private static void initStyles(SheetTemplate template, Cell cell, String moduleContext) { - if (null == moduleContext || "".equals(moduleContext)) + if (null == moduleContext || "".equals(moduleContext)) { return; - if (!moduleContext.startsWith("&")) + } + if (!moduleContext.startsWith("&")) { moduleContext = moduleContext.toLowerCase(); + } if (HandlerConstant.DEFAULT_STYLE.equals(moduleContext)) { template.defaultStyle = cell.getCellStyle(); clearCell(cell); @@ -160,12 +165,14 @@ public class SheetTemplateHandler { * @param data 替换映射 */ public static void extendData(SheetTemplate template, Map data) { - if (data == null) + if (data == null) { return; + } for (Row row : template.sheet) { for (Cell c : row) { - if (c.getCellTypeEnum() != CellType.STRING) + if (c.getCellTypeEnum() != CellType.STRING) { continue; + } String str = c.getStringCellValue().trim(); if (str.startsWith("#") && data.containsKey(str.substring(1))) { c.setCellValue(data.get(str.substring(1))); @@ -194,8 +201,9 @@ public class SheetTemplateHandler { * @param styleKey 样式标识 */ public static void insertSerial(SheetTemplate template, String styleKey) { - if (template.serialNumberColumnIndex < 0) + if (template.serialNumberColumnIndex < 0) { return; + } template.serialNumber++; Cell c = template.currentRow.createCell(template.serialNumberColumnIndex); setCellStyle(template, c, styleKey); @@ -294,8 +302,9 @@ public class SheetTemplateHandler { cell.setCellStyle(template.doubleLineStyle); return; } - if (null != template.defaultStyle) + if (null != template.defaultStyle) { cell.setCellStyle(template.defaultStyle); + } } /*-----------------------------------数据填充结束-----------------------------------*/ diff --git a/src/main/java/com/github/crab2died/utils/DateUtils.java b/src/main/java/com/github/crab2died/utils/DateUtils.java index 144eaac34ab39f1849a9d38a01bd41516a160bbe..3d901f5ae055a05032dadedef7f25da6ce3145ef 100644 --- a/src/main/java/com/github/crab2died/utils/DateUtils.java +++ b/src/main/java/com/github/crab2died/utils/DateUtils.java @@ -40,6 +40,7 @@ public class DateUtils { public static final String DATE_FORMAT_DAY = "yyyy-MM-dd"; public static final String DATE_FORMAT_DAY_2 = "yyyy/MM/dd"; + public static final String DATE_FORMAT_DAY_EN = "MM/dd/yyyy"; public static final String TIME_FORMAT_SEC = "HH:mm:ss"; public static final String DATE_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_FORMAT_MSEC = "yyyy-MM-dd HH:mm:ss.SSS"; diff --git a/src/main/java/com/github/crab2died/utils/RegularUtils.java b/src/main/java/com/github/crab2died/utils/RegularUtils.java index 74816eff87ff96cb98b356ef5e3c1463ed027e7d..206e52c42ffb5254e9c9b8a83e679d25437ebb0a 100644 --- a/src/main/java/com/github/crab2died/utils/RegularUtils.java +++ b/src/main/java/com/github/crab2died/utils/RegularUtils.java @@ -72,8 +72,9 @@ public class RegularUtils { List matchGroups = new ArrayList<>(); Pattern compile = Pattern.compile(reg); Matcher matcher = compile.matcher(pattern); - if (group > matcher.groupCount() || group < 0) + if (group > matcher.groupCount() || group < 0) { throw new IllegalGroupIndexException("Illegal match group :" + group); + } while (matcher.find()) { matchGroups.add(matcher.group(group)); } @@ -91,7 +92,7 @@ public class RegularUtils { * @param reg 正则表达式 * @return 提取内容集合 */ - public static String match(String pattern, String reg) { + public static String match(String pattern, String reg) { String match = null; List matches = match(pattern, reg, 0); @@ -101,9 +102,10 @@ public class RegularUtils { return match; } - public static String converNumByReg(String number) { - Pattern compile = Pattern.compile("^(\\d+)(\\.0*)?$"); - Matcher matcher = compile.matcher(number); + private static final Pattern PATTERN_NUMBER = Pattern.compile("^(\\d+)(\\.0*)?$"); + + public static String convertNumByReg(String number) { + Matcher matcher = PATTERN_NUMBER.matcher(number); while (matcher.find()) { number = matcher.group(1); } diff --git a/src/main/java/com/github/crab2died/utils/Utils.java b/src/main/java/com/github/crab2died/utils/Utils.java index 7a453812f2320fadc241ea5c9ca9520e319a0d83..866805b2cba7ca89796b47e10f761d434e57cc40 100644 --- a/src/main/java/com/github/crab2died/utils/Utils.java +++ b/src/main/java/com/github/crab2died/utils/Utils.java @@ -27,6 +27,7 @@ package com.github.crab2died.utils; import com.github.crab2died.annotation.ExcelField; +import com.github.crab2died.annotation.I18nField; import com.github.crab2died.converter.DefaultConvertible; import com.github.crab2died.converter.WriteConvertible; import com.github.crab2died.exceptions.Excel4JException; @@ -44,7 +45,6 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; -import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Pattern; @@ -59,17 +59,29 @@ public class Utils { * getter或setter枚举 */ public enum FieldAccessType { + /** + * read method + */ + GETTER, + /** + * write method + */ + SETTER + } - GETTER, SETTER + public static List getHeaderList(Class clz) throws Excel4JException { + return getHeaderList(clz, null); } /** - *

根据JAVA对象注解获取Excel表头信息

+ * 根据注解获取表头信息 * - * @param clz 类型 - * @return 表头信息 + * @param clz 类型 + * @param language 语言,eg: zh-cn + * @return + * @throws Excel4JException */ - public static List getHeaderList(Class clz) throws Excel4JException { + public static List getHeaderList(Class clz, String language) throws Excel4JException { List headers = new ArrayList<>(); List fields = new ArrayList<>(); @@ -81,14 +93,26 @@ public class Utils { if (field.isAnnotationPresent(ExcelField.class)) { ExcelField er = field.getAnnotation(ExcelField.class); try { - headers.add(new ExcelHeader( + ExcelHeader header = new ExcelHeader( er.title(), er.order(), er.writeConverter().newInstance(), er.readConverter().newInstance(), field.getName(), field.getType() - )); + ); + //是否使用国际化 + if (language != null && language.length() > 0 && field.isAnnotationPresent(I18nField.class)) { + I18nField i18n = field.getAnnotation(I18nField.class); + String[] titles = i18n.titles(); + for (String title : titles) { + String[] arr = title.split("\\|"); + if (arr[0].equalsIgnoreCase(language)) { + header.setTitle(arr[1]); + } + } + } + headers.add(header); } catch (InstantiationException | IllegalAccessException e) { throw new Excel4JException(e); } @@ -147,7 +171,7 @@ public class Utils { } else { o = String.valueOf(c.getNumericCellValue()); o = matchDoneBigDecimal(o); - o = RegularUtils.converNumByReg(o); + o = RegularUtils.convertNumByReg(o); } break; case STRING: @@ -168,16 +192,17 @@ public class Utils { * @return 转换后数据 */ public static Object str2TargetClass(String strField, Class clazz) { - if (null == strField || "".equals(strField)) + if (null == strField || "".equals(strField)) { return null; + } if ((Long.class == clazz) || (long.class == clazz)) { strField = matchDoneBigDecimal(strField); - strField = RegularUtils.converNumByReg(strField); + strField = RegularUtils.convertNumByReg(strField); return Long.parseLong(strField); } if ((Integer.class == clazz) || (int.class == clazz)) { strField = matchDoneBigDecimal(strField); - strField = RegularUtils.converNumByReg(strField); + strField = RegularUtils.convertNumByReg(strField); return Integer.parseInt(strField); } if ((Float.class == clazz) || (float.class == clazz)) { @@ -230,8 +255,9 @@ public class Utils { public static Method getterOrSetter(Class clazz, String fieldName, FieldAccessType methodType) throws IntrospectionException { - if (null == fieldName || "".equals(fieldName)) + if (null == fieldName || "".equals(fieldName)) { return null; + } BeanInfo beanInfo = Introspector.getBeanInfo(clazz); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); @@ -281,9 +307,16 @@ public class Utils { */ public static String getProperty(Object bean, String fieldName, WriteConvertible writeConvertible) throws Excel4JException { + return getProperty(bean, fieldName, writeConvertible, null); + } - if (bean == null || fieldName == null) + + public static String getProperty(Object bean, String fieldName, WriteConvertible writeConvertible, String language) + throws Excel4JException { + + if (bean == null || fieldName == null) { throw new IllegalArgumentException("Operating bean or filed class must not be null"); + } Method method; Object object; try { @@ -294,7 +327,7 @@ public class Utils { } if (null != writeConvertible && writeConvertible.getClass() != DefaultConvertible.class) { // 写入转换器 - object = writeConvertible.execWrite(object); + object = writeConvertible.execWrite(object, language); } return object == null ? "" : object.toString(); } @@ -309,11 +342,13 @@ public class Utils { public static void copyProperty(Object bean, String name, Object value) throws Excel4JException { - if (null == name || null == value) + if (null == name || null == value) { return; + } Field field = matchClassField(bean.getClass(), name); - if (null == field) + if (null == field) { return; + } Method method; try { method = getterOrSetter(bean.getClass(), name, FieldAccessType.SETTER); diff --git a/src/test/java/base/Excel2Module.java b/src/test/java/base/Excel2Module.java index 3195f75a5fb7fcd17138b081a7e789b27063c1d3..502419d713a2379ab8b5a2f5b072fb7948d717c9 100644 --- a/src/test/java/base/Excel2Module.java +++ b/src/test/java/base/Excel2Module.java @@ -45,7 +45,7 @@ public class Excel2Module { // 2) // 基于注解,将Excel内容读至List对象内 // 验证读取转换函数Student2ExpelConverter - // 注解 `@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)` + // 注解 `@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelRealConverter.class)` List students = ExcelUtils.getInstance().readExcel2Objects(path, Student2.class, 0, 0); System.out.println("读取Excel至对象数组(支持类型转换):"); for (Student2 st : students) { @@ -57,7 +57,7 @@ public class Excel2Module { } // 基于注解,将Excel内容读至List对象内 - // 验证读取转换函数Student2ExpelConverter,注解 `@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)` + // 验证读取转换函数Student2ExpelConverter,注解 `@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelRealConverter.class)` @Test public void testReadConverter() throws Exception { diff --git a/src/test/java/base/Module2Excel.java b/src/test/java/base/Module2Excel.java index 6a6b4d009726196633671cf022024e564deed82a..0bce564a1e5cd59585f904cfb18bfe3f816b2e17 100644 --- a/src/test/java/base/Module2Excel.java +++ b/src/test/java/base/Module2Excel.java @@ -17,6 +17,8 @@ import java.util.*; public class Module2Excel { + private static final String BASE_PATH = "D:\\temp\\"; + @Test public void testObject2Excel() throws Exception { @@ -162,10 +164,12 @@ public class Module2Excel { @Test public void uuid() throws IOException { List list = new ArrayList<>(); - for (int i =0; i < 10000; i ++){ + for (int i = 0; i < 10000; i++) { list.add(UUID.randomUUID().toString()); } - ExcelUtils.getInstance().exportObjects2Excel(list, new ArrayList(){{add("uuid");}}, "J.xlsx"); + ExcelUtils.getInstance().exportObjects2Excel(list, new ArrayList() {{ + add("uuid"); + }}, "J.xlsx"); } // 验证日期转换函数 Student2DateConverter @@ -175,9 +179,11 @@ public class Module2Excel { List list = new ArrayList<>(); for (int i = 0; i < 1000; i++) { - list.add(new Student2(10000L + i, "学生" + i, new Date(), 201, false)); + list.add(new Student2(10000L + i, "学生" + i, new Date(), 201, i % 2 == 0)); } - ExcelUtils.getInstance().exportObjects2Excel(list, Student2.class, true, "sheet0", true, "E.xlsx"); + ExcelUtils.getInstance().exportObjects2Excel(list, Student2.class, true, "student", true, BASE_PATH + "student2-en.xlsx", "en-us"); + ExcelUtils.getInstance().exportObjects2Excel(list, Student2.class, true, "学生信息", true, BASE_PATH + "student2-cn.xlsx", "zh-cn"); + ExcelUtils.getInstance().exportObjects2Excel(list, Student2.class, true, "学生信息", true, BASE_PATH + "student2-default.xlsx"); } // 多sheet无模板、基于注解的导出 diff --git a/src/test/java/converter/Student2DateConverter.java b/src/test/java/converter/Student2DateConverter.java index 7211adb4dc6ca5a72b45c52d0710cc4a585c5541..bc35365aab6b66dae174e60c508e887f9303bf80 100644 --- a/src/test/java/converter/Student2DateConverter.java +++ b/src/test/java/converter/Student2DateConverter.java @@ -1,5 +1,6 @@ package converter; +import com.github.crab2died.constant.LanguageEnum; import com.github.crab2died.converter.WriteConvertible; import com.github.crab2died.utils.DateUtils; @@ -10,11 +11,18 @@ import java.util.Date; */ public class Student2DateConverter implements WriteConvertible { - @Override public Object execWrite(Object object) { + return execWrite(object, null); + } + @Override + public Object execWrite(Object object, String language) { Date date = (Date) object; - return DateUtils.date2Str(date, DateUtils.DATE_FORMAT_MSEC_T_Z); + if (!LanguageEnum.CHINESE.getValue().equalsIgnoreCase(language)) { + return DateUtils.date2Str(date, DateUtils.DATE_FORMAT_DAY_EN); + } else { + return DateUtils.date2Str(date, DateUtils.DATE_FORMAT_DAY); + } } } diff --git a/src/test/java/converter/Student2ExpelConverter.java b/src/test/java/converter/Student2ExpelConverter.java deleted file mode 100644 index c80ce548b00a77099d64567b6496d76ccdf0039e..0000000000000000000000000000000000000000 --- a/src/test/java/converter/Student2ExpelConverter.java +++ /dev/null @@ -1,15 +0,0 @@ -package converter; - -import com.github.crab2died.converter.ReadConvertible; - -/** - * excel是否开除 列数据转换器 - */ -public class Student2ExpelConverter implements ReadConvertible{ - - @Override - public Object execRead(String object) { - - return object.equals("是"); - } -} diff --git a/src/test/java/converter/Student2ExpelRealConverter.java b/src/test/java/converter/Student2ExpelRealConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..6bc5c37990f6623f77ae27331f68a342b3280a8c --- /dev/null +++ b/src/test/java/converter/Student2ExpelRealConverter.java @@ -0,0 +1,23 @@ +package converter; + +import com.github.crab2died.constant.LanguageEnum; +import com.github.crab2died.converter.ReadConvertible; + +/** + * excel是否开除 列数据转换器 + */ +public class Student2ExpelRealConverter implements ReadConvertible { + + @Override + public Object execRead(String object) { + return execRead(object, null); + } + + @Override + public Object execRead(String object, String language) { + if (language.equalsIgnoreCase(LanguageEnum.CHINESE.getValue())) { + return object.equals("是"); + } + return object.equals("Yes"); + } +} diff --git a/src/test/java/converter/Student2ExpelWriteConverter.java b/src/test/java/converter/Student2ExpelWriteConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..025ba882c0b8bb36eba5a6de5bbd22986ced2591 --- /dev/null +++ b/src/test/java/converter/Student2ExpelWriteConverter.java @@ -0,0 +1,25 @@ +package converter; + +import com.github.crab2died.constant.LanguageEnum; +import com.github.crab2died.converter.WriteConvertible; + +/** + * 导出excel Boolean 数据转换器 + */ +public class Student2ExpelWriteConverter implements WriteConvertible { + + @Override + public Object execWrite(Object object) { + return execWrite(object, null); + } + + @Override + public Object execWrite(Object object, String language) { + Boolean expel = (Boolean) object; + if (!LanguageEnum.CHINESE.getValue().equalsIgnoreCase(language)) { + return expel ? "Yes" : "No"; + } else { + return expel ? "是" : "否"; + } + } +} diff --git a/src/test/java/modules/Student2.java b/src/test/java/modules/Student2.java index 3411912a281699afb89b13e9b95732a1f4dfd24a..f07229cafa2260158448a24fe3b4475daf750508 100644 --- a/src/test/java/modules/Student2.java +++ b/src/test/java/modules/Student2.java @@ -1,70 +1,39 @@ package modules; import com.github.crab2died.annotation.ExcelField; +import com.github.crab2died.annotation.I18nField; import converter.Student2DateConverter; -import converter.Student2ExpelConverter; +import converter.Student2ExpelRealConverter; +import converter.Student2ExpelWriteConverter; +import lombok.Data; import java.util.Date; - +@Data public class Student2 { @ExcelField(title = "学号", order = 1) + @I18nField(titles = {"en-us|student id"}) private Long id; @ExcelField(title = "姓名", order = 2) + @I18nField(titles = {"en-us|name"}) private String name; // 写入数据转换器 Student2DateConverter @ExcelField(title = "入学日期", order = 3, writeConverter = Student2DateConverter.class) + @I18nField(titles = {"en-us|enroll date"}) private Date date; @ExcelField(title = "班级", order = 4) + @I18nField(titles = {"en-us|class"}) private Integer classes; - // 读取数据转换器 Student2ExpelConverter - @ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class) + // 读写数据转换器 Student2ExpelRealConverter + @ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelRealConverter.class, writeConverter = Student2ExpelWriteConverter.class) + @I18nField(titles = {"en-us|is expel?"}) private boolean expel; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Date getDate() { - return date; - } - - public void setDate(Date date) { - this.date = date; - } - - public Integer getClasses() { - return classes; - } - - public void setClasses(Integer classes) { - this.classes = classes; - } - - public boolean isExpel() { - return expel; - } - - public void setExpel(boolean expel) { - this.expel = expel; - } public Student2(Long id, String name, Date date, Integer classes, boolean expel) { this.id = id; @@ -74,17 +43,5 @@ public class Student2 { this.expel = expel; } - public Student2() { - } - @Override - public String toString() { - return "Student2{" + - "id=" + id + - ", name='" + name + '\'' + - ", date=" + date + - ", classes=" + classes + - ", expel='" + expel + '\'' + - '}'; - } }