• java poi 获取单元格值


     /*
         * poi特殊日期格式:数字格式化成-yyyy年MM月dd日,格式
         * */
        private static ArrayList<String> PoiDateList = new ArrayList<String>() {
            {
                add("年");
                add("月");
                add("日");
            }
        };
        public static String GetValueByCellStyle(Cell rowCell, int rowCellType) {
            String value = "";
            switch (rowCellType) {
                case Cell.CELL_TYPE_STRING:
                    value = rowCell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    //  获取单元格值的格式化信息
                    String dataFormat = rowCell.getCellStyle().getDataFormatString();
                    //  判断格式化信息中是否存在:年月日
                    AtomicReference<Boolean> isDate = new AtomicReference<>(false);
                    if (StringUtils.isNotBlank(dataFormat))
                        PoiDateList.forEach(x -> isDate.set(isDate.get() || dataFormat.contains(x)));
    
                    if (DateUtil.isCellDateFormatted(rowCell)) {
                        value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                    } else if (DateUtil.isCellInternalDateFormatted(rowCell)) {
                        value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                    }
                    //有些情况,时间搓?数字格式化显示为时间,不属于上面两种时间格式
                    else if (isDate.get()) {
                        value = new SimpleDateFormat("yyyy-MM-dd").format(rowCell.getDateCellValue());
                    }
                    //有些情况,时间搓?数字格式化显示为时间,不属于上面两种时间格式
                    else if (dataFormat == null) {
                        value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                    } else {
                        if (!StringUtils.isNotBlank(dataFormat)) {
                            value = String.valueOf(rowCell.getNumericCellValue());
                        } else {
                            if (rowCell.getCellStyle().getDataFormatString().contains("$")) {
                                value = "$" + rowCell.getNumericCellValue();
                            } else if (rowCell.getCellStyle().getDataFormatString().contains("¥")) {
                                value = "¥" + rowCell.getNumericCellValue();
                            } else if (rowCell.getCellStyle().getDataFormatString().contains("¥")) {
                                value = "¥" + rowCell.getNumericCellValue();
                            } else if (rowCell.getCellStyle().getDataFormatString().contains("€")) {
                                value = "€" + String.valueOf(rowCell.getNumericCellValue());
                            } else {
                                value = String.valueOf(rowCell.getNumericCellValue());
                            }
                        }
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    value = String.valueOf(rowCell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_ERROR:
                    value = ErrorEval.getText(rowCell.getErrorCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    //  TODO: 是否存在 嵌套 公式类型
                    value = GetValueByCellStyle(rowCell, rowCell.getCachedFormulaResultType());
                    break;
                default:
                    break;
            }
            return value;
        }
    
  • 相关阅读:
    2017 经典的CVPR 关于ImageCaptioning论文
    BLEU METEOR ROUGE CIDEr 详解和实现
    pytorch中的nn.CrossEntropyLoss()
    pytorch 中改变tensor维度(transpose)、拼接(cat)、压缩(squeeze)详解
    pytorch 中tensor的加减和mul、matmul、bmm
    pytorch 中的Variable一般常用的使用方法
    Attention 和self-attention
    pytorch中的pack_padded_sequence和pad_packed_sequence用法
    linux 的sleep()、usleep()、nanosleep()函数的区别
    linux下删除东西还原
  • 原文地址:https://www.cnblogs.com/mengzhao/p/16111516.html
Copyright © 2020-2023  润新知