Qt读取excel表格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/****************************************************************************************************/
/one*************************************************************************************************/
void TestReadExcel::readExcel()
{
 QAxObject *excel = NULL;
    QAxObject *workbooks = NULL;
    QAxObject *workbook = NULL;
  
    excel = new QAxObject("Excel.Application");
    if (!excel)
    {
        QMessageBox::critical(this"错误信息""EXCEL对象丢失");
        return;
    }
    excel->dynamicCall("SetVisible(bool)"false);
    workbooks = excel->querySubObject("WorkBooks");
    workbook = workbooks->querySubObject("Open(QString, QVariant)", QString(tr("d:\\test.xls")));
    QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1);//打开第一个sheet
  
 //QAxObject * worksheet = workbook->querySubObject("WorkSheets");//获取sheets的集合指针
 //int intCount = worksheet->property("Count").toInt();//获取sheets的数量
  
  
    QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
    QAxObject * rows = usedrange->querySubObject("Rows");
    QAxObject * columns = usedrange->querySubObject("Columns");
    /*获取行数和列数*/
    int intRowStart = usedrange->property("Row").toInt();
    int intColStart = usedrange->property("Column").toInt();
    int intCols = columns->property("Count").toInt();
    int intRows = rows->property("Count").toInt();
    /*获取excel内容*/
    for (int i = intRowStart; i < intRowStart + intRows; i++)  //行
    {
        for (int j = intColStart; j < intColStart + intCols; j++)  //列
        {
            QAxObject * cell = worksheet->querySubObject("Cells(int,int)", i, j );  //获取单元格
           // qDebug() << i << j << cell->property("Value");         //*****************************出问题!!!!!!
   qDebug() << i << j <<cell->dynamicCall("Value2()").toString(); //正确
        }
    }
 workbook->dynamicCall("Close (Boolean)"false);
  
 //同样,设置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))这样才成功的。。
  
 //excel->dynamicCall("Quit (void)");
 delete excel;//一定要记得删除,要不线程中会一直打开excel.exe
}
 
/****************************************************************************************************/
/two*************************************************************************************************/
int main(int argc, char **argv)
{
    QApplication a(argc, argv);
 //   QAxSelect select;
 //   select.show();
    QAxWidget excel("Excel.Application");
    excel.setProperty("Visible", true);//显示当前窗口
    //excel.setProperty("Caption","Invoke Microsoft Excel");//更改标题栏
    QAxObject * workbooks = excel.querySubObject("WorkBooks");//获取excel的集合指针
    QAxObject *workbook = workbooks->querySubObject("Open (const QString &)",QString("c:/test.xls"));//打开指定路径的xls文档
    QAxObject * worksheets = workbook->querySubObject("WorkSheets");//获取sheets的集合指针
    int intCount = worksheets->property("Count").toInt();//获取sheets的数量
    for (int i = 1; i <= intCount; i++)
    {
        int intVal;
        QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);//获取指定位置的sheet页面
        qDebug() << i << worksheet->property("Name").toString();
        QAxObject * range = worksheet->querySubObject("Cells(1,1)");//获取sheet页面中的C(1,1)数据
        intVal = range->property("Value").toInt();//保存该数据
        range->setProperty("Value", QVariant(intVal+1));//将数据加1并重新保存回C(1,1)
        QAxObject * range2 = worksheet->querySubObject("Range(C1)");//???????????
        intVal = range2->property("Value").toInt();
        range2->setProperty("Value", QVariant(intVal+1));
    }
    QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);//打开第一个sheet
    QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
    QAxObject * rows = usedrange->querySubObject("Rows");
    QAxObject * columns = usedrange->querySubObject("Columns");
    int intRowStart = usedrange->property("Row").toInt();
    qDebug() << intRowStart;
    int intColStart = usedrange->property("Column").toInt();
    qDebug() << intColStart;
    int intCols = columns->property("Count").toInt();
    int intRows = rows->property("Count").toInt();
    for (int i = intRowStart; i < intRowStart + intRows; i++)
    {
        for (int j = intColStart; j < intColStart + intCols; j++)
        {
            QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );
            qDebug() << i << j << range->property("Value");
        }
    }
    excel.setProperty("DisplayAlerts", 0);
    workbook->dynamicCall("SaveAs (const QString&)", QString("c:/xlsbyqt.xls"));
    excel.setProperty("DisplayAlerts", 1);
    workbook->dynamicCall("Close (Boolean)", false);
    excel.dynamicCall("Quit (void)");
    return a.exec();
}

### 使用 Qt 读取 Excel 表格数据 #### 创建 `Excel.Application` 对象 为了在 Qt读取 Excel 文件,首先需要创建一个 `Excel.Application` 实例。这可以通过 `QAxObject` 来完成: ```cpp QAxObject* excelApplication = new QAxObject("Excel.Application", this); excelApplication->dynamicCall("SetVisible (bool Visible)", false); // 不显示 Excel 应用程序窗口 ``` 此部分代码初始化了 Excel 应用实例,并设置其不可见[^2]。 #### 打开工作簿 接着,加载指定路径下的 Excel 工作簿: ```cpp QString filePath = "C:/example.xlsx"; // 替换为实际文件路径 QAxObject *workbooks = excelApplication->querySubObject("Workbooks"); workbooks->dynamicCall("Open (const QString&)", filePath); ``` 上述代码片段展示了如何打开特定位置的 Excel 文件[^4]。 #### 访问工作表及其单元格 一旦打开了工作簿,就可以访问其中的工作表以及具体单元格的内容: ```cpp // 获取第一个工作表 QAxObject *worksheet = excelApplication->querySubObject("ActiveSheet"); // 定义要读取的范围 A1:B5 QAxObject *range = worksheet->querySubObject("Range(const QVariant&, const QVariant&)", QVariant("A1"), QVariant("B5")); // 遍历选定区域内的每一个单元格 for(int row = 1; row <= range->property("Rows").toInt(); ++row){ for(int col = 1; col <= range->property("Columns").toInt(); ++col){ QAxObject *cell = range->querySubObject("Cells(int, int)", row, col); qDebug() << cell->property("Value").toString(); } } ``` 这段代码说明了怎样获取活动工作表上的某一片段内所有单元格的信息[^3]。 #### 关闭文档与清理资源 最后,在完成了所有的读取操作后记得关闭文档并释放占用的对象指针: ```cpp if(worksheet != nullptr) delete worksheet; if(range != nullptr) delete range; // 关闭当前工作簿而不保存更改 excelApplication->dynamicCall("Quit()"); delete excelApplication; ``` 以上就是完整的流程介绍,确保每次使用完毕都妥善处理好所使用的 COM 组件以防止内存泄漏等问题的发生[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值