Qt打开excel文件,并读取指定单元格数据

  1. 下载并安装QXlsx库,详见之前的博文Qt子线程创建excel文件报错QObject: Cannot create children for a parent that is in a different thread.-CSDN博客

cpp 复制代码
// 创建一个XlsxDocument对象
    QString filename = "D:\\mydocuments\\data_acquisition\\data\\test.xlsx";
    QXlsx::Document xlsx(filename);
    // 打开Excel文件
    if (!xlsx.load()) {
        qDebug() << "Failed to open Excel file.";
        return;
    }
    // 获取工作表名称列表
    QStringList sheetNames = xlsx.sheetNames();
    if (sheetNames.isEmpty()) {
        qDebug() << "No sheets found in the Excel file.";
    }
    // 选择第一个工作表
    xlsx.selectSheet(sheetNames.first());
    // 指定要读取的列(例如,第2列,索引从0开始)
    int columnIndex = 1; // 列B(Excel中的第二列)

    // 读取指定列的数据
    int rowCount = xlsx.dimension().rowCount();
    for (int row = 1; row <= rowCount; ++row) { // 跳过标题行,从第二行开始读取数据
        QVariant cellValue = xlsx.cellAt(row, columnIndex)->value();
        double voltage = cellValue.toDouble(); //经过这一步就可以得到正常的单元格数值,笔者的数值是双精度浮点型
    }
相关推荐
外收内放4 小时前
FastAPI猜字谜实战(5):会话管理
开发语言·python·学习·fastapi
Ai-_Man4 小时前
Microsoft Copilot对话数据怎么导入到另外一个Microsoft Copilot账号里去?AI导出鸭
开发语言·javascript·人工智能·microsoft·chatgpt·ecmascript·copilot
H.莓飛4 小时前
【数据结构】归并与外排序
linux·服务器·c语言·开发语言·数据结构·visual studio
技术老孙4 小时前
给 Agent 装上对话框 —— AgentScope-Java 接入 Spring Boot 与 SSE 流式实战
java·开发语言·spring boot
Yyyyyy~5 小时前
【C++】String
c++
技术小甜甜5 小时前
【Copilot】能选 Claude 和 GPT-6 了,Excel 分析该交给谁
gpt·excel·copilot·claude·microsoft365·gpt-6
星夜离尘5613 小时前
MATLAB 工程实验:不等间隔流量积分,先把缺口与覆盖率说清楚
开发语言·matlab·时间戳·数据质量·数值积分
Ai-_Man13 小时前
Meta Muse还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·c#·1024程序员节
梓豪csd13 小时前
消息订阅中心:谁该收到哪条通知
java·开发语言·人工智能·低代码·自动化·1024程序员节
会编程的小孩13 小时前
C++ qt的安装
c++