Qt读取excel文件

  1. 云栖社区>
  2. 博客列表>
  3. 正文

Qt读取excel文件

this_is_bill 2015-04-05 00:10:00 浏览2010 评论0

摘要: RT,Qt读取excel #include "xlsIO.h" #include <ActiveQt/QAxWidget> #include <ActiveQt/QAxObject> #include <QVariant> #include <QFileDialog> #include <Q...

RT,Qt读取excel


#include "xlsIO.h"
#include <ActiveQt/QAxWidget>
#include <ActiveQt/QAxObject>
#include <QVariant>
#include <QFileDialog>
#include <QMainWindow>
#include <QTextCodec>
#include <QMessageBox>
#include <qDebug>
#include <vector>
#include "ui_teacherassistant.h"

using namespace std;

bool XlsIO::xlsReader(QString excelPath,vector<string> &stuNames)
{
	QAxObject excel("Excel.Application");
	excel.setProperty("Visible", false);
	QAxObject *work_books = excel.querySubObject("WorkBooks");
	work_books->dynamicCall("Open (const QString&)", excelPath);
	QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
	QAxObject *work_sheets = work_book->querySubObject("Sheets");  //Sheets也可换用WorkSheets
	int sheet_count = work_sheets->property("Count").toInt();  //获取工作表数目

	if(sheet_count > 0)
	{
		QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", 1);
		QAxObject *used_range = work_sheet->querySubObject("UsedRange");
		QAxObject *rows = used_range->querySubObject("Rows");
		//QAxObject *columns = used_range->querySubObject("Columns");
		int row_start = used_range->property("Row").toInt();  //获取起始行:1
		//int column_start = used_range->property("Column").toInt();  //获取起始列
		int row_count = rows->property("Count").toInt();  //获取行数
		//int column_count = columns->property("Count").toInt();  //获取列数

		QString StudentName;
		for(int i=row_start+4; i<=row_count;i++)
		{
			QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", i, 3);
			StudentName = cell->dynamicCall("Value2()").toString();//获取单元格内容
// 			cell = work_sheet->querySubObject("Cells(int,int)", i, 3);
// 			StudentNum[i-1] = cell->dynamicCall("Value2()").toString();//获取(i,3)

			stuNames.push_back(StudentName.toStdString());
		}
	}

	work_books->dynamicCall("Close()");//关闭工作簿
	excel.dynamicCall("Quit()");//关闭excel

	return true;
}


用云栖社区APP,舒服~

【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口  详情请点击

网友评论

this_is_bill
文章436篇 | 关注12
关注
面向阿里云ECS实例、HPC和Docker的文件存储服务。 查看详情
操作审计(ActionTrail)会记录您的云账户资源操作,提供操作记录查询,并可以将记录文... 查看详情
支持MySQL、SQL Server、PostgreSQL、MongoDB、Redis等关系... 查看详情
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效... 查看详情
阿里云总监课正式启航

阿里云总监课正式启航