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

相关推荐
麦兜*41 分钟前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
序属秋秋秋42 分钟前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript