标签:
首先在.pro文件中加入: QT += axcontainer
再在头文件中包含<QAxObject>
QAxObject *used_range;//定义表格的使用范围
QAxObject *row;//定义表格的使用行
int row_start;//行开始
QAxObject *cell;//存放结果的单元格指针
QVariant cell_value;//单元格的值
QAxObject excel("Excel.Application");
excel.setProperty("Visible",true);//可见
QAxObject *work_books = excel.querySubObject("WorkBooks");
work_books->dynamicCall("Open(const QString)","D:\\plus.xlsx");
QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
QAxObject *work_sheets = work_book->querySubObject("Sheets");
QAxObject *first_sheet = work_sheets->querySubObject("Item(int)",1);//1代表第一张工作表,还可以是2,3
used_range = first_sheet->querySubObject("UsedRange");
row = used_range->querySubObject("Rows");
row_start = used_range->property("Row").toInt();
int row_count = row->property("Count").toInt();
double sum = 0;//用于累计和
for(int i=row_start;i<row_start+row_count;i++){
cell = first_sheet->querySubObject("Cells(int,int)",i,2);//计算第二列数据的和
cell_value = cell->property("Value");
sum = sum+cell_value.toDouble();
}
cell = first_sheet->querySubObject("Cells(int,int)",1,1);
cell->setProperty("Value",sum);//将结果放在(1,1)这个单元格中
work_book->dynamicCall("Save()");//保存文件
标签:
原文地址:http://www.cnblogs.com/littleqt/p/4979468.html