ExportReportManager.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "ExportReportManager.h"
  2. #include "ProjectManager.h"
  3. #include <dbService/ClassSet.h>
  4. #include <dbService/SchemeInfoService.h>
  5. #include <QWord/QWord.h>
  6. #include <QWord/QWordDemo.h>
  7. #include <QDir>
  8. #include <QDate>
  9. #include <QLabel>
  10. #include <QDebug>
  11. ExportReportManager::ExportReportManager(QObject *parent) : QObject(parent)
  12. {
  13. m_word = new QWord(this);
  14. }
  15. ExportReportManager::~ExportReportManager()
  16. {
  17. delete m_word;
  18. }
  19. ProjectInfo *ExportReportManager::proj() const
  20. {
  21. return m_proj;
  22. }
  23. void ExportReportManager::setProj(ProjectInfo *proj)
  24. {
  25. m_proj = proj;
  26. }
  27. int ExportReportManager::evalType() const
  28. {
  29. return m_evalType;
  30. }
  31. void ExportReportManager::setEvalType(int type)
  32. {
  33. m_evalType = type;
  34. }
  35. bool ExportReportManager::exportReport()
  36. {
  37. bool createRet = createWord();
  38. if (createRet == false) {
  39. qDebug() << __FUNCTION__ << __LINE__ << m_word->getStrErrorInfo() << endl;
  40. return false;
  41. }
  42. insertCommonInfo();
  43. if (m_evalType == ProjectManager::OptimalEval) {
  44. insertSchemeDesignInfo();
  45. insertSchemeIndex();
  46. insertSchemeData();
  47. }
  48. m_word->setVisible(true);
  49. QAxObject *doc = m_word->getWordApp()->querySubObject("ActiveDocument");
  50. doc->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(m_word->getFilePath()));
  51. m_word->close();
  52. return true;
  53. }
  54. bool ExportReportManager::createWord()
  55. {
  56. delete m_word;
  57. m_word = new QWord(this);
  58. // 报告文件夹路径
  59. QString curPath = QDir::currentPath();
  60. QString dateStr = QDate::currentDate().toString("yyyy-MM-dd");
  61. QString reportDir = curPath + "/reports/" + dateStr + "/";
  62. QDir dir(reportDir);
  63. if (!dir.exists()) {
  64. dir.mkpath(reportDir);
  65. }
  66. // 根据模板创建 word 文档
  67. QString typeStr = ProjectManager::reportNameOfEvalType((ProjectManager::EvalType)m_evalType);
  68. QString tempPath = "E:/qfd/code/QFD2/bin/report_templates/" + typeStr + ".docx";
  69. if (!QFile::exists(tempPath)) {
  70. qDebug() << __FUNCTION__ << __LINE__ << "Report template not found" << endl;
  71. return false;
  72. }
  73. QString fileName = m_proj->projectName + "_" + typeStr + ".docx";
  74. QString filePath = reportDir + fileName;
  75. if (QFile::exists(filePath)) {
  76. QFile::remove(filePath);
  77. }
  78. bool ret = QFile::copy(tempPath, filePath);
  79. if (ret) {
  80. ret = m_word->open(filePath, false);
  81. } else {
  82. qDebug() << __FUNCTION__ << __LINE__ << "Copy report template failed" << endl;
  83. }
  84. return ret;
  85. }
  86. void ExportReportManager::insertCommonInfo()
  87. {
  88. insertText("bmProjName", m_proj->projectName);
  89. insertText("bmProjUnit", m_proj->estimateDept);
  90. QString dateStr = QDate::currentDate().toString("yyyy年MM月");
  91. insertText("bmProjDate", dateStr);
  92. insertText("bmEvalPurpose", m_proj->estimateObjective);
  93. }
  94. void ExportReportManager::insertSchemeDesignInfo()
  95. {
  96. QList<SchemaEval *> schemeList;
  97. bool ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, 0);
  98. if (ret == false) {
  99. qDebug() << __FUNCTION__ << __LINE__ << "query" << endl;
  100. return;
  101. }
  102. for (int i = schemeList.size() - 1; i >= 0; --i) {
  103. // 插入图片
  104. QAxObject *bmSchemes = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmSchemes");
  105. if (bmSchemes != nullptr) {
  106. bmSchemes->dynamicCall("Select(void)");
  107. QAxObject *range = bmSchemes->querySubObject("Range");
  108. QAxObject *inlineShapes = range->querySubObject("InlineShapes");
  109. QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", schemeList[i]->filePath);
  110. if (shape != nullptr) {
  111. shape->setProperty("Width", 200);
  112. shape->setProperty("Height", 200);
  113. }
  114. }
  115. m_word->insertMoveDown();
  116. // 插入图片标题
  117. QString remark = schemeList[i]->remark;
  118. int picTtlIndex = remark.lastIndexOf("(图");
  119. QString subStr = remark.mid(picTtlIndex + 1, 8);
  120. QString picTtl = "";
  121. if (subStr.contains(")")) {
  122. picTtl = subStr.split(")").first();
  123. }
  124. m_word->setFontName(QString::fromLocal8Bit("黑体"));
  125. m_word->setFontSize(9);
  126. m_word->setParagraphAlignment(0);
  127. m_word->insertText(picTtl + " " + "阵地位置图");
  128. m_word->insertMoveDown();
  129. // 插入方案描述
  130. insertText("bmSchemes", schemeList[i]->name + ":" + schemeList[i]->remark);
  131. if (i != 0) {
  132. m_word->insertMoveDown();
  133. }
  134. }
  135. }
  136. void ExportReportManager::insertSchemeIndex()
  137. {
  138. insertText("bmProjName31", m_proj->projectName);
  139. insertText("bmProjName32", m_proj->projectName);
  140. // bmIndexTable
  141. QAxObject *table = insertTable("bmIndexTable", 15, 5);
  142. if (table == nullptr) {
  143. return;
  144. }
  145. m_word->setFontName(QString::fromLocal8Bit("仿宋_GB2312"));
  146. m_word->setFontSize(9);
  147. m_word->setParagraphAlignment(0);
  148. setCellString(table, 1, 1, "index");
  149. mergeCells(table, 1, 1, 2, 2);
  150. setCellString(table, 3, 1, "index2");
  151. }
  152. void ExportReportManager::insertSchemeData()
  153. {
  154. insertText("bmProjName41", m_proj->projectName);
  155. }
  156. bool ExportReportManager::insertText(const QString &bookmark, const QString &text)
  157. {
  158. bool ret = false;
  159. QAxObject *bmProjName = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark);
  160. if (bmProjName != nullptr) {
  161. bmProjName->dynamicCall("Select(void)");
  162. QAxObject *range = bmProjName->querySubObject("Range");
  163. ret = range->setProperty("Text", text);
  164. }
  165. return ret;
  166. }
  167. QAxObject *ExportReportManager::insertTable(QString bookmark, int row, int column)
  168. {
  169. QAxObject *bmTable = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark);
  170. if (bmTable) {
  171. bmTable->dynamicCall("Select(void)");
  172. QAxObject *selection = m_word->getWordApp()->querySubObject("Selection");
  173. if (!selection)
  174. return nullptr;
  175. selection->dynamicCall("InsertAfter(QString&)", "\n");
  176. // selection->dynamicCall("MoveLeft(int)", 1);
  177. selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");
  178. // selection->dynamicCall("TypeText(QString&)", "Table Test"); //设置标题
  179. QAxObject *range = selection->querySubObject("Range");
  180. QAxObject *tables = m_word->getDocument()->querySubObject("Tables");
  181. QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column);
  182. if (!tables)
  183. return nullptr;
  184. for (int i = 1; i <= 6; i++) {
  185. QString str = QString("Borders(-%1)").arg(i);
  186. QAxObject *borders = table->querySubObject(str.toLocal8Bit().constData());
  187. borders->dynamicCall("SetLineStyle(int)", 1);
  188. }
  189. return table;
  190. }
  191. return nullptr;
  192. }
  193. void ExportReportManager::setCellString(QAxObject *table, int row, int column, const QString &text)
  194. {
  195. if (table == nullptr) {
  196. return;
  197. }
  198. table->querySubObject("Cell(int,int)", row, column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
  199. }
  200. void ExportReportManager::mergeCells(QAxObject *table, int row, int column, int rowSpan, int columnSpan)
  201. {
  202. if (table == nullptr) {
  203. return;
  204. }
  205. QAxObject *startCell = table->querySubObject("Cell(int, int)", row, column);
  206. QAxObject *endCell = table->querySubObject("Cell(int, int)", row + rowSpan - 1, column + columnSpan - 1);
  207. if (nullptr == startCell) {
  208. return;
  209. }
  210. if (nullptr == endCell) {
  211. return;
  212. }
  213. startCell->querySubObject("Merge(LPDISPATCH)", endCell->asVariant());
  214. }