侧边栏壁纸
博主头像
三味的小站博主等级

世界上没有偶然,有的只是必然的结果。

  • 累计撰写 61 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
Qt

Qt读取Excel之QAxObject

三味线
2018-04-18 / 0 评论 / 0 点赞 / 10 阅读 / 1583 字
QAxObject * excel = NULL;
QAxObject * workbooks = NULL;
QAxObject * workbook = NULL;
excel = new QAxObject(this);
excel -> setControl("Excel.Application");
if (!excel) {
    QMessageBox:: critical(NULL, "错误信息", "EXCEL对象丢失");
    return;
}
excel -> dynamicCall("SetVisible(bool)", false);
workbooks = excel -> querySubObject("WorkBooks");
workbook = workbooks -> querySubObject("Open(const QString&)", path);
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 intCols = columns -> property("Count").toInt();
int intRows = rows -> property("Count").toInt();
int intRowStart = usedrange -> property("Row").toInt();
int intColStart = usedrange -> property("Column").toInt();
ui.tableWidget -> setRowCount(intRows);
/*获取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);
        QString value = cell -> dynamicCall("Value2()").toString();
        setItemValue(i - 1, j - 1, value);
        delete cell;
    }
}
// 关闭excel
workbook -> dynamicCall("Close(Boolean)", true);
excel -> dynamicCall("Quit(void)");
delete excel;
excel = NULL;

0

评论区