You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

readxl包的read_excel()读取xls文件时为何全为0?

解决xls文件读取前三列全为0及RStudio崩溃问题

1. 排查原文件异常

  • 手动打开目标xls文件的"Humedad"工作表,检查前三列:
    • 确认列名是否正常显示,有无隐藏格式、合并单元格或无效数据
    • 验证单元格内容是否存在损坏(比如特殊字符、格式冲突)

2. 调整readxl读取参数

  • 先取消强制列类型,让readxl自动推断,排查是否是col_types = "text"导致的异常:
    df <- read_excel(archivo_estm, sheet = "Humedad")
    
  • 若自动推断正常,再针对性设置前三列为文本类型:
    # 先获取工作表总列数
    total_cols <- ncol(read_excel(archivo_estm, sheet = "Humedad", n_max = 0))
    # 构建列类型规则:前三列文本,其余自动推断
    col_spec <- c(rep("text", 3), rep("guess", total_cols - 3))
    df <- read_excel(archivo_estm, sheet = "Humedad", col_types = col_spec)
    
  • 尝试指定读取范围,跳过可能异常的表头区域:
    # 示例:从第1行(列名)到第100行读取,可根据实际数据行数调整
    df <- read_excel(archivo_estm, sheet = "Humedad", range = "A1:C100", col_types = "text")
    

3. 转换文件格式规避损坏

  • 用Excel打开原xls文件,选择「另存为」→「Excel工作簿(.xlsx)」,转换后再读取:
    df <- read_excel("转换后的文件.xlsx", sheet = "Humedad", col_types = "text")
    
  • 无Excel时,可使用LibreOffice等工具完成格式转换,避免原xls文件的损坏问题

4. 替代读取方案

  • 使用readxl专门针对xls格式的read_xls()函数:
    df <- read_xls(archivo_estm, sheet = "Humedad", col_types = "text")
    
  • 转成xlsx后用openxlsx包读取(稳定性优于xlsx包):
    library(openxlsx)
    df <- read.xlsx("转换后的文件.xlsx", sheet = "Humedad", colNames = TRUE)
    # 转换为tibble
    df <- dplyr::as_tibble(df)
    

内容的提问来源于stack exchange,提问作者Alvaro Morales

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:03:22