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

相关推荐
软泡芙39 分钟前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
小程故事多_8042 分钟前
Harness实战指南,在Java Spring Boot项目中规范落地OpenSpec+Claude Code
java·人工智能·spring boot·架构·aigc·ai编程
浪扼飞舟43 分钟前
WPF输入验证(ValidationRule)
java·javascript·wpf
砍材农夫5 小时前
spring-ai 第四多模态API
java·人工智能·spring
她说..8 小时前
Java 对象相关高频面试题
java·开发语言·spring·java-ee
watson_pillow8 小时前
c++ 协程的初步理解
开发语言·c++
庞轩px8 小时前
深入理解 sleep() 与 wait():从基础到监视器队列
java·开发语言·线程··wait·sleep·监视器
故事和你918 小时前
洛谷-算法1-2-排序2
开发语言·数据结构·c++·算法·动态规划·图论
皮皮林5519 小时前
面试官:ZSet 的底层实现是什么?
java
码云数智-大飞9 小时前
C++ RAII机制:资源管理的“自动化”哲学
java·服务器·php