From 55d5ceae6d18896e55a904e4fdb995d54b1919c6 Mon Sep 17 00:00:00 2001 From: zhuo0821 Date: Wed, 27 Mar 2019 01:54:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E5=80=BC?= =?UTF-8?q?=E5=BC=95=E8=B5=B7=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/wuwenze/poi/util/BeanUtil.java | 7 ++++++- src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/wuwenze/poi/util/BeanUtil.java b/src/main/java/com/wuwenze/poi/util/BeanUtil.java index 1a72fe8..d013426 100644 --- a/src/main/java/com/wuwenze/poi/util/BeanUtil.java +++ b/src/main/java/com/wuwenze/poi/util/BeanUtil.java @@ -15,10 +15,13 @@ package com.wuwenze.poi.util; -import java.lang.reflect.InvocationTargetException; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.PropertyUtilsBean; +import org.apache.commons.beanutils.converters.DateConverter; + +import java.lang.reflect.InvocationTargetException; /** * @author wuwenze @@ -28,6 +31,8 @@ public class BeanUtil extends org.apache.commons.beanutils.BeanUtils { public static void setComplexProperty(Object bean, String name, Object value) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException { + //修复日期为空时bug + ConvertUtils.register(new DateConverter(null), java.util.Date.class); if (!name.contains(".")) { BeanUtil.setProperty(bean, name, value); return; diff --git a/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java b/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java index b0adc23..57111c2 100644 --- a/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java +++ b/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java @@ -354,6 +354,10 @@ public class ExcelXlsxReader extends DefaultHandler { private Map checkAndConvertProperty(Integer cellIndex, ExcelProperty property, Object propertyValue) { + //如果值为$EMPTY_CELL$时,直接置为空 + if (propertyValue != null && propertyValue.equals(Const.XLSX_DEFAULT_EMPTY_CELL_VALUE)) { + propertyValue = null; + } // required Boolean required = property.getRequired(); if (null != required && required) { @@ -377,7 +381,7 @@ public class ExcelXlsxReader extends DefaultHandler { // dateFormat String dateFormat = property.getDateFormat(); - if (!ValidatorUtil.isEmpty(dateFormat)) { + if (!ValidatorUtil.isEmpty(dateFormat) && isNotBlank(propertyValue)) { try { // 时间格式转换后,直接返回。 Date parseDateValue = DateUtil.parse(dateFormat, propertyValue); @@ -410,7 +414,7 @@ public class ExcelXlsxReader extends DefaultHandler { // regularExp String regularExp = property.getRegularExp(); - if (!ValidatorUtil.isEmpty(regularExp)) { + if (!ValidatorUtil.isEmpty(regularExp) && isNotBlank(propertyValue)) { if (!RegexUtil.isMatches(regularExp, propertyValue)) { String regularExpMessage = property.getRegularExpMessage(); String validErrorMessage = !ValidatorUtil.isEmpty(regularExpMessage) ? -- Gitee From 3c30b59d985acc88f0b2b93520bd704913e77cd2 Mon Sep 17 00:00:00 2001 From: zhuo0821 Date: Wed, 27 Mar 2019 02:39:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wuwenze/poi/xlsx/ExcelXlsxReader.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java b/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java index 57111c2..57bf9da 100644 --- a/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java +++ b/src/main/java/com/wuwenze/poi/xlsx/ExcelXlsxReader.java @@ -31,6 +31,7 @@ import com.wuwenze.poi.validator.Validator; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; @@ -72,6 +73,9 @@ public class ExcelXlsxReader extends DefaultHandler { private Integer mBeginReadRowIndex = Const.XLSX_DEFAULT_BEGIN_READ_ROW_INDEX; private final Object mEmptyCellValue = Const.XLSX_DEFAULT_EMPTY_CELL_VALUE; + private final DataFormatter formatter = new DataFormatter(); + + public ExcelXlsxReader(Class entityClass,// ExcelMapping excelMapping, // ExcelReadHandler excelReadHandler) { @@ -276,6 +280,10 @@ public class ExcelXlsxReader extends DefaultHandler { XSSFCellStyle style = mStylesTable.getStyleAt(styleIndex); mFormatIndex = style.getDataFormat(); mFormatString = style.getDataFormatString(); + if ("m/d/yy".equals(mFormatString)) { + mNextCellType = mNextCellType.DATE; + mFormatString = "yyyy-MM-dd hh:mm:ss.SSS"; + } if (null == mFormatString) { mNextCellType = mNextCellType.NULL; mFormatString = BuiltinFormats.getBuiltinFormat(mFormatIndex); @@ -284,6 +292,7 @@ public class ExcelXlsxReader extends DefaultHandler { } private String getCellValue(String value) { + String thisStr = ""; switch (mNextCellType) { case BOOL: return value.charAt(0) == '0' ? "FALSE" : "TRUE"; @@ -295,8 +304,25 @@ public class ExcelXlsxReader extends DefaultHandler { return new XSSFRichTextString(value).toString(); case STRING: return String.valueOf(value); + case NUMBER: + if (mFormatString != null) { + thisStr = formatter.formatRawCellContents(Double.parseDouble(value), mFormatIndex, mFormatString).trim(); + } else { + thisStr = value; + } + + thisStr = thisStr.replace("_", "").trim(); + break; + case DATE: + thisStr = formatter.formatRawCellContents(Double.parseDouble(value), mFormatIndex, mFormatString); + // 对日期字符串作特殊处理 + thisStr = thisStr.replace(" ", "T"); + break; + default: + thisStr = ""; + break; } - return value; + return thisStr; } private final static String CHECK_MAP_KEY_OF_VALUE = "CELL_VALUE"; -- Gitee