Browse Source

获取时间加强

4.1.3.A
jueyue 7 years ago
parent
commit
dca0ce9d62
2 changed files with 38 additions and 32 deletions
  1. +6
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportBase.java
  2. +32
    -32
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueServer.java

+ 6
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportBase.java View File

@@ -92,6 +92,12 @@ public class ExportBase {
temp = format.parse(value.toString()); temp = format.parse(value.toString());
} else if (value instanceof Date) { } else if (value instanceof Date) {
temp = (Date) value; temp = (Date) value;
} else if (value instanceof java.sql.Date){
temp = new Date(((java.sql.Date)value).getTime());
} else if (value instanceof java.sql.Time){
temp = new Date(((java.sql.Time)value).getTime());
} else if (value instanceof java.sql.Timestamp){
temp = new Date(((java.sql.Timestamp)value).getTime());
} }
if (temp != null) { if (temp != null) {
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); SimpleDateFormat format = new SimpleDateFormat(entity.getFormat());


+ 32
- 32
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueServer.java View File

@@ -1,13 +1,13 @@
/** /**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* *
* Unless required by applicable law or agreed to in writing, software
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
@@ -28,7 +28,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
@@ -45,19 +44,19 @@ import cn.afterturn.easypoi.util.PoiPublicUtil;
/** /**
* Cell 取值服务 * Cell 取值服务
* 判断类型处理数据 1.判断Excel中的类型 2.根据replace替换值 3.handler处理数据 4.判断返回类型转化数据返回 * 判断类型处理数据 1.判断Excel中的类型 2.根据replace替换值 3.handler处理数据 4.判断返回类型转化数据返回
*
*
* @author JueYue * @author JueYue
* 2014年6月26日 下午10:42:28 * 2014年6月26日 下午10:42:28
*/ */
public class CellValueServer { public class CellValueServer {
private static final Logger LOGGER = LoggerFactory.getLogger(CellValueServer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(CellValueServer.class);
private List<String> hanlderList = null;
private List<String> hanlderList = null;
/** /**
* 获取单元格内的值 * 获取单元格内的值
*
*
* @param cell * @param cell
* @param entity * @param entity
* @return * @return
@@ -68,8 +67,8 @@ public class CellValueServer {
} }
Object result = null; Object result = null;
if ("class java.util.Date".equals(xclass) || "class java.sql.Date".equals(xclass) if ("class java.util.Date".equals(xclass) || "class java.sql.Date".equals(xclass)
|| ("class java.sql.Time").equals(xclass)
|| ("class java.sql.Timestamp").equals(xclass)) {
|| ("class java.sql.Time").equals(xclass)
|| ("class java.sql.Timestamp").equals(xclass)) {
/* /*
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
// 日期格式 // 日期格式
@@ -79,10 +78,9 @@ public class CellValueServer {
result = getDateData(entity, cell.getStringCellValue()); result = getDateData(entity, cell.getStringCellValue());
}*/ }*/
//FIX: 单元格yyyyMMdd数字时候使用 cell.getDateCellValue() 解析出的日期错误 //FIX: 单元格yyyyMMdd数字时候使用 cell.getDateCellValue() 解析出的日期错误
if (HSSFDateUtil.isCellDateFormatted(cell)){
result = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
else {
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)) {
result = DateUtil.getJavaDate(cell.getNumericCellValue());
} else {
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
result = getDateData(entity, cell.getStringCellValue()); result = getDateData(entity, cell.getStringCellValue());
} }
@@ -95,11 +93,13 @@ public class CellValueServer {
if (("class java.sql.Timestamp").equals(xclass)) { if (("class java.sql.Timestamp").equals(xclass)) {
result = new Timestamp(((Date) result).getTime()); result = new Timestamp(((Date) result).getTime());
} }
} else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)) {
result = DateUtil.getJavaDate(cell.getNumericCellValue());
} else { } else {
switch (cell.getCellType()) { switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: case Cell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue() == null ? "" result = cell.getRichStringCellValue() == null ? ""
: cell.getRichStringCellValue().getString();
: cell.getRichStringCellValue().getString();
break; break;
case Cell.CELL_TYPE_NUMERIC: case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
@@ -123,7 +123,7 @@ public class CellValueServer {
} catch (Exception e1) { } catch (Exception e1) {
try { try {
result = cell.getRichStringCellValue() == null ? "" result = cell.getRichStringCellValue() == null ? ""
: cell.getRichStringCellValue().getString();
: cell.getRichStringCellValue().getString();
} catch (Exception e2) { } catch (Exception e2) {
throw new RuntimeException("获取公式类型的单元格失败", e2); throw new RuntimeException("获取公式类型的单元格失败", e2);
} }
@@ -149,7 +149,7 @@ public class CellValueServer {
/** /**
* 获取日期类型数据 * 获取日期类型数据
*
*
* @author JueYue * @author JueYue
* 2013年11月26日 * 2013年11月26日
* @param entity * @param entity
@@ -179,7 +179,7 @@ public class CellValueServer {
/** /**
* 获取cell的值 * 获取cell的值
*
*
* @param object * @param object
* @param excelParams * @param excelParams
* @param cell * @param cell
@@ -192,7 +192,7 @@ public class CellValueServer {
String xclass = "class java.lang.Object"; String xclass = "class java.lang.Object";
if (!(object instanceof Map)) { if (!(object instanceof Map)) {
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod();
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod();
Type[] ts = setMethod.getGenericParameterTypes(); Type[] ts = setMethod.getGenericParameterTypes();
xclass = ts[0].toString(); xclass = ts[0].toString();
} }
@@ -219,7 +219,7 @@ public class CellValueServer {
String titleString) { String titleString) {
ExcelImportEntity entity = excelParams.get(titleString); ExcelImportEntity entity = excelParams.get(titleString);
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod();
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod();
Type[] ts = setMethod.getGenericParameterTypes(); Type[] ts = setMethod.getGenericParameterTypes();
String xclass = ts[0].toString(); String xclass = ts[0].toString();
Object result = cellEntity.getValue(); Object result = cellEntity.getValue();
@@ -231,13 +231,13 @@ public class CellValueServer {
/** /**
* 把后缀删除掉 * 把后缀删除掉
* @param result
* @param suffix
* @param result
* @param suffix
* @return * @return
*/ */
private Object hanlderSuffix(String suffix, Object result) { private Object hanlderSuffix(String suffix, Object result) {
if (StringUtils.isNotEmpty(suffix) && result != null if (StringUtils.isNotEmpty(suffix) && result != null
&& result.toString().endsWith(suffix)) {
&& result.toString().endsWith(suffix)) {
String temp = result.toString(); String temp = result.toString();
return temp.substring(0, temp.length() - suffix.length()); return temp.substring(0, temp.length() - suffix.length());
} }
@@ -246,10 +246,10 @@ public class CellValueServer {
/** /**
* 根据返回类型获取返回值 * 根据返回类型获取返回值
*
*
* @param xclass * @param xclass
* @param result * @param result
* @param entity
* @param entity
* @return * @return
*/ */
private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) { private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) {
@@ -309,18 +309,18 @@ public class CellValueServer {
/** /**
* 调用处理接口处理值 * 调用处理接口处理值
*
*
* @param dataHanlder * @param dataHanlder
* @param object * @param object
* @param result * @param result
* @param titleString * @param titleString
* @return * @return
*/ */
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
private Object hanlderValue(IExcelDataHandler dataHanlder, Object object, Object result, private Object hanlderValue(IExcelDataHandler dataHanlder, Object object, Object result,
String titleString) { String titleString) {
if (dataHanlder == null || dataHanlder.getNeedHandlerFields() == null if (dataHanlder == null || dataHanlder.getNeedHandlerFields() == null
|| dataHanlder.getNeedHandlerFields().length == 0) {
|| dataHanlder.getNeedHandlerFields().length == 0) {
return result; return result;
} }
if (hanlderList == null) { if (hanlderList == null) {
@@ -334,7 +334,7 @@ public class CellValueServer {
/** /**
* 替换值 * 替换值
*
*
* @param replace * @param replace
* @param result * @param result
* @return * @return


Loading…
Cancel
Save