poi Excel 纯数字的情况下,将单元格改为文本格式

/**

     * 根据HSSFCell类型设置数据

     * @param cell

     * @return 

     */  

    private String getCellFormatValue(HSSFCell cell) {  

        String cellvalue = “”;  

        if (cell != null) {  

            // 判断当前Cell的Type  

            switch (cell.getCellType()) {  

            // 如果当前Cell的Type为NUMERIC  

            case HSSFCell.CELL_TYPE_NUMERIC:  

            case HSSFCell.CELL_TYPE_FORMULA: {  

                // 判断当前的cell是否为Date  

                if (HSSFDateUtil.isCellDateFormatted(cell)) {  

                    // 如果是Date类型则,转化为Data格式  

                      

                    //方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00  

                    //cellvalue = cell.getDateCellValue().toLocaleString();  

                      

                    //方法2:这样子的data格式是不带带时分秒的:2011-10-12  

                    Date date = cell.getDateCellValue();  

                    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);  

                    cellvalue = sdf.format(date);  

                }  

                else {  

                    // 纯数字的情况下,将单元格改为文本格式

                    HSSFDataFormatter dataFormatter = new HSSFDataFormatter();

                    cellvalue = dataFormatter.formatCellValue(cell);

 

                 // 如果是纯数字  

//                    cellvalue = String.valueOf(cell.getNumericCellValue());  

                }  

                break;  

            }  

            // 如果当前Cell的Type为STRIN  

            case HSSFCell.CELL_TYPE_STRING:  

                // 取得当前的Cell字符串  

                cellvalue = cell.getRichStringCellValue().getString();  

                break;  

            // 默认的Cell值  

            default:  

                cellvalue = ” “;  

            }  

        } else {  

            cellvalue = “”;  

        }  

        return cellvalue;  

  

    }

    原文作者:LiMay4
    原文地址: https://blog.csdn.net/weixin_41560845/article/details/105954193
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞