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
 */
}

你可能感兴趣的:(Excel)