QT 讀取 EXCEL XLS文件

轉載地址:http://blog.sina.com.cn/s/blog_6524fd1f01015hz1.html

.pro

CONFIG += axcontainer

 

#include <QAxObject>

 

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("g:\BFS.xls"))); 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();

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文件,excel打開成功,但是在用qDebug()輸出時出現問題
qDebug() << i << j << range->property("Value"); 總是顯示QVariant(QVariant,)
加上.toString()後,則顯示空字符串

 

不對qDebug()<<range->property("Value");
運行正確qDebug()<<cell->dynamicCall("Value2()").toString();

前者是針對Range,後者針對Cell
前者返回的是數組,後者不是

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章