#include "ExportReportManager.h" #include "ProjectManager.h" #include #include #include #include #include #include #include #include ExportReportManager::ExportReportManager(QObject *parent) : QObject(parent) { m_word = new QWord(this); } ExportReportManager::~ExportReportManager() { delete m_word; } ProjectInfo *ExportReportManager::proj() const { return m_proj; } void ExportReportManager::setProj(ProjectInfo *proj) { m_proj = proj; } int ExportReportManager::evalType() const { return m_evalType; } void ExportReportManager::setEvalType(int type) { m_evalType = type; } bool ExportReportManager::exportReport() { bool createRet = createWord(); if (createRet == false) { qDebug() << __FUNCTION__ << __LINE__ << m_word->getStrErrorInfo() << endl; return false; } insertCommonInfo(); if (m_evalType == ProjectManager::OptimalEval) { insertSchemeDesignInfo(); insertSchemeIndex(); insertSchemeData(); } m_word->setVisible(true); QAxObject *doc = m_word->getWordApp()->querySubObject("ActiveDocument"); doc->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(m_word->getFilePath())); m_word->close(); return true; } bool ExportReportManager::createWord() { delete m_word; m_word = new QWord(this); // 报告文件夹路径 QString curPath = QDir::currentPath(); QString dateStr = QDate::currentDate().toString("yyyy-MM-dd"); QString reportDir = curPath + "/reports/" + dateStr + "/"; QDir dir(reportDir); if (!dir.exists()) { dir.mkpath(reportDir); } // 根据模板创建 word 文档 QString typeStr = ProjectManager::reportNameOfEvalType((ProjectManager::EvalType)m_evalType); QString tempPath = "E:/qfd/code/QFD2/bin/report_templates/" + typeStr + ".docx"; if (!QFile::exists(tempPath)) { qDebug() << __FUNCTION__ << __LINE__ << "Report template not found" << endl; return false; } QString fileName = m_proj->projectName + "_" + typeStr + ".docx"; QString filePath = reportDir + fileName; if (QFile::exists(filePath)) { QFile::remove(filePath); } bool ret = QFile::copy(tempPath, filePath); if (ret) { ret = m_word->open(filePath, false); } else { qDebug() << __FUNCTION__ << __LINE__ << "Copy report template failed" << endl; } return ret; } void ExportReportManager::insertCommonInfo() { insertText("bmProjName", m_proj->projectName); insertText("bmProjUnit", m_proj->estimateDept); QString dateStr = QDate::currentDate().toString("yyyy年MM月"); insertText("bmProjDate", dateStr); insertText("bmEvalPurpose", m_proj->estimateObjective); } void ExportReportManager::insertSchemeDesignInfo() { QList schemeList; bool ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, 0); if (ret == false) { qDebug() << __FUNCTION__ << __LINE__ << "query" << endl; return; } for (int i = schemeList.size() - 1; i >= 0; --i) { // 插入图片 QAxObject *bmSchemes = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmSchemes"); if (bmSchemes != nullptr) { bmSchemes->dynamicCall("Select(void)"); QAxObject *range = bmSchemes->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", schemeList[i]->filePath); if (shape != nullptr) { shape->setProperty("Width", 200); shape->setProperty("Height", 200); } } m_word->insertMoveDown(); // 插入图片标题 QString remark = schemeList[i]->remark; int picTtlIndex = remark.lastIndexOf("(图"); QString subStr = remark.mid(picTtlIndex + 1, 8); QString picTtl = ""; if (subStr.contains(")")) { picTtl = subStr.split(")").first(); } m_word->setFontName(QString::fromLocal8Bit("黑体")); m_word->setFontSize(9); m_word->setParagraphAlignment(0); m_word->insertText(picTtl + " " + "阵地位置图"); m_word->insertMoveDown(); // 插入方案描述 insertText("bmSchemes", schemeList[i]->name + ":" + schemeList[i]->remark); if (i != 0) { m_word->insertMoveDown(); } } } void ExportReportManager::insertSchemeIndex() { insertText("bmProjName31", m_proj->projectName); insertText("bmProjName32", m_proj->projectName); // bmIndexTable QAxObject *table = insertTable("bmIndexTable", 15, 5); if (table == nullptr) { return; } m_word->setFontName(QString::fromLocal8Bit("仿宋_GB2312")); m_word->setFontSize(9); m_word->setParagraphAlignment(0); setCellString(table, 1, 1, "index"); mergeCells(table, 1, 1, 2, 2); setCellString(table, 3, 1, "index2"); } void ExportReportManager::insertSchemeData() { insertText("bmProjName41", m_proj->projectName); } bool ExportReportManager::insertText(const QString &bookmark, const QString &text) { bool ret = false; QAxObject *bmProjName = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark); if (bmProjName != nullptr) { bmProjName->dynamicCall("Select(void)"); QAxObject *range = bmProjName->querySubObject("Range"); ret = range->setProperty("Text", text); } return ret; } QAxObject *ExportReportManager::insertTable(QString bookmark, int row, int column) { QAxObject *bmTable = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark); if (bmTable) { bmTable->dynamicCall("Select(void)"); QAxObject *selection = m_word->getWordApp()->querySubObject("Selection"); if (!selection) return nullptr; selection->dynamicCall("InsertAfter(QString&)", "\n"); // selection->dynamicCall("MoveLeft(int)", 1); selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter"); // selection->dynamicCall("TypeText(QString&)", "Table Test"); //设置标题 QAxObject *range = selection->querySubObject("Range"); QAxObject *tables = m_word->getDocument()->querySubObject("Tables"); QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column); if (!tables) return nullptr; for (int i = 1; i <= 6; i++) { QString str = QString("Borders(-%1)").arg(i); QAxObject *borders = table->querySubObject(str.toLocal8Bit().constData()); borders->dynamicCall("SetLineStyle(int)", 1); } return table; } return nullptr; } void ExportReportManager::setCellString(QAxObject *table, int row, int column, const QString &text) { if (table == nullptr) { return; } table->querySubObject("Cell(int,int)", row, column)->querySubObject("Range")->dynamicCall("SetText(QString)", text); } void ExportReportManager::mergeCells(QAxObject *table, int row, int column, int rowSpan, int columnSpan) { if (table == nullptr) { return; } QAxObject *startCell = table->querySubObject("Cell(int, int)", row, column); QAxObject *endCell = table->querySubObject("Cell(int, int)", row + rowSpan - 1, column + columnSpan - 1); if (nullptr == startCell) { return; } if (nullptr == endCell) { return; } startCell->querySubObject("Merge(LPDISPATCH)", endCell->asVariant()); }