zoukankan      html  css  js  c++  java
  • poi cell转成对应的字符

    package utils;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.DateUtil;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    
    public class ExcelCellUtils {
    
    
        private static Map<Class<?>, CellType[]> validateMap = new HashMap<>();
    
        static {
            validateMap.put(String[].class, new CellType[]{CellType.STRING});
            validateMap.put(Double[].class, new CellType[]{CellType.NUMERIC});
            validateMap.put(String.class, new CellType[]{CellType.STRING});
            validateMap.put(Double.class, new CellType[]{CellType.NUMERIC});
            validateMap.put(Date.class, new CellType[]{CellType.NUMERIC, CellType.STRING});
            validateMap.put(Integer.class, new CellType[]{CellType.NUMERIC});
            validateMap.put(Float.class, new CellType[]{CellType.NUMERIC});
            validateMap.put(Long.class, new CellType[]{CellType.NUMERIC});
            validateMap.put(Boolean.class, new CellType[]{CellType.BOOLEAN});
        }
    
        /**
         * 获取cell类型的文字描述
         *
         * @param cellType <pre>
         *                 CellType.BLANK
         *                 CellType.BOOLEAN
         *                 CellType.ERROR
         *                 CellType.FORMULA
         *                 CellType.NUMERIC
         *                 CellType.STRING
         *                 </pre>
         * @return
         */
        private static String getCellTypeByInt(CellType cellType) {
            if(cellType == CellType.BLANK) {
                return "Null type";
            } else if(cellType == CellType.BOOLEAN) {
                return "Boolean type";
            } else if(cellType == CellType.ERROR) {
                return "Error type";
            } else if(cellType == CellType.FORMULA) {
                return "Formula type";
            } else if(cellType == CellType.NUMERIC) {
                return "Numeric type";
            } else if(cellType == CellType.STRING) {
                return "String type";
            } else {
                return "Unknown type";
            }
        }
    
        /**
         * 获取单元格值
         *
         * @param cell
         * @return
         */
        public static Object getCellValue(Cell cell) {
            if (cell == null
                    || (cell.getCellTypeEnum() == CellType.STRING && StringUtils.isBlank(cell
                    .getStringCellValue()))) {
                return null;
            }
            CellType cellType = cell.getCellTypeEnum();
            if(cellType == CellType.BLANK) {
                return null;
            } else if(cellType == CellType.BOOLEAN) {
                return cell.getBooleanCellValue();
            } else if(cellType == CellType.ERROR) {
                return cell.getErrorCellValue();
            } else if(cellType == CellType.FORMULA) {
                try {
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        return cell.getDateCellValue();
                    } else {
                        return cell.getNumericCellValue();
                    }
                } catch (IllegalStateException e) {
                    return cell.getRichStringCellValue();
                }
            }
            else if(cellType == CellType.NUMERIC){
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue();
                } else {
                    return cell.getNumericCellValue();
                }
            }
            else if(cellType == CellType.STRING) {
                return cell.getStringCellValue();
            } else {
                return null;
            }
        }
    
    
    
    
    
    }
    
  • 相关阅读:
    多线程与线程池
    hdu1506 Largest Rectangle in a Histogram
    安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口
    最小总代价 状压DP
    Sumsets 递推
    不容易系列之(4)——考新郎 递推
    超级楼梯 递推
    阿牛的EOF牛肉串(递推)
    子串查询(二维前缀数组) 2018"百度之星"程序设计大赛
    cf#513 B. Maximum Sum of Digits
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713819.html
Copyright © 2011-2022 走看看