当前位置:   article > 正文

利用QAxObject简单操作Excel_qaxobject头文件

qaxobject头文件

需要在pro中添加QT += axcontainer,将模块引入

1、头文件copertexcelobj.h

  1. #ifndef COPERTEXCELOBJ_H
  2. #define COPERTEXCELOBJ_H
  3. #include <QObject>
  4. #include <QAxObject>
  5. #include <QDir>
  6. #include <QVariant>
  7. /*
  8. * 利用QAxObject实现对Excel的相关操作
  9. */
  10. class COpertExcelObj : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit COpertExcelObj(QObject *parent = nullptr);
  15. ~COpertExcelObj();
  16. void createrNewFile(const QString filename); //带路径的
  17. bool openFile(const QString filename); //带路径的
  18. QList<QVariantList> readSheetData(int index);
  19. QList<QVariantList> readSheetDataByName(QString name);
  20. QAxObject *getWorkSheet(int index);
  21. QAxObject *getWorkSheetByName(QString name);
  22. void setCellText(QAxObject *work_sheet, int row, int column, QString value);
  23. /*
  24. setCellValue fieldname 的值需要EXCEL支持才行,比如Color String
  25. */
  26. void setCellValue(QAxObject *work_sheet, int row, int column, QString fieldname, QVariant value);
  27. QAxObject *getCell(QAxObject *work_sheet, int row, int column);
  28. private:
  29. QAxObject *m_excelobj;
  30. QAxObject *m_workbooks;
  31. };
  32. #endif // COPERTEXCELOBJ_H

2、cpp文件copertexcelobj.cpp

  1. #include "copertexcelobj.h"
  2. #include <QDebug>
  3. COpertExcelObj::COpertExcelObj(QObject *parent)
  4. : QObject{parent}
  5. {
  6. m_excelobj = new QAxObject();
  7. if (!m_excelobj->setControl("Excel.Application")) //连接Excel控件
  8. {
  9. m_excelobj->setControl("ket.Application"); //??wps??
  10. }
  11. m_excelobj->setProperty("Visible", false);//显示窗体看效果
  12. m_excelobj->setProperty("DisplayAlerts", false);//显示警告看效果
  13. m_excelobj->setProperty("Caption", "Qt Excel"); //标题为Qt Excel
  14. m_workbooks = m_excelobj->querySubObject("WorkBooks");
  15. /*
  16. QAxObject* workbook = workbooks->querySubObject("Open(const QString&)",QDir::toNativeSeparators(ExcelFile) ); //打开
  17. QAxObject *work_book = excel->querySubObject("ActiveWorkBook");
  18. QAxObject *worksheet = work_book->querySubObject("Sheets(int)",1); //获取表单1
  19. Excel_SetCell(worksheet,2,2,QColor(74,51,255),"12345"); //设置B2单元格内容为12345
  20. workbook->dynamicCall("Save()" );
  21. */
  22. }
  23. COpertExcelObj::~COpertExcelObj()
  24. {
  25. m_workbooks->dynamicCall("Close(Boolean)", false); //关闭文件
  26. m_excelobj->dynamicCall("Quit(void)"); //退出
  27. delete m_excelobj;
  28. m_excelobj = nullptr;
  29. }
  30. void COpertExcelObj::createrNewFile(const QString filename)
  31. {
  32. QAxObject *workbook = m_workbooks->querySubObject("Open(const QString&)",QDir::toNativeSeparators(filename) );
  33. }
  34. bool COpertExcelObj::openFile(const QString filename)
  35. {
  36. QAxObject *workbook = m_workbooks->querySubObject("Open(const QString&)",QDir::toNativeSeparators(filename) );
  37. if (workbook) return true;
  38. return false;
  39. }
  40. QList<QVariantList> COpertExcelObj::readSheetData(int index)
  41. {
  42. QList<QVariantList> rlist;
  43. QAxObject *work_book = m_excelobj->querySubObject("ActiveWorkBook");
  44. QAxObject *work_sheet = work_book->querySubObject("Sheets(int)",index); //获取表单1
  45. if (work_sheet == nullptr) return rlist;
  46. QString work_sheet_name = work_sheet->property("Name").toString(); //获取工作表名称
  47. QAxObject *used_range = work_sheet->querySubObject("UsedRange");
  48. QAxObject *rows = used_range->querySubObject("Rows");
  49. QAxObject *columns = used_range->querySubObject("Columns");
  50. int row_start = used_range->property("Row").toInt(); //获取起始行
  51. int column_start = used_range->property("Column").toInt(); //获取起始列
  52. int row_count = rows->property("Count").toInt(); //获取行数
  53. int column_count = columns->property("Count").toInt(); //获取列数
  54. for (int i = row_start; i < row_count; ++i)
  55. {
  56. QVariantList rowlist;
  57. for (int j = column_start; j < column_count; ++j)
  58. {
  59. QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", i, j);
  60. QVariant cell_value = cell->property("Value"); //获取单元格内容
  61. rowlist << cell_value;
  62. }
  63. rlist << rowlist;
  64. }
  65. return rlist;
  66. }
  67. QList<QVariantList> COpertExcelObj::readSheetDataByName(QString name)
  68. {
  69. QList<QVariantList> rlist;
  70. QAxObject *work_book = m_excelobj->querySubObject("ActiveWorkBook");
  71. QAxObject *work_sheet = work_book->querySubObject("Sheets(string)",name); //获取表单1
  72. if (work_sheet == nullptr) return rlist;
  73. QString work_sheet_name = work_sheet->property("Name").toString(); //获取工作表名称
  74. QAxObject *used_range = work_sheet->querySubObject("UsedRange");
  75. QAxObject *rows = used_range->querySubObject("Rows");
  76. QAxObject *columns = used_range->querySubObject("Columns");
  77. int row_start = used_range->property("Row").toInt(); //获取起始行
  78. int column_start = used_range->property("Column").toInt(); //获取起始列
  79. int row_count = rows->property("Count").toInt(); //获取行数
  80. int column_count = columns->property("Count").toInt(); //获取列数
  81. for (int i = row_start; i < row_count; ++i)
  82. {
  83. QVariantList rowlist;
  84. for (int j = column_start; j < column_count+1; ++j)
  85. {
  86. QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", i, j);
  87. QVariant cell_value = cell->property("Value"); //获取单元格内容
  88. // if (cell_value.type() == QVariant::Invalid)
  89. // {
  90. // cell_value = "";
  91. // }
  92. rowlist << cell_value;
  93. }
  94. rlist << rowlist;
  95. }
  96. return rlist;
  97. }
  98. QAxObject *COpertExcelObj::getWorkSheet(int index)
  99. {
  100. QAxObject *work_book = m_excelobj->querySubObject("ActiveWorkBook");
  101. QAxObject *work_sheet = work_book->querySubObject("Sheets(int)",index); //获取表单1
  102. return work_sheet;
  103. }
  104. QAxObject *COpertExcelObj::getWorkSheetByName(QString name)
  105. {
  106. QAxObject *work_book = m_excelobj->querySubObject("ActiveWorkBook");
  107. QAxObject *work_sheet = work_book->querySubObject("Sheets(string)",name); //获取表单1
  108. return work_sheet;
  109. }
  110. void COpertExcelObj::setCellText(QAxObject *work_sheet, int row, int column, QString value)
  111. {
  112. if (work_sheet == nullptr) return;
  113. QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", row, column);
  114. cell->setProperty("Value", value);
  115. }
  116. void COpertExcelObj::setCellValue(QAxObject *work_sheet, int row, int column, QString valuefield, QVariant value)
  117. {
  118. if (work_sheet == nullptr) return;
  119. QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", row, column);
  120. cell->setProperty(valuefield.toLocal8Bit().constData(), value);
  121. }
  122. QAxObject *COpertExcelObj::getCell(QAxObject *work_sheet, int row, int column)
  123. {
  124. QAxObject *cell = nullptr;
  125. if (work_sheet == nullptr) return cell;
  126. cell = work_sheet->querySubObject("Cells(int,int)", row, column);
  127. return cell;
  128. }

3、使用案例

  1. QString ExcelFile = "E://test12.xlsx";
  2. COpertExcelObj obj;
  3. obj.openFile(ExcelFile);
  4. //obj.readSheetData(1);
  5. QList<QVariantList> dataList = obj.readSheetDataByName("Sheet1"); //读取Sheet1的表单数据

其他C++读写Excel有许多开源库_缘如风的博客-CSDN博客_c++ excel库

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/170553?site
推荐阅读
相关标签
  

闽ICP备14008679号