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;
            }
        }
    
    
    
    
    
    }
    
  • 相关阅读:
    html HTML 文本格式化
    纯css3实现图片切换
    绝对路径和相对路径
    CSS设置html网页背景图片 CSS设置网页背景颜色
    使用CSS3制作响应式网页背景图像
    CSS背景全攻略
    HTML中通过CSS设置背景图片自适应浏览器大小
    CSS设置背景——图片背景
    如何让图片自适应手机等移动设备屏幕大小
    SERVER 2012 R2 对域用户禁用移动存储设备
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713819.html
Copyright © 2011-2022 走看看