qt excel


 

    /*
    QAxObject excel("Excel.Application");
    excel.setProperty("Visible", false);
    QAxObject *work_books = excel.querySubObject("WorkBooks");
    work_books->dynamicCall("Open(const QString&)", "d:\\XSpeed產品測試管理表.xls");
    excel.setProperty("Caption", "Qt Excel");
    QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
    QAxObject *work_sheets = work_book->querySubObject("Sheets"); //Sheets也可換用WorkSheets
    QAxObject *work_sheet = work_sheets->querySubObject("Item(1)",3);
    //操作單元格(第2行第2列)
    QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", 2, 2);
    cell->setProperty("Value", "Java C++ C# PHP Perl Python Delphi Ruby"); //設置單元格值
    cell->setProperty("RowHeight", 50); //設置單元格行高
    cell->setProperty("ColumnWidth", 30); //設置單元格列寬
    cell->setProperty("HorizontalAlignment", -4108); //左對齊(xlLeft):-4131 居中(xlCenter):-4108 右對齊(xlRight):-4152
    cell->setProperty("VerticalAlignment", -4108); //上對齊(xlTop)-4160 居中(xlCenter):-4108 下對齊(xlBottom):-4107
    cell->setProperty("WrapText", true); //內容過多,自動換行
    //cell->dynamicCall("ClearContents()"); //清空單元格內容
 
        
    QAxObject* interior = cell->querySubObject("Interior");
    interior->setProperty("Color", QColor(0, 255, 0)); //設置單元格背景色(綠色)
 
        
    QAxObject* border = cell->querySubObject("Borders");
    border->setProperty("Color", QColor(0, 0, 255)); //設置單元格邊框色(藍色)
 
        
    QAxObject *font = cell->querySubObject("Font"); //獲取單元格字體
    font->setProperty("Name", QStringLiteral("華文彩雲")); //設置單元格字體
    font->setProperty("Bold", true); //設置單元格字體加粗
    font->setProperty("Size", 20); //設置單元格字體大小
    font->setProperty("Italic", true); //設置單元格字體斜體
    font->setProperty("Underline", 2); //設置單元格下划線
    font->setProperty("Color", QColor(255, 0, 0)); //設置單元格字體顏色(紅色)
 
        
    //設置單元格內容,並合並單元格(第5行第3列-第8行第5列)
    QAxObject *cell_5_6 = work_sheet->querySubObject("Cells(int,int)", 2, 2);
    cell_5_6->setProperty("Value2", "Java"); //設置單元格值
    QAxObject *cell_8_5 = work_sheet->querySubObject("Cells(int,int)", 8, 5);
    cell_8_5->setProperty("Value2", "C++dd");
 
        
    QString merge_cell;
    merge_cell.append(QChar(3 - 1 + 'A')); //初始列
    merge_cell.append(QString::number(5)); //初始行
    merge_cell.append(":");
    merge_cell.append(QChar(5 - 1 + 'A')); //終止列
    merge_cell.append(QString::number(8)); //終止行
    QAxObject *merge_range = work_sheet->querySubObject("Range(const QString&)", merge_cell);
    merge_range->setProperty("HorizontalAlignment", -4108);
    merge_range->setProperty("VerticalAlignment", -4108);
    merge_range->setProperty("WrapText", true);
    merge_range->setProperty("MergeCells", true); //合並單元格
    //merge_range->setProperty("MergeCells", false); //拆分單元格
    work_book->dynamicCall("SaveAs(const QString&)", "E:\\test2.xlsx"); //另存為另一個文件
    //work_book->dynamicCall("Save()"); //保存文件(為了對比test與下面的test2文件,這里不做保存操作) work_book->dynamicCall("SaveAs(const QString&)", "E:\\test2.xlsx"); //另存為另一個文件
    work_book->dynamicCall("Close(Boolean)", false); //關閉文件
    excel.dynamicCall("Quit(void)"); //退出
 
        
 
        
    QAxObject *excel = NULL;
    QAxObject *workbooks = NULL;
    QAxObject *workbook = NULL;
 
        
    excel = new QAxObject("Excel.Application");
    if (!excel)
    {
        QMessageBox::critical(this, "錯誤信息", "EXCEL對象丟失");
        return;
    }
    excel->dynamicCall("SetVisible(bool)", false);
    workbooks = excel->querySubObject("WorkBooks");
    workbook = workbooks->querySubObject("Open(QString, QVariant)", QString(tr("d:\\XSpeed產品測試管理表.xls")));
    QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1);//打開第一個sheet
 
        
    //QAxObject * worksheet = workbook->querySubObject("WorkSheets");//獲取sheets的集合指針
    //int intCount = worksheet->property("Count").toInt();//獲取sheets的數量
    QAxObject *cell = worksheet->querySubObject("Cells(int,int)",6,3);
    cell->setProperty("Value2","測試管理");
    workbook->dynamicCall("Save()");
    QAxObject *cell2 = worksheet->querySubObject("Cells(int,int)",7,3);
 
        
    cell2->setProperty("Value2","方法");
 
        
    workbook->dynamicCall("Save()");
 
 
        
    QAxObject * usedrange = worksheet->querySubObject("UsedRange");//獲取該sheet的使用范圍對象
    QAxObject * rows = usedrange->querySubObject("Rows");
    QAxObject * columns = usedrange->querySubObject("Columns");
    //獲取行數和列數
    int intRowStart = usedrange->property("Row").toInt();
    int intColStart = usedrange->property("Column").toInt();
    int intCols = columns->property("Count").toInt();
    int intRows = rows->property("Count").toInt();
    //獲取excel內容
    for (int i = intRowStart; i < intRowStart + intRows; i++) //行
    {
        for (int j = intColStart; j < intColStart + intCols; j++) //列
        {
            QAxObject * cell = worksheet->querySubObject("Cells(int,int)", i, j ); //獲取單元格
            // qDebug() << i << j << cell->property("Value"); //*****************************出問題!!!!!!
            qDebug() << i << j <<cell->dynamicCall("Value2()").toString(); //正確
        }
    }
    workbook->dynamicCall("Close (Boolean)", false);
 
        
    //同樣,設置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))這樣才成功的。。
     excel->dynamicCall("Quit(void)");
    //excel->dynamicCall("Quit (void)");
    delete excel;//一定要記得刪除,要不線程中會一直打開excel.exe
 */
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM