首先在 工程文件里面添加:QT+=axcontainer(CONFIG += qaxcontainer已经被弃用建议不用)
常用方法:
1.QAxObject *excel = new QAxObject("Excel.Application"); //!建立excel操作对象,并连接Excel控件
2.excel->dynamicCall("SetVisible (bool Visible)", "false"); //! 设置为不显示窗体
更改 Excel 标题栏:
3.excel.setProperty("Caption", "Invoke Microsoft Excel");
添加新工作簿:
4.QAxObject * workbooks = excel.querySubObject("WorkBooks");
5.workbooks->dynamicCall("Add");
打开已存在的工作簿:
6.workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));
也可以:QAxObject* workbook = workbooks->querySubObject("Open(const QString&)",filename"");
保存工作簿
获取活动工作簿:
7.QAxObject * workbook = excel.querySubObject("ActiveWorkBook");(这里要要用excel,不能用workbooks)
获取所有的工作表:
8.QAxObject * worksheets = workbook->querySubObject("WorkSheets");
获取工作表数量:
9.int intCount = worksheets->property("Count").toInt();
获取第一个工作表:
10.QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
获取cell的值:
11.QAxObject * range = worksheet->querySubObje