QT操作Excel(通过QAxObject使用了OLE,前提是本地安装了Excel)
2016-03-18 00:13
656 查看
新建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
#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
相关文章推荐
- DOM (Document Object Model)文档对象模型
- 利用JsonObject解析Json数据
- Gson 解析时候报错: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
- BOM (Browser Object Model) 浏览器对象模型
- Objective-C中的集合类
- iOS开发系列--Objective-C之协议、代码块、分类
- Java Object有哪些公用方法?
- objective c关于下划线和双下划线的用法
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
- iOS 中C如何调用Objective c的代码更新界面问题解决
- 论文阅读笔记:You Only Look Once: Unified, Real-Time Object Detection
- Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
- Parcelable encountered ClassNotFoundException reading a Serializable object bug解析
- Objective-C的Method Swizzle、对象模型、消息机制、消息转发的详解
- enumerateObjectsUsingBlock
- 从Map、JSONObject取不存在键值对时的异常情况
- Object类
- Objective-C 【对于@property功能的新认识(setter、getter)】
- Objective-C中 ==、isEqual、isEqualToString判断字符串相等
- 转载_理解 Objective-C Runtime