Qt Excel com operations

Source: Internet
Author: User

[Cpp]
// Configure //--------------------------------------------------------------------------------------------------
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 select box event: Todo: not changed, not triggered
Connect (worksheet, SIGNAL (SelectionChange (IDispatch *), this, SLOT (OnExcelSheetRangedClicked (IDispatch *)));
}
 
// Configure //--------------------------------------------------------------------------------------------------
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;

}
 
}
 
 
 
// Configure //--------------------------------------------------------------------------------------------------
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
} // End
}
}


[Cpp]
//// Test: Images and tables embedded in the shape table,
// 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 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.