Qt读取excel文件

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值