程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> qt操作Excel com方式

qt操作Excel com方式

編輯:C++入門知識

[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); 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved