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中公式的计算值

相关推荐
Wang's Blog21 小时前
Go-Zero项目开发4: 用户服务搜索、详情与统一错误处理
开发语言·golang
我星期八休息21 小时前
网络编程—应用层HTTP协议
linux·运维·开发语言·前端·网络·网络协议·http
梅雅达编程笔记21 小时前
零基础学 Python 第14章 | 模块、包与第三方库
开发语言·python·django·numpy·pandas
Mr__Miss21 小时前
Java泛型完全指南:从入门到精通
java·开发语言·python
赵庆明老师21 小时前
Vben精讲:14-Vben远程加载语言包
开发语言·vben
jinyishu_1 天前
模拟实现 C++ 栈和队列——从适配器模式看懂 STL 容器之美
java·c++·适配器模式
hehelm1 天前
AI大模型接入SDK—通用模块设计
linux·开发语言·c++
前端工作日常1 天前
我学习到的Java类和对象区别
java·后端
前端工作日常1 天前
我学习到的Java类完整结构
java·后端
什巳1 天前
JAVA练习309- 二叉树的层序遍历
java·数据结构·算法·leetcode