QT 操作excel

[cpp]  view plain  copy
 print ?
  1. //--------------------------------------------------------------------------------------------------  
  2. void OExcelWnd::openExcel()  
  3. {  
  4.     excel = new QAxObject("Excel.Application");  
  5.     excel->setProperty("UserControl"false);  
  6.     excel->setProperty("Visible"true);  
  7.     workbooks = excel->querySubObject("WorkBooks");  
  8.     workbook = workbooks->querySubObject("Open(QString, QVariant)", curFile);  
  9.     worksheets = workbook->querySubObject("WorkSheets");  
  10.     int sheetCount = worksheets->property("Count").toInt();  
  11.     worksheet = workbook->querySubObject("WorkSheets(int)", 1);  
  12.   
  13.     //sheet选择框事件:Todo:不改变不触发  
  14.     connect(worksheet,SIGNAL(SelectionChange(IDispatch*)),this,SLOT(OnExcelSheetRangedClicked(IDispatch*)));  
  15. }  
  16.   
  17. //--------------------------------------------------------------------------------------------------  
  18. void OExcelWnd::OnExcelSheetRangedClicked(IDispatch* selectedRange)  
  19. {  
  20.   
  21.   
  22.     QAxObject *userSelectedRange = new QAxObject((IUnknown*)selectedRange);  
  23.     QAxObject * rows = userSelectedRange->querySubObject("Rows");  
  24.     QAxObject * columns = userSelectedRange->querySubObject("Columns");  
  25.     int intRowCount = rows->property("Count").toInt();  
  26.     int intColCount = columns->property("Count").toInt();  
  27.     if(intRowCount == 1 && intColCount == 1)  
  28.     {  
  29.         int intRow = userSelectedRange->property("Row").toInt();  
  30.         int intCol = userSelectedRange->property("Column").toInt();  
  31.   
  32.         QAxObject * cellRange = worksheet->querySubObject("Cells(int,int)",intRow,intCol);    
  33.         QString cellValue = cellRange->dynamicCall("Value2()").toString();   
  34.         qDebug()<<cellValue;  
  35.           
  36.     }  
  37.   
  38. }  
  39.   
  40.   
  41.   
  42. //--------------------------------------------------------------------------------------------------  
  43. void OExcelWnd::readExcel1()  
  44. {  
  45.     excel = new QAxObject("Excel.Application");  
  46.     excel->setProperty("Visible"false);  
  47.     workbooks = excel->querySubObject("WorkBooks");  
  48.     workbook = workbooks->querySubObject("Open(QString, QVariant)", curFile);  
  49.     worksheets = workbook->querySubObject("WorkSheets");  
  50.     int sheetCount = worksheets->property("Count").toInt();  
  51.   
  52.     tabWidget->clear();  
  53.     for (int i = 1 ; i <= sheetCount; i++) {  
  54.   
  55.         QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", i);  
  56.         QString sheetName = worksheet->property("Name").toString();  
  57.   
  58.   
  59.         QAxObject * usedrange = worksheet->querySubObject("UsedRange");  
  60.         QAxObject * rows = usedrange->querySubObject("Rows");  
  61.         QAxObject * columns = usedrange->querySubObject("Columns");  
  62.         int intRows = rows->property("Count").toInt();  
  63.         int intCols = columns->property("Count").toInt();  
  64.   
  65.         int intRowStart = usedrange->property("Row").toInt();  
  66.         int intColStart = usedrange->property("Column").toInt();  
  67.           
  68.         QTableWidget *onetable = new QTableWidget(intRows,intCols,this);      
  69.         connect(onetable, SIGNAL(itemClicked(QTableWidgetItem*)),this, SLOT(OnTableWidgetItemClicked(QTableWidgetItem*)));  
  70.         onetable->setObjectName(sheetName);  
  71.         tabWidget->addTab(onetable, sheetName);  
  72.   
  73.           
  74.         for (int i = intRowStart; i < intRowStart + intRows; i++)    
  75.         {  
  76.             for (int j = intColStart; j < intColStart + intCols; j++)    
  77.             {  
  78.   
  79.                 QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );    
  80.                 QString itemValue = range->dynamicCall("Value2()").toString();   
  81.                 QTableWidgetItem *newItem = new QTableWidgetItem(itemValue);  
  82.                 onetable->setItem(i,j,newItem);  
  83.             }// end for  
  84.         }//end for  
  85.     }  
  86. }  



[cpp]  view plain  copy
 print ?
  1. Test: shape嵌入在表中的图片和表,  
  2. //QAxObject *shapes;  
  3. //shapes = worksheet->querySubObject("Shapes");  
  4. //int shapecount = shapes->property("Count").toInt();  
  5. //qDebug()<<shapecount;  
  6.   
  7. //QAxObject *shape;  
  8. //shape = worksheet->querySubObject("Shapes(int)",1);  
  9. //QString shapename = shapes->property("Name").toString();  
  10. //qDebug()<<shapename;  
  11. //shape->querySubObject("IncrementLeft(int)",100);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值