QT操作Excel(通过QAxObject使用了OLE,前提是本地安装了Excel)
新建QT GUI项目,在选择选项中勾选ActiveQT Container.
#include <qaxobject.h>
QAxObject *obj = new QAxObject("Excel.Application");
obj->setProperty("Visible", true);
obj->setProperty("Caption", "Hello world");
QAxObject *workBooks = obj->querySubObject("Workbooks");
//打开已存的excel文件
//QAxObject *workBook = workBooks->querySubObject("Open(QString)", "D:\\QTDemo\\OpExcel\\Debug\\1.xls");
workBooks->dynamicCall("Add");
QAxObject *workBook = workBooks->querySubObject("Item(const int)", 1);
QAxObject *sheets = workBook->querySubObject("Sheets");
QAxObject *sheet = sheets->querySubObject("Item(int)", 1);
QAxObject *range = sheet->querySubObject("Range(const QVariant&)", QVariant(QString("A1:A1")));
range->dynamicCall("Clear()");
range->dynamicCall("SetValue(const QVariant&)", QVariant(5));
obj->dynamicCall("SetScreenUpdating(bool)", true);
在Excel中插入图表
QAxObject *excel = new QAxObject("Excel.Application", 0);
excel->setProperty("SheetsInNewWorkbook", 1);
excel->setProperty("Visible", true);
QAxObject *workbooks = excel->querySubObject("Workbooks");
QAxObject *workbook = workbooks->querySubObject("Add");
QAxObject *worksheet = workbook->querySubObject("Worksheets (int)", 1);
worksheet->setProperty("Name","Dati applicazione");
QAxObject *cell = worksheet->querySubObject("Cells(int,int)", 1, 1);
cell->dynamicCall("SetValue(String)", "Serie");
cell = worksheet->querySubObject("Cells(int,int)", 1, 2);
cell->dynamicCall("SetValue(String)", "Dati");
for (int i = 2; i < 10; i++){
cell = worksheet->querySubObject("Cells(int,int)", i, 1);
cell->dynamicCall("SetValue(int)", i - 1);
cell = worksheet->querySubObject("Cells(int,int)", i, 2);
cell->dynamicCall("SetValue(double)", qrand());
}
QAxObject *charts = workbook->querySubObject("Charts");
QAxObject *chart = charts->querySubObject("Add");
chart->setProperty("Name", "Report Grafico dei dati");
chart->setProperty("ChartType", 73);
QAxObject * serie = chart->querySubObject("SeriesCollection(int)", 1);
QAxObject * xvalues = worksheet->querySubObject("Range(A2:A9)");
QAxObject * yvalues = worksheet->querySubObject("Range(B2:B9)");
serie->setProperty("XValues", xvalues->asVariant());
serie->setProperty("Values", yvalues->asVariant());
http://blog.csdn.net/henreash/article/details/7397774
---------------------------------------------------------------------------------------------------------------------
QAxWidget excel("Excel.Application");
1) 显示当前窗口:
excel.setProperty("Visible", true);
2) 更改 Excel 标题栏:
excel.setProperty("Caption", "Invoke Microsoft Excel");
3) 添加新工作簿:
QAxObject * workbooks = excel.querySubObject("WorkBooks");
workbooks->dynamicCall("Add");
4) 打开已存在的工作簿:
workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));
5) 获取活动工作簿:
QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
6) 获取所有的工作表:
QAxObject * worksheets = workbook->querySubObject("WorkSheets");
7) 获取工作表数量:
int intCount = worksheets->property("Count").toInt();
8) 获取第一个工作表:
QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
9) 获取cell的值:
QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, 1 );
示例代码:

#include <QtGui>
#include <QAxObject>
#include <QAxWidget>
#include <qaxselect.h> //备注:在我(GUOBBS,不是原作者)的测试中,已删掉此行
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();
}

以上文章来源:http://blog.csdn.net/tingsking18/article/details/5677353
补充说明:
工程*.pro的文件需要加上下面这行以加载COM组件
CONFIG += qaxcontainer
http://www.cnblogs.com/guobbs/p/3915602.html
QT操作Excel(通过QAxObject使用了OLE,前提是本地安装了Excel)的更多相关文章
- Qt 操作Excel
Qt对Excel的数据读/写操作没有现存的类,需要使用QAxObject,下面是从网上下载下来的一个封装好的类,感觉还可以,一般情况下够用,拿来给大家分享. 头文件: #ifndef EXCELENG ...
- QT操作EXCEL
介绍一下最基本的QT对EXCEL的读写操作. 声明:转载于:http://blog.csdn.net/czyt1988/article/details/52121360 在使用QT的操作数据库的时候, ...
- qt 操作excel表格
自己编写的一个Qt C++类,用于操作excel表格,在Qt中操作excel需在.pro中增加CONFIG+=qaxcontainer配置. 1.打开Excel:objExcel = new QAx ...
- QT 操作 excel 教程
前言:环境 win7 64位,QT4.8.5,QT Creator 在 .pro 文件中加入语句"CONFIG+=qaxcontainer"; 源码如下: //main.cpp # ...
- qt操作excel報錯解決
如果電腦上沒有office,需要判斷,否则,会报错 onecore\com\combase\catalog\catalog.cxx()\combase.dll!00007FFF1DF823CB: (c ...
- QT 操作数据库
整理一下 QT 操作数据库的一些要点,以备以后的查询学习(主要是操作 mysql ). 首先,要查询相关的驱动是否已经装好了,可以用以下的程序进行验证: #include <QtCore/QCo ...
- 不依赖Excel是否安装的Excel导入导出类
本文利用第三方开源库NPOI实现Excel97-2003,Excel2007+的数据导入导出操作. 不依赖Office是否安装.NPOI开源项目地址:http://npoi.codeplex.com/ ...
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制 如果您使用的电脑要操作的是office2 ...
- NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx 格式
下载地址 http://npoi.codeplex.com/releases 下面放一个 NPOIHelper 助手类吧,也不是我写的- NpoiExcelHelper 可以生成xlsx格式publi ...
随机推荐
- [转]PostgreSQL 中文资料汇总
原文链接:http://francs3.blog.163.com/blog/static/405767272014017341219/ --1 中文社区网站 PostgreSQL 中文社区官网: h ...
- mysql tcp 4层负载
-bash-4.1# cat /etc/haproxy/haproxy.cfg global log 127.0.0.1 local3 maxconn 65535 chroot /usr/local/ ...
- Java的位运算符具体解释实例——与(&)、非(~)、或(|)、异或(^)
位运算符主要针对二进制,它包含了:“与”.“非”.“或”.“异或”.从表面上看似乎有点像逻辑运算符,但逻辑运算符是针对两个关系运算符来进行逻辑运算,而位运算符主要针对两个二进制数的位进行逻辑运算.以下 ...
- Ubuntu14.04更新源
Ubuntu14.04更新源 http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html Ubuntu源 http://wiki.ub ...
- iTunes Store:隐藏和取消隐藏已购项目
使用 Mac 或 PC 上的 iTunes 来隐藏或取消隐藏已购项目. 如何隐藏已购项目 在 Mac 或 PC 上打开 iTunes. 从 Store 菜单中,选取商店 > 登录,然后输入您的 ...
- __autoload()方法
php中__autoload()方法详解 PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例化100个对象,那么你必须用include或者require包含进来100个 ...
- js数组基础整理
首页: 主要整理了一下数组中常用的一些基础知识,代码都是自己手敲,有不对的地方希望能指出,目前只有4篇,后续会不断的增加这一板块. 由于少于100字不能发所以把一些最基本的创建数组也写上. // 创建 ...
- Axure RP中线条的设置
文章来源与网络 来自:非原型不设计
- android studio之argument for @notnull parameter 'name'
1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开.2)在idea.properties文件末尾添加一行: disable.and ...
- 基于visual Studio2013解决C语言竞赛题之1054抽牌游戏
题目 解决代码及点评 /************************************************************************/ /* 54 ...