Qt excel操作

weixin_38088241 2019-09-19 12:45:21
复制代码#include <QtGui>#include <QAxObject>#include <QAxWidget>#include <qaxselect.h>int main(int argc, char **argv){    QApplication a(argc, argv);    QAxSelect select;    select.show();    QAxWidget excel("Excel.Application");    excel.setProperty("Visible", true);    QAxObject * workbooks = excel.querySubObject("WorkBooks");    workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));    QAxObject * workbook = excel.querySubObject("ActiveWorkBook");    QAxObject * worksheets = workbook->querySubObject("WorkSheets");    int intCount = worksheets->property("Count").toInt();    for (int i = 1; i <= intCount; i++)    {        int intVal;        QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);        qDebug() << i << worksheet->property("Name").toString();        QAxObject * range = worksheet->querySubObject("Cells(1,1)");        intVal = range->property("Value").toInt();        range->setProperty("Value", QVariant(intVal+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);    QAxObject * usedrange = worksheet->querySubObject("UsedRange");    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();    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();}执行的时候 会出现这样的错误复制代码CoCreateInstance failure (????????) QAxBase::setControl: requested control Excel.Application could not be instantiated QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed
...全文
53 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38100812 2019-09-19
  • 打赏
  • 举报
回复
因为没用过QAx所以帮不了什么……上网搜索一下那些提示看看?或者找文档看看是不是哪些地方没做好

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧