ExportReportManager.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "ExportReportManager.h"
  2. #include "ProjectManager.h"
  3. #include <dbService/ClassSet.h>
  4. #include <QWord/QWord.h>
  5. #include <QWord/QWordDemo.h>
  6. #include <QDir>
  7. #include <QDate>
  8. #include <QDebug>
  9. ExportReportManager::ExportReportManager(QObject *parent) : QObject(parent)
  10. {
  11. m_word = new QWord(this);
  12. }
  13. ExportReportManager::~ExportReportManager()
  14. {
  15. delete m_word;
  16. }
  17. ProjectInfo *ExportReportManager::proj() const
  18. {
  19. return m_proj;
  20. }
  21. void ExportReportManager::setProj(ProjectInfo *proj)
  22. {
  23. m_proj = proj;
  24. }
  25. int ExportReportManager::evalType() const
  26. {
  27. return m_evalType;
  28. }
  29. void ExportReportManager::setEvalType(int type)
  30. {
  31. m_evalType = type;
  32. }
  33. bool ExportReportManager::exportReport()
  34. {
  35. bool createRet = createWord();
  36. if (createRet == false) {
  37. qDebug() << __FUNCTION__ << __LINE__ << m_word->getStrErrorInfo() << endl;
  38. return false;
  39. }
  40. configWord();
  41. addCover();
  42. m_word->setVisible(true);
  43. m_word->saveAs();
  44. m_word->close();
  45. return true;
  46. }
  47. bool ExportReportManager::createWord()
  48. {
  49. // 报告文件夹路径
  50. QString curPath = QDir::currentPath();
  51. QString dateStr = QDate::currentDate().toString("yyyy-MM-dd");
  52. QString reportDir = curPath + "/reports/" + dateStr + "/";
  53. QDir dir(reportDir);
  54. if (!dir.exists()) {
  55. dir.mkpath(reportDir);
  56. }
  57. // 报告文件路径
  58. QString typeStr = ProjectManager::nameOfEvalType((ProjectManager::EvalType)m_evalType);
  59. QString fileName = m_proj->projectName + "_" + typeStr + "报告.docx";
  60. QString filePath = reportDir + fileName;
  61. qDebug() << __FUNCTION__ << __LINE__ << filePath << endl;
  62. // 创建 word 文档
  63. return m_word->createNewWord(filePath);
  64. }
  65. void ExportReportManager::configWord()
  66. {
  67. m_word->setPageOrientation(0);
  68. m_word->setWordPageView(3);
  69. }
  70. void ExportReportManager::addCover()
  71. {
  72. m_word->setParagraphAlignment(0); //下面文字位置
  73. m_word->setFontName(QString::fromLocal8Bit("黑体"));
  74. m_word->setFontSize(16);
  75. insertWraps(5);
  76. m_word->setFontSize(26);
  77. m_word->insertText(m_proj->projectName);
  78. QString typeStr = ProjectManager::nameOfEvalType((ProjectManager::EvalType)m_evalType);
  79. if (typeStr.length() > 2) {
  80. typeStr = typeStr.mid(0, typeStr.length() - 2);
  81. }
  82. m_word->insertText(typeStr);
  83. m_word->setFontSize(16);
  84. insertWraps();
  85. m_word->setFontSize(26);
  86. m_word->insertText("评估报告");
  87. m_word->setFontSize(16);
  88. insertWraps(10);
  89. m_word->setFontSize(18);
  90. m_word->insertText(m_proj->estimateDept);
  91. m_word->setFontSize(16);
  92. insertWraps();
  93. m_word->setFontSize(18);
  94. m_word->insertText(QDate::currentDate().toString("yyyy年MM月"));
  95. m_word->setFontSize(16);
  96. insertWraps(2);
  97. }
  98. void ExportReportManager::insertWraps(int count)
  99. {
  100. for (int i = 0; i < count; ++i) {
  101. m_word->insertMoveDown();
  102. }
  103. }