QT操作Excel

标签: qtexcelchartsstringreport
2939人阅读 评论(1) 收藏 举报
分类:

新建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());

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1239791次
    • 积分:14578
    • 等级:
    • 排名:第803名
    • 原创:134篇
    • 转载:82篇
    • 译文:119篇
    • 评论:216条
    博客专栏
    C++与QT

    文章:10篇

    阅读:113079
    最新评论