qt操作Excel com方式

//--------------------------------------------------------------------------------------------------
void OExcelWnd::openExcel()
{
	excel = new QAxObject("Excel.Application");
	excel->setProperty("UserControl", false);
	excel->setProperty("Visible", true);
	workbooks = excel->querySubObject("WorkBooks");
	workbook = workbooks->querySubObject("Open(QString, QVariant)", curFile);
	worksheets = workbook->querySubObject("WorkSheets");
	int sheetCount = worksheets->property("Count").toInt();
	worksheet = workbook->querySubObject("WorkSheets(int)", 1);

	//sheet选择框事件:Todo:不改变不触发
	connect(worksheet,SIGNAL(SelectionChange(IDispatch*)),this,SLOT(OnExcelSheetRangedClicked(IDispatch*)));
}

//--------------------------------------------------------------------------------------------------
void OExcelWnd::OnExcelSheetRangedClicked(IDispatch* selectedRange)
{


	QAxObject *userSelectedRange = new QAxObject((IUnknown*)selectedRange);
	QAxObject * rows = userSelectedRange->querySubObject("Rows");
	QAxObject * columns = userSelectedRange->querySubObject("Columns");
	int intRowCount = rows->property("Count").toInt();
	int intColCount = columns->property("Count").toInt();
	if(intRowCount == 1 && intColCount == 1)
	{
		int intRow = userSelectedRange->property("Row").toInt();
		int intCol = userSelectedRange->property("Column").toInt();

		QAxObject * cellRange = worksheet->querySubObject("Cells(int,int)",intRow,intCol);  
		QString cellValue = cellRange->dynamicCall("Value2()").toString(); 
		qDebug()<<cellValue;
		
	}

}



//--------------------------------------------------------------------------------------------------
void OExcelWnd::readExcel1()
{
	excel = new QAxObject("Excel.Application");
    excel->setProperty("Visible", false);
	workbooks = excel->querySubObject("WorkBooks");
	workbook = workbooks->querySubObject("Open(QString, QVariant)", curFile);
	worksheets = workbook->querySubObject("WorkSheets");
	int sheetCount = worksheets->property("Count").toInt();

	tabWidget->clear();
	for (int i = 1 ; i <= sheetCount; i++) {

		QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", i);
		QString sheetName = worksheet->property("Name").toString();


		QAxObject * usedrange = worksheet->querySubObject("UsedRange");
		QAxObject * rows = usedrange->querySubObject("Rows");
		QAxObject * columns = usedrange->querySubObject("Columns");
		int intRows = rows->property("Count").toInt();
		int intCols = columns->property("Count").toInt();

		int intRowStart = usedrange->property("Row").toInt();
		int intColStart = usedrange->property("Column").toInt();
		
		QTableWidget *onetable = new QTableWidget(intRows,intCols,this);	
		connect(onetable, SIGNAL(itemClicked(QTableWidgetItem*)),this, SLOT(OnTableWidgetItemClicked(QTableWidgetItem*)));
		onetable->setObjectName(sheetName);
		tabWidget->addTab(onetable, sheetName);

		
		for (int i = intRowStart; i < intRowStart + intRows; i++)  
		{
			for (int j = intColStart; j < intColStart + intCols; j++)  
			{

				QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );  
				QString itemValue = range->dynamicCall("Value2()").toString(); 
				QTableWidgetItem *newItem = new QTableWidgetItem(itemValue);
				onetable->setItem(i,j,newItem);
			}// end for
		}//end for
	}
}



		Test: shape嵌入在表中的图片和表,
		//QAxObject *shapes;
		//shapes = worksheet->querySubObject("Shapes");
		//int shapecount = shapes->property("Count").toInt();
		//qDebug()<<shapecount;

		//QAxObject *shape;
		//shape = worksheet->querySubObject("Shapes(int)",1);
		//QString shapename = shapes->property("Name").toString();
		//qDebug()<<shapename;
		//shape->querySubObject("IncrementLeft(int)",100);


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值