大神们,给点力啊~关于qt 读取excel的问题

ff007bit 2011-01-15 11:14:09
    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();
/*获取excel内容*/
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()后,则显示空字符串
请问这是什么问题,怎么解决啊
代码是我借鉴网上的
...全文
591 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
coblan 2011-01-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yangglemu 的回复:]
这样取单元格的值:
qDebug()<<cell->dynamicCall("Value2()").toString();
[/Quote]
这个我一定要顶下,我也遇到这个问题,网上全是 property("Value")的方法,我按那个方法弄了一天了,查MSDN,也说Value 和Value2是property,也没什么问题,但是不管,怎么弄返回的都是空。
终于换了这个dynamicCall("Value2()")这个返回了正常的值。
同样,设置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))这样才成功的。。
我不知道网上的那么多例子用Property("Value")和SetProperty("Value")是怎么成功的。
  • 打赏
  • 举报
回复
MSDN详细的不得了
Excel.Range成员函数/属性详细
ff007bit 2011-01-16
  • 打赏
  • 举报
回复
我再研究研究~
您知道这方面的资料吗?帮推荐下~
  • 打赏
  • 举报
回复
前者是针对Range,后者针对Cell
前者返回的是数组,后者不是
ff007bit 2011-01-16
  • 打赏
  • 举报
回复
qDebug()<<range->property("Value");
qDebug()<<cell->dynamicCall("Value2()").toString();
为什么前一个不对?
谢谢~
刚刚试了,运行正确了
  • 打赏
  • 举报
回复
我就是觉得起cell这个名字意义更明确一点,和你那个range是一样的东西
ff007bit 2011-01-16
  • 打赏
  • 举报
回复
是用那个range是吧?
这两个有什么区别吗?
ff007bit 2011-01-16
  • 打赏
  • 举报
回复
cell是变量吗?需要自己设吗?
  • 打赏
  • 举报
回复
这样取单元格的值:
qDebug()<<cell->dynamicCall("Value2()").toString();

16,201

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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