QT操作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());

 

阅读更多
个人分类: 跨平台-QT
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

QT操作Excel

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭