QWordDemo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "QWordDemo.h"
  2. #include "QWord.h"
  3. #include <QDateTime>
  4. #include <QDir>
  5. #include <QPushButton>
  6. QWordDemo::QWordDemo(QWidget *parent) : QMainWindow(parent)
  7. {
  8. resize(400, 300);
  9. QPushButton *exportPushBtn = new QPushButton(this);
  10. exportPushBtn->setGeometry(QRect(140, 100, 93, 28));
  11. exportPushBtn->setText("生成Word");
  12. connect(exportPushBtn, &QPushButton::clicked, this, &QWordDemo::on_exportPushBtn_clicked);
  13. }
  14. QWordDemo::~QWordDemo() { }
  15. QString QWordDemo::getDetectType(int detectType)
  16. {
  17. QString strDetectType;
  18. switch (detectType) {
  19. case ALARM_CURRENT_HEAT: {
  20. strDetectType = tr("Current Heat");
  21. } break;
  22. case ALARM_VOLTAGE_HEAT: {
  23. strDetectType = tr("Voltage Heat");
  24. } break;
  25. default:
  26. break;
  27. }
  28. return strDetectType;
  29. }
  30. QString QWordDemo::getAlarmLevel(int level)
  31. {
  32. QString strLevel;
  33. switch (level) {
  34. case NORMAL_ALARM: {
  35. strLevel = tr("Normal");
  36. } break;
  37. case GENERAL_ALARM: {
  38. strLevel = tr("Deferred Alarm "); //一般告警
  39. } break;
  40. case CRITICAL_ALARM: {
  41. strLevel = tr("Critical Alarm "); //严重告警
  42. } break;
  43. case URGENT_ALARM: {
  44. strLevel = tr("Urgent Alarm"); //紧急告警
  45. } break;
  46. default:
  47. break;
  48. }
  49. return strLevel;
  50. }
  51. void QWordDemo::on_exportPushBtn_clicked()
  52. {
  53. repItem.domid = "";
  54. repItem.devcode = "";
  55. repItem.devname = "device1";
  56. repItem._detectType = 2;
  57. repItem._typeDesc = "Pressured oil driverpipe";
  58. repItem.electricVal = 1.2;
  59. repItem.result = "comp_A_B_C_phase Over the upper limit,possible cause:no oil;";
  60. repItem._installspot = "hang zhou";
  61. repItem.alarm_level = 3;
  62. repItem.alarmTime = "2017-09-04 04:40:39";
  63. repItem._maintainer = "HK";
  64. repItem._model = "HK005";
  65. repItem._productSeq = "1234569";
  66. repItem._productTime = "2015-09-04 04:40:39";
  67. repItem.envtemp = 12;
  68. repItem.envhumidity = 25;
  69. repItem.envwindspeed = 13;
  70. QString strDevName = QString::fromStdString(repItem.devname);
  71. QString strResult = QString::fromStdString(repItem.result);
  72. QString strOrgName = QString::fromStdString(repItem._installspot);
  73. QString strAlarmLevel = QString("%1").arg(repItem.alarm_level);
  74. QString strAlarmTime = QString::fromStdString(repItem.alarmTime);
  75. QString strDevCode = QString::fromStdString(repItem.devcode);
  76. QString strDetectType = getDetectType(repItem._detectType);
  77. QString strDevType = QString::fromStdString(repItem._typeDesc);
  78. QString strInstallPlace = QString::fromStdString(repItem._installspot);
  79. QString strManufacture = QString::fromStdString(repItem._maintainer);
  80. QString strDevModel = QString::fromStdString(repItem._model);
  81. QString strRateCurrent = QString("%1").arg(repItem.electricVal);
  82. QString strDevProSeq = QString::fromStdString(repItem._productSeq);
  83. QString strDevProTime = QString::fromStdString(repItem._productTime);
  84. QString current_Date_Time = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
  85. QString fileName = tr("Infrared diagnosis report") + "_" + strDevName + "_" + current_Date_Time;
  86. fileName.replace("-", "");
  87. fileName.replace(":", "");
  88. fileName.replace(" ", "");
  89. QString filePath = "";
  90. QDate Cur_Date = QDate::currentDate();
  91. QString strCurDate = Cur_Date.toString("yyyy-MM-dd");
  92. filePath = "D:/" + strCurDate + "/";
  93. QDir dirReportPath(filePath);
  94. if (!dirReportPath.exists()) {
  95. if (dirReportPath.mkpath(filePath)) { filePath += fileName + tr(".docx"); }
  96. } else {
  97. filePath += fileName + tr(".docx");
  98. }
  99. QWord word;
  100. if (!word.createNewWord(filePath)) {
  101. QString error = tr("Failed to export report,") + word.getStrErrorInfo();
  102. return;
  103. }
  104. word.setPageOrientation(0); //页面方向
  105. word.setWordPageView(3); //页面视图
  106. word.setFontName(QString::fromLocal8Bit("宋体"));
  107. word.setParagraphAlignment(0); //下面文字位置
  108. word.setFontSize(20); //字体大小
  109. word.setFontBold(true); //字体加粗
  110. word.insertText(tr("Electrical Equipment Infrared Diagnosis Report "));
  111. word.setFontBold(false);
  112. word.insertMoveDown();
  113. word.setFontSize(10);
  114. word.setParagraphAlignment(1);
  115. QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  116. word.insertText(tr("Report Generation Date:"));
  117. word.insertText(current_Time);
  118. word.insertMoveDown();
  119. #if 0
  120. // first table
  121. word.intsertTable(4, 6);
  122. word.setCellFontBold(1, 1, 1, true);
  123. word.setCellFontBold(1, 1, 3, true);
  124. word.setCellFontBold(1, 1, 5, true);
  125. word.setCellFontBold(1, 2, 1, true);
  126. word.setCellFontBold(1, 2, 3, true);
  127. word.setCellFontBold(1, 2, 5, true);
  128. word.setCellFontBold(1, 3, 1, true);
  129. word.setCellFontBold(1, 3, 3, true);
  130. word.setCellFontBold(1, 3, 5, true);
  131. word.setCellFontBold(1, 4, 1, true);
  132. word.setCellFontBold(1, 4, 3, true);
  133. word.setCellFontBold(1, 4, 5, true);
  134. word.setTableAutoFitBehavior(0);
  135. word.setFontSize(10);
  136. word.setColumnWidth(1, 1, 120);
  137. word.setColumnWidth(1, 2, 120);
  138. word.setColumnWidth(1, 3, 120);
  139. word.setColumnWidth(1, 4, 120);
  140. word.setColumnWidth(1, 5, 120);
  141. word.setRowAlignment(1, 1, 0);
  142. word.setCellString(1, 1, 1, tr("Device Name"));
  143. word.setCellString(1, 1, 2, strDevName);
  144. word.setCellString(1, 1, 3, tr("Analysis Type"));
  145. word.setCellString(1, 1, 4, strDetectType);
  146. word.setCellString(1, 1, 5, tr("Device Type"));
  147. word.setCellString(1, 1, 6, strDevType);
  148. word.setRowAlignment(1, 2, 0);
  149. word.setCellString(1, 2, 1, tr("Install place"));
  150. word.setCellString(1, 2, 2, strInstallPlace);
  151. word.setCellString(1, 2, 3, tr("Manufacture"));
  152. word.setCellString(1, 2, 4, strManufacture);
  153. word.setCellString(1, 2, 5, tr("Device Model"));
  154. word.setCellString(1, 2, 6, strDevModel);
  155. word.setRowAlignment(1, 3, 0);
  156. word.setCellString(1, 3, 1, tr("Rated Current"));
  157. word.setCellString(1, 3, 2, strRateCurrent);
  158. word.setCellString(1, 3, 3, tr("Produce sequence Number "));
  159. word.setCellString(1, 3, 4, strDevProSeq);
  160. word.setCellString(1, 3, 5, tr("Factory Time"));
  161. word.setCellString(1, 3, 6, strDevProTime);
  162. word.setRowAlignment(1, 4, 0);
  163. word.setCellString(1, 4, 1, tr("Temperature"));
  164. if (repItem.envtemp == 255) {
  165. word.setCellString(1, 4, 2, tr("Unknown Value "));
  166. } else {
  167. word.setCellString(1, 4, 2, QString("%1").arg(repItem.envtemp) + QString::fromLocal8Bit("℃ "));
  168. }
  169. word.setCellString(1, 4, 3, tr("Humidity"));
  170. if (repItem.envhumidity == -1) {
  171. word.setCellString(1, 4, 4, tr("Unknown Value "));
  172. } else {
  173. word.setCellString(1, 4, 4, QString("%1").arg(repItem.envhumidity) + QString::fromLocal8Bit("% "));
  174. }
  175. word.setCellString(1, 4, 5, tr("Wind speed"));
  176. if (repItem.envwindspeed == -1) {
  177. word.setCellString(1, 4, 6, tr(" Unknown Value "));
  178. } else {
  179. word.setCellString(1, 4, 6, QString("%1").arg(repItem.envwindspeed) + tr("Class "));
  180. }
  181. word.moveForEnd();
  182. word.insertMoveDown();
  183. // second table
  184. word.intsertTable(2, 2);
  185. word.setCellString(2, 1, 1, tr("Infrared alarm heat map"));
  186. word.setCellString(2, 1, 2, tr("Visible light picture"));
  187. word.setCellString(2, 2, 1, tr(""));
  188. word.setCellString(2, 2, 2, tr(""));
  189. word.setTableAutoFitBehavior(0);
  190. word.moveForEnd();
  191. word.insertMoveDown();
  192. // third table
  193. word.intsertTable(1, 5);
  194. word.setRowAlignment(3, 1, 0);
  195. word.setColumnWidth(1, 100);
  196. word.setColumnWidth(2, 60);
  197. word.setColumnWidth(3, 60);
  198. word.setColumnWidth(4, 60);
  199. word.setColumnWidth(5, 135);
  200. word.setCellString(3, 1, 1, tr("Picture"));
  201. word.setCellString(3, 1, 2, tr("Record"));
  202. word.setCellString(3, 1, 3, tr("Map"));
  203. word.setCellString(3, 1, 4, tr("DownLoad"));
  204. word.setCellString(3, 1, 5, tr("Count"));
  205. word.moveForEnd();
  206. word.insertMoveDown();
  207. word.setParagraphAlignment(1);
  208. word.setFontBold(true);
  209. word.insertText(tr("Diagnosis Analysis:"));
  210. word.intsertTable(1, 1);
  211. word.setParagraphAlignment(1);
  212. word.setCellFontBold(4, 1, 1, false);
  213. word.setTableAutoFitBehavior(0);
  214. #endif
  215. QString strRet = tr("Refer to the equipment reason for the diagnosis and treatment of the equipment,according to "
  216. "the standard of infrared diagnosis of electrical equipment .")
  217. + QString::fromLocal8Bit("\r\n");
  218. strRet += tr("According to the actual measured temperature,reduced temperature and temperature difference "
  219. "racquetball,General analysis,and diagnosis conclusion as follows:")
  220. + QString::fromLocal8Bit("\r\n");
  221. int AlmLevel = NORMAL_ALARM;
  222. QString tempAlaLevel = getAlarmLevel(AlmLevel);
  223. strRet += tr("Fault Level:");
  224. strRet += tempAlaLevel;
  225. strRet += QString::fromLocal8Bit("\r\n");
  226. strRet += strResult;
  227. strRet += QString::fromLocal8Bit("\r\n");
  228. strRet += QString::fromLocal8Bit("\r\n");
  229. word.insertText(strRet);
  230. word.moveForEnd();
  231. word.setParagraphAlignment(1);
  232. word.setFontBold(true);
  233. word.insertText(tr("Conclusion and suggestions:"));
  234. word.intsertTable(1, 1);
  235. word.setParagraphAlignment(1);
  236. QString strAdvice;
  237. switch (AlmLevel) {
  238. case NORMAL_ALARM: {
  239. strAdvice = tr("Normal");
  240. } break;
  241. case GENERAL_ALARM: {
  242. strAdvice = tr("Current device alarm,and the system will automatically track inspect,proposing arrangment for "
  243. "the analysis and detection of professional personnel.");
  244. } break;
  245. case CRITICAL_ALARM: {
  246. strAdvice = tr(
  247. "Current device alarm,it's suggested that the diagnosis and analysis of the actual diagnosis and "
  248. "analysis,in order to further identify the fault and arrange the professional personnelto solve it.");
  249. } break;
  250. case URGENT_ALARM: {
  251. strAdvice = tr("Current device alarm,it's suggested that the diagnosis and analysis of the actual diagnosis "
  252. "and analysis.if necessary,as soon as possible to carry out power outage.");
  253. } break;
  254. default:
  255. break;
  256. }
  257. word.setCellFontBold(1, 1, false);
  258. word.insertText(strAdvice);
  259. word.insertMoveDown();
  260. word.moveForEnd();
  261. word.setParagraphAlignment(1);
  262. word.insertText(tr("Comments: ")); //备注
  263. word.intsertTable(1, 1);
  264. word.setParagraphAlignment(1);
  265. word.insertMoveDown();
  266. word.insertMoveDown();
  267. word.insertMoveDown();
  268. word.insertMoveDown();
  269. word.moveForEnd();
  270. word.setVisible(true);
  271. word.saveAs();
  272. }