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

相关推荐
SunnyDays10111 分钟前
Java 实现插入和删除 Excel 行和列
java·python·excel
kels88992 分钟前
加密货币实时api的订单簿快照多久更新一次?
开发语言·笔记·python·金融·区块链
Byte Wizard5 分钟前
C语言数据在内存中的存储
c语言·开发语言
basketball6166 分钟前
C++面试考点 头文件与实现文件形式
开发语言·c++
SilentSamsara7 分钟前
类型注解进阶:Union、Optional、Any 与 Callable
开发语言·python·青少年编程
历程里程碑7 分钟前
56 . 高效ET非阻塞IO服务器设计指南
java·运维·服务器·开发语言·数据结构·c++·排序算法
@SmartSi10 分钟前
AgentScope Java 入门:如何使用 DashScopeChatModel 集成百练模型
java·agentscope
恣艺12 分钟前
Python 游戏开发与文件处理:PyGame + Turtle + openpyxl + python-docx + PyPDF2
开发语言·python·pygame
爱编程的小新☆13 分钟前
JAVA实现Manus智能体
java·react·cot·智能体·spring ai·manus·agent loop