• 7226阅读
  • 1回复

[提问]Qt excel操作 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 楼主  发表于: 2011-11-13
  1. #include <QtGui>
  2. #include <QAxObject>
  3. #include <QAxWidget>
  4. #include <qaxselect.h>
  5. int main(int argc, char **argv)
  6. {
  7.     QApplication a(argc, argv);
  8.     QAxSelect select;
  9.     select.show();
  10.     QAxWidget excel("Excel.Application");
  11.     excel.setProperty("Visible", true);
  12.     QAxObject * workbooks = excel.querySubObject("WorkBooks");
  13.     workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));
  14.     QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
  15.     QAxObject * worksheets = workbook->querySubObject("WorkSheets");
  16.     int intCount = worksheets->property("Count").toInt();
  17.     for (int i = 1; i <= intCount; i++)
  18.     {
  19.         int intVal;
  20.         QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);
  21.         qDebug() << i << worksheet->property("Name").toString();
  22.         QAxObject * range = worksheet->querySubObject("Cells(1,1)");
  23.         intVal = range->property("Value").toInt();
  24.         range->setProperty("Value", QVariant(intVal+1));
  25.         QAxObject * range2 = worksheet->querySubObject("Range(C1)");
  26.         intVal = range2->property("Value").toInt();
  27.         range2->setProperty("Value", QVariant(intVal+1));
  28.     }
  29.     QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
  30.     QAxObject * usedrange = worksheet->querySubObject("UsedRange");
  31.     QAxObject * rows = usedrange->querySubObject("Rows");
  32.     QAxObject * columns = usedrange->querySubObject("Columns");
  33.     int intRowStart = usedrange->property("Row").toInt();
  34.     int intColStart = usedrange->property("Column").toInt();
  35.     int intCols = columns->property("Count").toInt();
  36.     int intRows = rows->property("Count").toInt();
  37.     for (int i = intRowStart; i < intRowStart + intRows; i++)
  38.     {
  39.         for (int j = intColStart; j <= intColStart + intCols; j++)
  40.         {
  41.             QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );
  42.             qDebug() << i << j << range->property("Value");
  43.         }
  44.     }
  45.     excel.setProperty("DisplayAlerts", 0);
  46.     workbook->dynamicCall("SaveAs (const QString&)", QString("c:/xlsbyqt.xls"));
  47.     excel.setProperty("DisplayAlerts", 1);
  48.     workbook->dynamicCall("Close (Boolean)", false);
  49.     excel.dynamicCall("Quit (void)");
  50.     return a.exec();
  51. }
执行的时候 会出现这样的错误
  1. CoCreateInstance failure (????????)
  2. QAxBase::setControl: requested control Excel.Application could not be instantiated
  3. QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed


离线roywillow

只看该作者 1楼 发表于: 2011-11-13
因为没用过QAx所以帮不了什么……
上网搜索一下那些提示看看?
或者找文档看看是不是哪些地方没做好
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
快速回复
限100 字节
 
上一个 下一个