ExportReportManager.cpp 8.9 KB

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