.pro
CONFIG += axcontainer
#include
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()<property("Value");
运行正确qDebug()<dynamicCall("Value2()").toString();
前者是针对Range,后者针对Cell
前者返回的是数组,后者不是