当前位置:   article > 正文

Qt读取word文档_qt读取word标题

qt读取word标题

为了记录一下

  1. CWordOperate::CWordOperate( const QString filename,QObject *parent /*= NULL*/ )
  2. : QObject(parent)
  3. {
  4. m_word = new QAxWidget("Word.Application");
  5. //m_doc->generateDocumentation (); //导出支持的函数以及相关属性
  6. m_document = m_word->querySubObject("Documents");
  7. m_document->dynamicCall("Open(const QString&)",filename);
  8. m_word->setProperty("Visible",QVariant(false)); //不显示word窗口
  9. m_doc = m_word->querySubObject("ActiveDocument");//获取当前工作簿
  10. bool ok2 = m_word->property("Visible").toBool();
  11. m_word->setProperty("Visible",QVariant(false));
  12. //getLine(0,13); //标题
  13. //getLine(14,34); //单位
  14. QString s = getLine(79,87);
  15. qDebug() << s;
  16. QString alltext = getAllText();
  17. QString teststr = QString::fromUtf8("");
  18. alltext = alltext.remove(teststr);
  19. //qDebug() << alltext;
  20. QStringList liststr = alltext.split("\r");
  21. int start = 0;
  22. foreach (QString sttr, liststr)
  23. {
  24. sttr = sttr.remove(teststr);
  25. qDebug() << sttr;
  26. }
  27. }
  28. void CWordOperate::readTables()
  29. {
  30. if (NULL == m_doc) return;
  31. QAxObject* tables = m_doc->querySubObject("Tables"); //获取所有表格
  32. //QAxBase::PropertyBag p = m_document->propertyBag();
  33. int tablecount = 1;
  34. if (NULL != tables)
  35. {
  36. tablecount = tables->dynamicCall("Count").toInt(); //获取表格个数
  37. delete tables;
  38. tables = NULL;
  39. }
  40. for (int i = 1; i < tablecount+1; ++i)
  41. {
  42. QAxObject *table = m_doc->querySubObject("Tables(int)",i); //获取某个表格
  43. if (NULL ==table) continue;
  44. int row = table->querySubObject("Rows")->dynamicCall("Count").toInt();
  45. int col = table->querySubObject("Columns")->dynamicCall("Count").toInt();
  46. QAxBase::PropertyBag p = table->propertyBag();
  47. for (int j = 0; j < row; ++j)
  48. {
  49. for (int z = 0; z < col+1; ++z)
  50. {
  51. QAxObject *cell = table->querySubObject("Cell(int,int)",j,z); //获取表格数据
  52. if (NULL ==cell) continue;
  53. QString sp = cell->querySubObject("Range")->property("Text").toString();
  54. qDebug() << j << " " << z << " " << sp;
  55. delete cell;
  56. cell = NULL;
  57. }
  58. }
  59. delete table;
  60. table = NULL;
  61. }
  62. }
  63. QString CWordOperate::getAllText()
  64. {
  65. QString text;
  66. if (NULL == m_doc)
  67. return text;
  68. QAxObject *pRange = m_doc->querySubObject("Range()");
  69. if (NULL != pRange)
  70. {
  71. text = pRange->property("Text").toString();
  72. delete pRange; //要释放内存,不然文档就被占用
  73. pRange = NULL;
  74. }
  75. return text;
  76. }
  77. QString CWordOperate::getLine(int start, int end )
  78. {
  79. QString text;
  80. if (NULL == m_doc)
  81. return text;
  82. QVariantList params;
  83. params << start << end;
  84. QAxObject *pRange = m_doc->querySubObject("Range(QVariant&, QVariant&)",params);
  85. text = pRange->property("Text").toString();
  86. delete pRange;
  87. pRange = NULL;
  88. return text;
  89. }
  90. QString CWordOperate::getTableItem( int tableindex,int row,int column )
  91. {
  92. QString restr;
  93. if (NULL == m_doc) return restr;
  94. //QAxObject* tables = m_doc->querySubObject("Tables"); //获取所有表格
  95. QAxObject *table = m_doc->querySubObject("Tables(int)",tableindex); //获取某个表格
  96. if (NULL ==table) return restr;
  97. //int row = table->querySubObject("Rows")->dynamicCall("Count").toInt();
  98. //int col = table->querySubObject("Columns")->dynamicCall("Count").toInt();
  99. QAxObject *cell = table->querySubObject("Cell(int,int)",row,column); //获取表格数据
  100. if (NULL ==cell) return restr;
  101. restr = cell->querySubObject("Range")->property("Text").toString();
  102. return restr;
  103. }

 

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

闽ICP备14008679号