前言
本文分享基于QT进行Word模板编辑以及Word转PDF的技术,希望对各位小伙伴有所帮助!
1. 制作Word模板-添加书签
》新建Word文档
》插入---标签
将待插入内容制作为标签
》保存为.dot格式
2. 基于QT编辑Word模板
》书签 替换为 文本
//新建一个word应用程序 QAxWidget* word = new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC); //并设置为不可见 word->setProperty("Visible", false); //获取所有的工作文档 QAxObject* documents = word->querySubObject("Documents"); //以test2.dot为模板新建一个文档 documents->dynamicCall("Add(QString)", strTemplatePath); //获取当前激活的文档 QAxObject* document = word->querySubObject("ActiveDocument"); //获取文档中名字为name的标签 QAxObject* bookmark_name = document->querySubObject("Bookmarks(QVariant)", "name"); //选中标签,将字符textg插入到标签位置 if (!bookmark_name->isNull()) { QString sText = ui->lineEdit_name->text(); //此处为替换内容 bookmark_name->dynamicCall("Select(void)"); //选中要选中的区域 bookmark_name->querySubObject("Range")->setProperty("Text", sText); //进行替换操作 } //获取文档中名字为sex的标签 QAxObject* bookmark_sex = document->querySubObject("Bookmarks(QVariant)", "sex"); //选中标签,将字符textg插入到标签位置 if (!bookmark_sex->isNull()) { QString sText = ui->lineEdit_sex->text(); //此处为替换内容 bookmark_sex->dynamicCall("Select(void)"); //选中要选中的区域 bookmark_sex->querySubObject("Range")->setProperty("Text", sText); //进行替换操作 } //将文件保存为doc,同样可以生成docx文档 QString pathsave = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\Report\\report_template.docx"); if (pathsave.isEmpty() == true) { return; } document->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(pathsave)); document->dynamicCall("Close (boolean)", false); word->dynamicCall("Quit()"); QMessageBox::warning(this, "完成", "文件已经保存", QMessageBox::Yes);
》书签 替换为 图片
bool insertPic(QAxObject* document, QString sLabel, QString sFile) { if (!document) return false; QAxObject* bookmark_pic = document->querySubObject("Bookmarks(QString)", sLabel); if (bookmark_pic) { bookmark_pic->dynamicCall("Select(void)"); QAxObject* range = bookmark_pic->querySubObject("Range"); QVariant tmp = range->asVariant(); QList<QVariant> qList; qList << QVariant(sFile); qList << QVariant(false); qList << QVariant(true); qList << tmp; QAxObject* Inlineshapes = document->querySubObject("InlineShapes"); Inlineshapes->dynamicCall("AddPicture(const QString&,QVariant,QVariant,QVariant)", qList); delete Inlineshapes; } return true; }
3. Word转PDF
void zxKneeReportWidget::convertToPDF(QString strWordPath, QString strTargetPath) { if (strWordPath.isEmpty() || strTargetPath.isEmpty()) return; QAxObject* pWordApplication = new QAxObject("Word.Application", 0); QAxObject* pWordDocuments = pWordApplication->querySubObject("Documents"); QString fileName = strWordPath; QString toFilePath = strTargetPath; QVariant filename(fileName); QVariant confirmconversions(false); QVariant readonly(true); QVariant addtorecentfiles(false); QVariant passworddocument(""); QVariant passwordtemplate(""); QVariant revert(false); //打开 QAxObject* doc = pWordDocuments->querySubObject("Open(const QVariant&, const QVariant&,const QVariant&, " "const QVariant&, const QVariant&, " "const QVariant&,const QVariant&)", filename, confirmconversions, readonly, addtorecentfiles, passworddocument, passwordtemplate, revert); QVariant OutputFileName(toFilePath); QVariant ExportFormat(17); //17是pdf QVariant OpenAfterExport(false); //保存后是否自动打开 //转成pdf doc->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)", OutputFileName, ExportFormat, OpenAfterExport ); //关闭 doc->dynamicCall("Close(boolean)", false); }
4. 其它Word操作
/* * 排版方式 * 纵行、横行 */ void WordEngine::setype(bool type) { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } if (type) { selection->querySubObject("PageSetup")->setProperty("Orientation", "wdOrientLandscape"); } else { selection->querySubObject("PageSetup")->setProperty("Orientation", "wdOrientPortrait"); } } /* * 获取页宽 */ int WordEngine::pageWidth() { int width; QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return width; } width = selection->querySubObject("PageSetup")->property("PageWidth").toInt();; return width; } /* *设置字号 */ void WordEngine::setFontSize(int size) { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } selection->querySubObject("Font")->setProperty("Size", size); } /* 设置对齐方式 */ void WordEngine::setAlignment(int index) { QAxObject *selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } if (index == 0) { selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter"); } if (index == 1) { selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify"); } if (index == 2) { selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight"); } if (index == 3) { selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphLeft"); } } /* 插入文字 */ void WordEngine::typeText(QString text, int index, int WdBuiltinStyle, bool bold, int size) { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } setAlignment(index); setFontSize(size); if (bold) selection->querySubObject("Range")->dynamicCall("SetBold(int)", true); //if(WdBuiltinStyle==0) // selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyText"); if(WdBuiltinStyle==1) selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1"); else if (WdBuiltinStyle == 2) selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading2"); else if (WdBuiltinStyle == 3) selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading3"); else if (WdBuiltinStyle == 4) selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading4"); selection->dynamicCall("TypeText(const QString&)", text); } /* 插入图片 */ void WordEngine::AddPicture(QString file) { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } setAlignment(0); QString filename = file; filename.replace("/", "\\"); QAxObject *Inlineshapes = selection->querySubObject("InlineShapes"); Inlineshapes->dynamicCall("AddPicture(const QString&)", filename); delete Inlineshapes; } /* 插入回车 */ void WordEngine::insertEnter() { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } selection->dynamicCall("TypeParagraph(void)"); } /* * 光标移到末尾,跳出单元格 */ void WordEngine::moveForEnd() { QAxObject* selection = m_pWord->querySubObject("Selection"); QVariantList params; params.append(6); params.append(0); selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt(); } /* 创建表格 QStringList headList 添加表头 */ QAxObject* WordEngine::createTable(int row, int column, QStringList headList,bool SetLineStyleHide) { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return false; } selection->dynamicCall("InsertAfter(QString&)", "\r\n"); QAxObject *range = selection->querySubObject("Range"); QAxObject *tables = m_pWorkDocument->querySubObject("Tables"); QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column); table->setProperty("Style", "网格型"); //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整 table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2); //设置表头 for (int i = 0; i<headList.size(); i++) { table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Range")->dynamicCall("SetText(QString)", headList.at(i)); //加粗 table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Range")->dynamicCall("SetBold(int)", true); table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Font")->setProperty("Size", 10); } if (!SetLineStyleHide) { for (int i = 1; i <= 6; i++) { QString str = QString("Borders(-%1)").arg(i); QAxObject *borders = table->querySubObject(str.toLatin1().constData()); borders->dynamicCall("SetLineStyle(int)", 1); } } return table; } /* 填充表格 */ void WordEngine::setCellText(QAxObject *table, int row, int column, QString text) { QAxObject* range = table->querySubObject("Cell(int, int)", row + 1, column + 1)->querySubObject("Range"); if (range) { return; } range->dynamicCall("SetText(QString)", text); } /* 表格插入图片 */ void WordEngine::setAddPicture(QAxObject *table, int row, int column, QString picPath) { QAxObject* range = table->querySubObject("Cell(int, int)", row + 1, column + 1)->querySubObject("Range"); if (!range) { return; } range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath); } /* 光标跳转,类似表格中的tab */ void WordEngine::moveRight() { QAxObject* selection = m_pWord->querySubObject("Selection"); if (!selection) { return; } selection->dynamicCall("MoveRight(int)", 1); }
文章来源:https://www.toymoban.com/news/detail-653301.html
/*****标签处添加字符串 ******input:(QString)word模板地址,(bool)生成word文档是否可视 ******output: */ bool WordEngine::Open(QString sFile, bool bVisible) { //新建一个word应用程序 m_pWord = new QAxObject(); bool bFlag = m_pWord->setControl("word.Application"); if (!bFlag) { return false; } QObject::connect(m_pWord, SIGNAL(exception(int, QString, QString, QString)), this, SLOT(saveLastError(int, QString, QString, QString))); m_pWord->setProperty("Visible", bVisible); //获取所有的工作文档 QAxObject *document = m_pWord->querySubObject("Documents"); if (!document) { return false; } //以文件template.dot为模版新建一个文档 document->dynamicCall("Add(QString)", sFile); //获取当前激活的文档 m_pWorkDocument = m_pWord->querySubObject("ActiveDocument"); if (m_pWorkDocument) m_bIsOpen = true; else m_bIsOpen = false; return m_bIsOpen; } /*****标签处添加字符串 ******input:(QString)保存地址 ******output: */ void WordEngine::save(QString sSavePath) { if (m_bIsOpen && m_pWorkDocument) { if (m_bNewFile) { m_pWorkDocument->dynamicCall("Save()"); } else { //m_pWorkDocument->dynamicCall("SaveAs (const QString&,int,const QString&,const QString&,bool,bool)", // m_sFile,56,QString(""),QString(""),false,false); m_pWorkDocument->dynamicCall("SaveAs (const QString&)", sSavePath); } } qDebug() << "save Done."; } /*****标签处添加字符串 ******input:(bool)是否保存当前文件,(QString)保存地址[必填] ******output: */ void WordEngine::close(bool bSave,QString save_path) { if (bSave) { save(save_path); } if (m_pWord) { m_pWord->setProperty("DisplayAlerts", true); } if (m_pWorkDocument) { m_pWorkDocument->dynamicCall("Close(bool)", true); } if (m_pWord) { m_pWord->dynamicCall("Quit()"); } if (m_pWorkDocuments) { delete m_pWorkDocuments; } if (m_pWord) { delete m_pWord; } m_pWorkDocument = NULL; m_pWorkDocuments = NULL; m_pWord = NULL; m_bIsOpen = false; m_bNewFile = false; } /*****标签处添加字符串 ******input:(QString)标签名字,(QString)内容 ******output: (bool) */ bool WordEngine::replaceText(QString sLabel, QString sText) { if (!m_pWorkDocument) { return false; } //获取文档中名字为sLabel的标签 QAxObject *pBookmark = m_pWorkDocument->querySubObject("Bookmarks(QString)", sLabel); if (pBookmark) { pBookmark->dynamicCall("Select(void)"); pBookmark->querySubObject("Range")->setProperty("Text", sText); delete pBookmark; } return true; } /***** 插入文本到标签处 【 居中,行距1.5,段后0.5】 ******input:(QString)标签名字,(QString)内容,(QString)字体,(int)字体大小 【默认黑体,字号12】 ******output: (bool) */ bool WordEngine::replaceTextStyle(QString sLabel, QString sText, QString FontName, int size) { if (!m_pWorkDocument) { return false; } //获取文档中名字为sLabel的标签 QAxObject *pBookmark = m_pWorkDocument->querySubObject("Bookmarks(QString)", sLabel); if (pBookmark) { pBookmark->dynamicCall("Select(void)"); QAxObject *selection = m_pWord->querySubObj## 标题ect("Selection");//设置字体FontName selection->dynamicCall("InsertAfter(QString&)", "\r\n");//不能删除,否则会出错 selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", size);//设置字体大小:小四12 selection->querySubObject("Range")->querySubObject("Font")->setProperty("Name", FontName);//字体(在插入字符前设置字体) pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");//水平居中 pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("LineSpacingRule", "wdLineSpace1pt5");//1.5行距 pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("LineUnitAfter", 0.5);//段后0.5 pBookmark->querySubObject("Range")->setProperty("Text", sText);//插入字符 delete pBookmark; //selection->querySubObject("Range")->querySubObject("Font")->setProperty("Bold", 1);//加粗 } return true; } /***** 插入文本到标签处 【 居中,行距1.5,段后0.5】 ******input:(QString)标签名字,(QString)内容,是否居中(0靠左,1居中,2靠右),(QString)字体,(int)字体大小 【默认黑体,字号12】 ******output: (bool) */ bool WordEngine::WordReplaceText(QString sLabel, QString sText, int iscenter, QString FontName, int size) { if (!m_pWorkDocument) { return false; } //获取文档中名字为sLabel的标签 QAxObject *pBookmark = m_pWorkDocument->querySubObject("Bookmarks(QString)", sLabel); if (pBookmark) { pBookmark->dynamicCall("Select(void)"); QAxObject *selection = m_pWord->querySubObject("Selection");//设置字体FontName selection->dynamicCall("InsertAfter(QString&)", "\r\n");//不能删除,否则会出错 selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", size);//设置字体大小:小四12 selection->querySubObject("Range")->querySubObject("Font")->setProperty("Name", FontName);//字体(在插入字符前设置字体) if (iscenter == 1) pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");//水平居中 pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("LineSpacingRule", "wdLineSpace1pt5");//1.5行距 pBookmark->querySubObject("Range")->querySubObject("ParagraphFormat")->setProperty("LineUnitAfter", 0.5);//段后0.5 pBookmark->querySubObject("Range")->setProperty("Text", sText);//插入字符 delete pBookmark; //selection->querySubObject("Range")->querySubObject("Font")->setProperty("Bold", 1);//加粗 } return true; } /*****删除标签处文本 ******input:(QString)标签名 ******output: */ bool WordEngine::deleteLable(QString sLabel) { if (!m_pWorkDocument) { return false; } //获取文档中名字为sLabel的标签 QAxObject *pBookmark = m_pWorkDocument->querySubObject("Bookmarks(QString)", sLabel); if (pBookmark) { pBookmark->dynamicCall("Select(void)"); pBookmark->querySubObject("Range")->deleteLater(); delete pBookmark; } return true; } /*****标签处添加图片 ******input:(QString)标签名字,(QString)图片地址 ******output: (bool) */ bool WordEngine::replacePic(QString sLabel, QString sFile) { if (!m_pWorkDocument) return false; QAxObject *bookmark_pic = m_pWorkDocument->querySubObject("Bookmarks(QString)", sLabel); if (bookmark_pic) { bookmark_pic->dynamicCall("Select(void)"); QAxObject *range = bookmark_pic->querySubObject("Range"); QVariant tmp = range->asVariant(); QList<QVariant> qList; qList << QVariant(sFile); qList << QVariant(false); qList << QVariant(true); qList << tmp; QAxObject *Inlineshapes = m_pWorkDocument->querySubObject("InlineShapes"); Inlineshapes->dynamicCall("AddPicture(const QString&,QVariant,QVariant,QVariant)", qList); delete Inlineshapes; } return true; } /*****标签处插入表格:不带头 ******input:(QString)标签名字,(int)表格行数,(int)列数 ******output: (QAxObject) */ QAxObject *WordEngine::insertTable(QString sLabel, int row, int column) { QAxObject *bookmark = m_pWorkDocument->querySubObject("Bookmarks(QVariant)", sLabel); if (bookmark) { bookmark->dynamicCall("Select(void)"); QAxObject *selection = m_pWord->querySubObject("Selection"); selection->dynamicCall("InsertAfter(QString&)", "\n"); //selection->dynamicCall("MoveLeft(int)", 1); selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");//水平居中 //selection->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 //selection->dynamicCall("TypeText(QString&)", "Table Test");//设置标题 QAxObject *range = selection->querySubObject("Range"); QAxObject *tables = m_pWorkDocument->querySubObject("Tables"); QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column); for (int i = 1; i <= 6; i++) { QString str = QString("Borders(-%1)").arg(i); QAxObject *borders = table->querySubObject(str.toLatin1().constData()); borders->dynamicCall("SetLineStyle(int)", 1); } return table; } return NULL; } /*****标签处插入表格:带头 ******input:(QString)标签名字,(int)表格行数,(int)列数,(QStringList)表头数据 ******output: (QAxObject) */ QAxObject *WordEngine::insertTable(QString sLabel, int row, int column, QStringList headList) { QAxObject *bookmark = m_pWorkDocument->querySubObject("Bookmarks(QVariant)", sLabel); if (headList.size() != column) { return NULL; } if (bookmark) { bookmark->dynamicCall("Select(void)"); QAxObject *selection = m_pWord->querySubObject("Selection"); selection->dynamicCall("InsertAfter(QString&)", "\r\n"); //selection->dynamicCall("MoveLeft(int)", 1); selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");//水平居中 //selection->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 //设置标题 //selection->dynamicCall("TypeText(QString&)", "Table Test"); QAxObject *range = selection->querySubObject("Range"); QAxObject *tables = m_pWorkDocument->querySubObject("Tables"); QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column); //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整 table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2); //设置表头 for (int i = 0; i < headList.size(); i++) { table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Range")->dynamicCall("SetText(QString)", headList.at(i)); //加粗 table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Range")->dynamicCall("SetBold(int)", true); } for (int i = 1; i <= 6; i++) { QString str = QString("Borders(-%1)").arg(i); QAxObject *borders = table->querySubObject(str.toLatin1().constData()); borders->dynamicCall("SetLineStyle(int)", 1); } return table; } return NULL; } /*****为表格添加行 ******input:(QAxObject)表格指针,(int)第几行处添加,(int)共添加几行 ******output: */ void WordEngine::addTableRow(QAxObject *table, int nRow, int rowCount) { QAxObject *rows = table->querySubObject("Rows"); int count = rows->dynamicCall("Count").toInt(); if (0 <= nRow && nRow <= count) { for (size_t i = 0; i < rowCount; i++) { QString sPos = QString("Item(%1)").arg(nRow + i); QAxObject *row = rows->querySubObject(sPos.toStdString().c_str()); QVariant param = row->asVariant(); rows->dynamicCall("Add(Variant)", param); } } } /*****获取表格行数 ******input:所选信号 ******output: (int) */ int WordEngine::currenRow() { QAxObject*selection = m_pWord->querySubObject("Selection"); int row = selection->dynamicCall("Information(QVariant&)", 10).toInt(); return row; } /*****合并单元格 ******input:(int)开始行,(int)开始列,(int)结束行,(int)结束列,(QAxObject)表格指针 ******output: (QAxObject) */ void WordEngine::MergeCells(int nStartRow, int nStartCol, int nEndRow, int nEndCol, QAxObject *table, int tableIndex) { if (nullptr == table) { return; } if (table) { QAxObject* StartCell = table->querySubObject("Cell(int, int)", nStartRow, nStartCol); QAxObject* EndCell = table->querySubObject("Cell(int, int)", nEndRow, nEndCol); if (nullptr == StartCell) { return; } if (nullptr == EndCell) { return; } StartCell->querySubObject("Merge(QAxObject *)", EndCell->asVariant()); } } /*****获取单元格内容 此处对于Excel来说列和行从1开始最少 ******input:(int)行,(int)列 ******output: (QVariant) */ QVariant WordEngine::getCellValue(int row, int column) { QAxObject* selection = m_pWord->querySubObject("Selection"); QAxObject* table = selection->querySubObject("Tables(1)"); if (nullptr != selection && nullptr != table) return table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range")->property("Text"); else return QVariant(""); } /*****获取单元格内容 此处对于Excel来说列和行从1开始最少 ******input:(int)行,(int)列,(QAxObject)表格指针 ******output: (QVariant) */ QVariant WordEngine::getCellValue(int row, int column, QAxObject *table) { if (nullptr != table) { QAxObject *cell = table->querySubObject("Cell(int, int)", row, column); if (cell) return cell->querySubObject("Range")->property("Text"); } return QVariant(""); } /*****设置表格列宽 ******input:(QAxObject)表格指针,(int)列,(int)宽度 ******output: */ void WordEngine::setColumnWidth(QAxObject *table, int column, int width) { if (!table) { return; } table->querySubObject("Columns(int)", column)->setProperty("Width", width); } /*****设置表格内容 ******input:(QAxObject)表格指针,(int)行,(int)列,(int)内容 ******output: */ void WordEngine::SetTableCellString(QAxObject *table, int row, int column, QString text,int Alignment) { if (!table) return; QAxObject *cell = table->querySubObject("Cell(int,int)", row, column); if (!cell) return; cell->dynamicCall("Select(void)"); if (!cell) return; cell->querySubObject("Range")->setProperty("Text", text); if (row == 1) //加粗 cell->querySubObject("Range")->dynamicCall("SetBold(int)", true); setAlignment(Alignment); cell->querySubObject("Range")->querySubObject("Font")->setProperty("Size", 10); cell->querySubObject("Range")->querySubObject("Font")->setProperty("Name", "Times New Roman"); } void WordEngine::saveLastError(int arg1, QString arg2, QString arg3, QString arg4) { qDebug() << "QAxBase Error:" << QString::number(arg1) << arg2 << arg3 << arg4; } void WordEngine::setRowAlignment(QAxObject *table, int row, int flag) { if (nullptr == table) { return; } QAxObject* Row = table->querySubObject("Rows(int)", row); if (nullptr == Row) { return; } QAxObject* range = Row->querySubObject("Range"); if (nullptr == range) { return; } Row->querySubObject("Alignment(int)", flag); if (flag == 0) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");//水平居中 range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 1) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify"); range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 2)//wdAlignParagraphJustifyMed { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight"); range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 3) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphLeft"); range->querySubObject("ParagraphFormat")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } } void WordEngine::setCellAlignment(QAxObject *table, int row, int col, int flag) { if (nullptr == table) { return; } QAxObject *cell = table->querySubObject("Cell(int,int)", row, col); if (nullptr == cell) { return; } QAxObject* range = cell->querySubObject("Range"); if (nullptr == range) { return; } cell->querySubObject("Alignment(int)", flag); if (flag == 0) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");//水平居中 range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 1) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify"); range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 2)//wdAlignParagraphJustifyMed { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight"); range->querySubObject("Cells")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } else if (flag == 3) { range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphLeft"); range->querySubObject("ParagraphFormat")->setProperty("VerticalAlignment", "wdCellAlignVerticalCenter");//垂直居中 } }
文章来源地址https://www.toymoban.com/news/detail-653301.html
到了这里,关于QT技术实现Word模板编辑及转PDF的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!