Java tutorial
/** * 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 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.qihang.winter.poi.excel.imports; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.math.BigDecimal; import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; import com.qihang.winter.core.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import com.qihang.winter.poi.excel.entity.params.ExcelImportEntity; import com.qihang.winter.poi.excel.entity.sax.SaxReadCellEntity; import com.qihang.winter.poi.exception.excel.ExcelImportException; import com.qihang.winter.poi.exception.excel.enums.ExcelImportEnum; import com.qihang.winter.poi.util.PoiPublicUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Cell ?? * ?? 1.Excel 2.?replace? 3.handler?? 4.? * * @author Zerrion * @date 2014626 ?10:42:28 */ public class CellValueServer { private static final Logger LOGGER = LoggerFactory.getLogger(CellValueServer.class); private List<String> hanlderList = null; /** * ?? * * @param xclass * @param cell * @param entity * @return */ private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity) { if (cell == null) { return ""; } Object result = null; // ?,cell?? if ("class java.util.Date".equals(xclass) || ("class java.sql.Time").equals(xclass)) { if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { // ? result = cell.getDateCellValue(); } else { cell.setCellType(Cell.CELL_TYPE_STRING); result = getDateData(entity, cell.getStringCellValue()); } if (("class java.sql.Time").equals(xclass)) { result = new Time(((Date) result).getTime()); } } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { result = cell.getNumericCellValue(); } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) { result = cell.getBooleanCellValue(); } else { result = cell.getStringCellValue(); } return result; } /** * ?? * * @author Zerrion * @date 20131126 * @param entity * @param value * @return */ private Date getDateData(ExcelImportEntity entity, String value) { if (StringUtils.isNotEmpty(entity.getFormat()) && StringUtils.isNotEmpty(value)) { SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); try { return format.parse(value); } catch (ParseException e) { LOGGER.error("?,?:{},:{}", entity.getFormat(), value); throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); } } return null; } /** * ?cell * * @param object * @param excelParams * @param cell * @param titleString */ public Object getValue(com.qihang.winter.poi.handler.inter.IExcelDataHandler dataHanlder, Object object, Cell cell, Map<String, ExcelImportEntity> excelParams, String titleString) throws Exception { ExcelImportEntity entity = excelParams.get(titleString); String xclass = "class java.lang.Object"; if (!(object instanceof Map)) { Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 ? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); Type[] ts = setMethod.getGenericParameterTypes(); xclass = ts[0].toString(); } Object result = getCellValue(xclass, cell, entity); if (entity != null) { result = hanlderSuffix(entity.getSuffix(), result); result = replaceValue(entity.getReplace(), result); } result = hanlderValue(dataHanlder, object, result, titleString); return getValueByType(xclass, result, entity); } /** * ?cell * @param dataHanlder * @param object * @param entity * @param excelParams * @param titleString * @return */ public Object getValue(com.qihang.winter.poi.handler.inter.IExcelDataHandler dataHanlder, Object object, SaxReadCellEntity cellEntity, Map<String, ExcelImportEntity> excelParams, String titleString) { ExcelImportEntity entity = excelParams.get(titleString); Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 ? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); Type[] ts = setMethod.getGenericParameterTypes(); String xclass = ts[0].toString(); Object result = cellEntity.getValue(); result = hanlderSuffix(entity.getSuffix(), result); result = replaceValue(entity.getReplace(), result); result = hanlderValue(dataHanlder, object, result, titleString); return getValueByType(xclass, result, entity); } /** * ? * @param result * @param suffix * @return */ private Object hanlderSuffix(String suffix, Object result) { if (StringUtils.isNotEmpty(suffix) && result != null && result.toString().endsWith(suffix)) { String temp = result.toString(); return temp.substring(0, temp.length() - suffix.length()); } return result; } /** * ?? * * @param xclass * @param result * @param entity * @return */ private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) { try { if ("class java.util.Date".equals(xclass)) { return result; } if ("class java.lang.Boolean".equals(xclass) || "boolean".equals(xclass)) { return Boolean.valueOf(String.valueOf(result)); } if ("class java.lang.Double".equals(xclass) || "double".equals(xclass)) { return result == null || result.equals("") ? null : Double.valueOf(String.valueOf(result)); } if ("class java.lang.Long".equals(xclass) || "long".equals(xclass)) { return result == null || result.equals("") ? null : Long.valueOf(String.valueOf(result)); } if ("class java.lang.Float".equals(xclass) || "float".equals(xclass)) { return result == null || result.equals("") ? null : Float.valueOf(String.valueOf(result)); } if ("class java.lang.Integer".equals(xclass) || "int".equals(xclass)) { return result == null || result.equals("") ? null : Integer.valueOf(String.valueOf(result)); } if ("class java.math.BigDecimal".equals(xclass)) { return new BigDecimal(String.valueOf(result)); } if ("class java.lang.String".equals(xclass)) { //String ,Excel????String,Double, if (result instanceof String) { return result; } // double if (result instanceof Double) { return PoiPublicUtil.doubleToString((Double) result); } return String.valueOf(result); } return result; } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); } } /** * ??? * * @param dataHanlder * @param object * @param result * @param titleString * @return */ private Object hanlderValue(com.qihang.winter.poi.handler.inter.IExcelDataHandler dataHanlder, Object object, Object result, String titleString) { if (dataHanlder == null || dataHanlder.getNeedHandlerFields() == null || dataHanlder.getNeedHandlerFields().length == 0) { return result; } if (hanlderList == null) { hanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); } if (hanlderList.contains(titleString)) { return dataHanlder.importHandler(object, titleString, result); } return result; } /** * ? * * @param replace * @param result * @return */ private Object replaceValue(String[] replace, Object result) { if (StringUtil.isNotEmpty(result)) { if (replace != null && replace.length > 0) { String temp = String.valueOf(result); String[] tempArr; for (int i = 0; i < replace.length; i++) { tempArr = replace[i].split("_"); if (temp.equals(tempArr[0])) { return tempArr[1]; } } } } return result; } }