QT操作Excel(通过QAxObject使用了OLE,前提是本地安装了Excel)

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于QT操作Excel(通过QAxObject使用了OLE,前提是本地安装了Excel)的主要内容,如果未能解决你的问题,请参考以下文章

Qt excel 操作使用说明

Qt导出Excel的简单实现

Qt ActiveX QAxObject 格式 Excel 单元格注释

Qt在windows 平台操作保存execel的表格(通过QAxObject来操作)

QT实现appendSheet(QAxObject的一种Add + Move的方法)

QT 使用第三方库QtXlsx操作Excel表