qt操作Excel com方式
[cpp]
//--------------------------------------------------------------------------------------------------
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
}
}
[cpp]
////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);
补充:软件开发 , C++ ,