Java excel单元格内容读取为字符串格式

导出数据到Excel,并把单元格内容转为字符串。

java 复制代码
// 将单元格内容转化为字符串
private static String convertCellValueToString(Cell cell) {
    if (null == cell) {
        return null;
    }
    String returnValue = null;
    switch (cell.getCellType()) {
        case STRING:  //字符串
            returnValue = cell.getStringCellValue();
            break;
        case NUMERIC: //数字
            double numericCellValue = cell.getNumericCellValue();
            boolean isInteger = isInteger(numericCellValue);
            if (isInteger) {
                DecimalFormat df = new DecimalFormat("0");
                returnValue = df.format(numericCellValue);
            } else {
                returnValue = Double.toString(numericCellValue);
            }
            break;
        case BOOLEAN: //布尔
            boolean booleanCellValue = cell.getBooleanCellValue();
            returnValue = Boolean.toString(booleanCellValue);
            break;
        case BLANK: //空值
            break;
        case FORMULA: //公式
            // returnValue = cell.getCellFormula();
            try {
                returnValue = String.valueOf(cell.getNumericCellValue());
            } catch (IllegalStateException e) {
                returnValue = String.valueOf(cell.getRichStringCellValue());
            }
            break;
        case ERROR: //故障
            break;
        default:
            break;
    }
    return returnValue;
}
// 判断是否为整数,是返回true,否则返回false.
public static boolean isInteger(Double num) {
    double eqs = 1e-10; //精度范围
    return num - Math.floor(num) < eqs;
}

参考

POI读取excel时,单元格内容转化字符串
Java poi读取Excel中公式的计算值

相关推荐
码农阿豪2 分钟前
Python Flask应用中文件处理与异常处理的实践指南
开发语言·python·flask
岁岁种桃花儿2 分钟前
CentOS7 彻底卸载所有JDK/JRE + 重新安装JDK8(实操完整版,解决kafka/jps报错)
java·开发语言·kafka
csbysj202014 分钟前
AngularJS 模块
开发语言
独好紫罗兰23 分钟前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n29 分钟前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
不会代码的小测试32 分钟前
UI自动化-POM封装
开发语言·python·selenium·自动化
roman_日积跬步-终至千里38 分钟前
【Java并发】Java 线程池实战:警惕使用CompletableFuture.supplyAsync
java·开发语言·网络
毕设源码-钟学长40 分钟前
【开题答辩全过程】以 基于Springboot的扶贫众筹平台为例,包含答辩的问题和答案
java·spring boot·后端
lsx20240643 分钟前
C++ 基本的输入输出
开发语言
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展