123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- #include "QWord.h"
- #ifdef Q_OS_WIN
- # include <QDateTime>
- # include <QFileDialog>
- # include <QFile>
- # include <QTextStream>
- # include <ActiveQt/QAxObject>
- # include <ActiveQt/QAxWidget>
- # include <QTextStream>
- # include <ActiveQt/QAxBase>
- # include <QTextFormat>
- # include <QDebug>
- QWord::QWord(QObject *parent)
- {
- CoInitializeEx(NULL, COINIT_MULTITHREADED); //解决非主线程无法调用word问题
- m_word = new QAxObject(parent);
- m_documents = NULL;
- m_document = NULL;
- m_bOpened = false;
- }
- QWord::~QWord()
- {
- close();
- }
- bool QWord::createWord(QString reportname) //创建一个新的word
- {
- QString ReportName = reportname;
- QString defaultFileName = QString("%1").arg(ReportName);
- m_saveName = QFileDialog::getSaveFileName(0, tr("Report Information"), defaultFileName, tr("*.doc"));
- // CGlobalAppData *pAppData = CAppDataSingleton::instance();
- QString SavePath = /*pAppData->m_strAppDirPath + */ "/ReportWord" + m_saveName;
- QFile file(m_saveName);
- if (file.exists()) {
- m_strError = tr("abnormal:the file already exists!");
- // m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
- return false;
- }
- if (!m_saveName.isEmpty()) {
- if (!m_word->setControl("Word.Application")) {
- m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
- // m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!");
- return false;
- }
- m_word->setProperty("Visible", false);
- m_word->setProperty("DisplayAlerts",
- false); //不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
- m_documents = m_word->querySubObject("Documents");
- m_documents->dynamicCall("Add (void)");
- m_document = m_word->querySubObject("ActiveDocument"); //获取当前激活的文档
- return true;
- } else {
- m_strError = tr("abnormal:the file name is empty!");
- // m_strError = QString::fromLocal8Bit("错误:文件名为空");
- return false;
- }
- }
- bool QWord::createNewWord(const QString &filePath)
- {
- m_saveName = filePath;
- QFile file(m_saveName);
- if (file.exists()) {
- file.remove(m_saveName);
- // m_strError = tr("error:the file already exists!");
- // m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
- // return false;
- }
- if (!m_saveName.isEmpty()) {
- if (!m_word->setControl("Word.Application")) {
- m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
- // m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!\n");
- return false;
- }
- m_word->setProperty("Visible", false);
- m_word->setProperty("DisplayAlerts",
- false); //不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
- m_documents = m_word->querySubObject("Documents");
- if (!m_documents) {
- m_strError = tr("abnormal:failed to get the documents!");
- // m_strError = QString::fromLocal8Bit("获取文档失败!\n");
- return false;
- }
- m_documents->dynamicCall("Add (void)");
- m_document = m_word->querySubObject("ActiveDocument"); //获取当前激活的文档
- return true;
- } else {
- m_strError = tr("abnormal:the file name is empty!");
- // m_strError = QString::fromLocal8Bit("错误:文件名为空");
- return false;
- }
- }
- bool QWord::openword(bool bVisable)
- {
- m_word = new QAxObject();
- bool bFlag = m_word->setControl("word.Application");
- if (!bFlag) {
- bFlag = m_word->setControl("kwps.Application");
- }
- if (!bFlag) {
- return false;
- }
- m_word->setProperty("Visible", bVisable);
- QAxObject *documents = m_word->querySubObject("Documents");
- if (!documents) {
- return false;
- }
- documents->dynamicCall("Add(QString)", m_strFilePath);
- m_document = m_word->querySubObject("ActiveDocument");
- m_bOpened = true;
- return m_bOpened;
- }
- bool QWord::open(const QString &strFilePath, bool bVisable)
- {
- m_strFilePath = strFilePath;
- // close();
- return openword(bVisable);
- }
- bool QWord::isOpen()
- {
- return m_bOpened;
- }
- void QWord::save()
- {
- if (m_document)
- m_document->dynamicCall("Save()");
- else
- return;
- }
- void QWord::close() //关闭 退出 析构时候也会自动调用一次
- {
- if (!m_saveName.isEmpty()) //如果不为空 则为新建
- {
- saveAs();
- m_saveName = "";
- }
- // if(m_document)
- // m_document->dynamicCall("Close (boolean)",false);
- // if(m_word)
- // m_word->dynamicCall("Quit (void)");
- if (m_documents)
- delete m_documents;
- if (m_word)
- delete m_word;
- m_document = NULL;
- m_documents = NULL;
- m_word = NULL;
- }
- void QWord::saveAs()
- {
- if (m_document)
- m_document->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(m_saveName));
- else
- return;
- }
- void QWord::setPageOrientation(int flag) //设置页面1 横向 还是 0竖向
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QString page;
- switch (flag) {
- case 0:
- page = "wdOrientPortrait";
- break;
- case 1:
- page = "wdOrientLandscape";
- break;
- }
- selection->querySubObject("PageSetUp")->setProperty("Orientation", page);
- }
- void QWord::setWordPageView(int flag)
- {
- QAxObject *viewPage = m_word->querySubObject("ActiveWindow");
- if (nullptr == viewPage) {
- return;
- }
- QString view;
- switch (flag) {
- case 1:
- view = "wdNormalView";
- break;
- case 2:
- view = "wdOutlineView";
- break;
- case 3:
- view = "wdPrintView";
- break;
- case 4:
- view = "wdPrintPreview";
- break;
- case 5:
- view = "wdMasterView";
- break;
- case 6:
- view = "wdWebView";
- break;
- case 7:
- view = "wdReadingView";
- break;
- case 8:
- view = "wdConflictView";
- break;
- }
- viewPage->querySubObject("View")->setProperty("Type", view);
- }
- void QWord::insertMoveDown() //插入回车
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->dynamicCall("TypeParagraph(void)");
- }
- void QWord::insertText(const QString &text)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->dynamicCall("TypeText(const QString&)", text);
- }
- QString QWord::GetText()
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- QString str;
- if (nullptr != selection) {
- str = selection->dynamicCall("GetText(void)").toString();
- }
- return str;
- }
- //设置选中位置文字居中 0 ,居左 1,居右 2
- void QWord::setParagraphAlignment(int flag)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- if (flag == 0) {
- selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");
- } else if (flag == 1) {
- selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify");
- } else if (flag == 2) {
- selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight");
- }
- }
- void QWord::setRowAlignment(int tableIndex, int row, int flag)
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
- 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");
- } else if (flag == 1) {
- range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify");
- } else if (flag == 2) {
- range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight");
- } else if (flag == 3) {
- range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphLeft");
- }
- }
- void QWord::setFontSize(int fontsize) //设置字体大小
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->querySubObject("Font")->setProperty("Size", fontsize);
- }
- void QWord::setFontBold(bool flag)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->querySubObject("Font")->setProperty("Bold", flag);
- }
- void QWord::setFontName(const QString &fontName)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->querySubObject("Font")->setProperty("Name", fontName);
- }
- void QWord::setSelectionRange(int start, int end)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- selection->dynamicCall("SetRange(int, int)", start, end); //第1个字符后开始,到第9个字符结束范围
- }
- void QWord::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)
- {
- QAxObject *range = m_document->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- *topLeftRow = range->property("Row").toInt();
- if (nullptr == topLeftRow) {
- return;
- }
- *topLeftColumn = range->property("Column").toInt();
- if (nullptr == topLeftColumn) {
- return;
- }
- QAxObject *rows = range->querySubObject("Rows");
- if (nullptr == rows) {
- return;
- }
- *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;
- if (nullptr == bottomRightRow) {
- return;
- }
- QAxObject *columns = range->querySubObject("Columns");
- if (nullptr == columns) {
- return;
- }
- *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
- if (nullptr == bottomRightColumn) {
- return;
- }
- }
- void QWord::intsertTable(int row, int column)
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *range = selection->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- QVariantList params;
- params.append(range->asVariant());
- params.append(row);
- params.append(column);
- tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
- QAxObject *table = selection->querySubObject("Tables(int)", 1);
- if (nullptr == table) {
- return;
- }
- table->setProperty("Style", "网格型");
- QAxObject *Borders = table->querySubObject("Borders");
- if (nullptr == Borders) {
- return;
- }
- Borders->setProperty("InsideLineStyle", 1);
- Borders->setProperty("OutsideLineStyle", 1);
- /*QString doc = Borders->generateDocumentation();
- QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
- outFile.open(QIODevice::WriteOnly|QIODevice::Append);
- QTextStream ts(&outFile);
- ts<<doc<<endl;*/
- }
- void QWord::intsertTable(int tableIndex, int row, int column)
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *range = selection->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- QVariantList params;
- params.append(range->asVariant());
- params.append(row);
- params.append(column);
- tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
- QAxObject *table = selection->querySubObject("Tables(int)", tableIndex);
- if (nullptr == table) {
- return;
- }
- table->setProperty("Style", "网格型");
- QAxObject *Borders = table->querySubObject("Borders");
- if (nullptr == Borders) {
- return;
- }
- Borders->setProperty("InsideLineStyle", 1);
- Borders->setProperty("OutsideLineStyle", 1);
- /*QString doc = Borders->generateDocumentation();
- QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
- outFile.open(QIODevice::WriteOnly|QIODevice::Append);
- QTextStream ts(&outFile);
- ts<<doc<<endl;*/
- }
- void QWord::setColumnWidth(int column, int width) //设置列宽
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- table->querySubObject("Columns(int)", column)->setProperty("Width", width);
- }
- void QWord::setCellString(int nTable, int row, int column, const QString &text)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (table) {
- table->querySubObject("Cell(int,int)", row, column)
- ->querySubObject("Range")
- ->dynamicCall("SetText(QString)", text);
- }
- }
- void QWord::MergeCells(int tableIndex, int nStartRow, int nStartCol, int nEndRow, int nEndCol) //合并单元格
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *table = tables->querySubObject("Item(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());
- }
- }
- //第二种方法调用
- // void QWord::MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol)//合并单元格
- // {
- // QAxObject* tables = m_document->querySubObject("Tables");
- // QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- // QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
- // QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
- // StartCell->dynamicCall("Merge(LPDISPATCH)",EndCell->asVariant());
- // }
- void QWord::setColumnHeight(int nTable, int column, int height)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (table) {
- table->querySubObject("Columns(int)", column)->setProperty("Hight", height);
- }
- }
- void QWord::setRowHeight(int nTable, int Row, int height)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (table) {
- table->querySubObject("Rows(int)", Row)->setProperty("Hight", height);
- }
- }
- void QWord::setColumnHeight(int column, int height) //设置列高
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (table) {
- table->querySubObject("Columns(int)", column)->setProperty("Hight", height);
- }
- }
- void QWord::setCellString(int row, int column, const QString &text)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- QAxObject *rows = table->querySubObject("Rows");
- if (rows) {
- return;
- }
- int Count = rows->dynamicCall("Count").toInt();
- table->querySubObject("Cell(int, int)", row, column)
- ->querySubObject("Range")
- ->dynamicCall("SetText(QString)", text);
- }
- void QWord::setCellFontBold(int row, int column, bool isBold) //设置内容粗体 isBold控制是否粗体
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
- }
- void QWord::setCellFontSize(int row, int column, int size) //设置文字大小
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- table->querySubObject("Cell(int, int)", row, column)
- ->querySubObject("Range")
- ->querySubObject("Font")
- ->setProperty("Size", size);
- }
- QVariant QWord::getCellValue(int row, int column) //获取单元格内容 此处对于Excel来说列和行从1开始最少
- {
- QAxObject *selection = m_word->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");
- }
- int QWord::getTableCount()
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- int val;
- if (nullptr != tables) {
- val = tables->property("Count").toInt();
- }
- return val;
- }
- void QWord::moveForEnd()
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QVariantList params;
- params.append(6);
- params.append(0);
- selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
- }
- void QWord::insertCellPic(int row, int column, const QString &picPath)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- QAxObject *range = table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath);
- }
- void QWord::setTableAutoFitBehavior(int flag)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- if (0 <= flag && flag <= 2)
- table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
- }
- void QWord::deleteSelectColumn(int column)
- {
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *table = selection->querySubObject("Tables(1)");
- if (nullptr == table) {
- return;
- }
- QAxObject *columns = table->querySubObject("Columns(int)", column);
- if (nullptr == columns) {
- return;
- }
- columns->dynamicCall("Delete()");
- }
- void QWord::setOptionCheckSpell(bool flags)
- {
- QAxObject *opetions = m_word->querySubObject("Options");
- if (!opetions)
- return;
- opetions->setProperty("CheckGrammarAsYouType", flags);
- opetions->setProperty("CheckGrammarWithSpelling", flags);
- opetions->setProperty("ContextualSpeller", flags);
- opetions->setProperty("CheckSpellingAsYouType", flags);
- }
- void QWord::addTableRow(int tableIndex, int nRow, int rowCount)
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
- if (nullptr == table) {
- return;
- }
- QAxObject *rows = table->querySubObject("Rows");
- if (nullptr == rows) {
- return;
- }
- int Count = rows->dynamicCall("Count").toInt();
- if (0 < nRow && nRow <= Count) {
- for (int i = 0; i < rowCount; ++i) {
- QString sPos = QString("Item(%1)").arg(nRow + i);
- QAxObject *row = rows->querySubObject(sPos.toStdString().c_str());
- QAxObject *row1 = rows->querySubObject("Last");
- QAxObject *row2 = rows->querySubObject("Row(int)", nRow + i);
- QAxObject *row3 = rows->querySubObject("Item(int)", nRow + i);
- if (nullptr != row) {
- QVariant param = row->asVariant();
- /*rows->dynamicCall("Add(Variant)",param);*/
- QAxObject *NewRow = rows->querySubObject("Add(Variant)", param);
- }
- }
- }
- }
- ////创建表格
- void QWord::insertTable(int tableIndex, int row, int column)
- {
- QAxObject *tables = m_document->querySubObject("Tables");
- if (nullptr == tables) {
- return;
- }
- QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
- if (nullptr == table) {
- return;
- }
- // QAxObject* rows =table->querySubObject("Rows");
- QAxObject *selection = m_word->querySubObject("Selection");
- if (nullptr == selection) {
- return;
- }
- QAxObject *range = selection->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- QVariantList params;
- params.append(range->asVariant());
- params.append(row);
- params.append(column);
- tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
- table = selection->querySubObject("Tables(int)", 1);
- table->setProperty("Style", "网格型");
- QAxObject *Borders = table->querySubObject("Borders");
- if (nullptr == Borders) {
- return;
- }
- Borders->setProperty("InsideLineStyle", 1);
- Borders->setProperty("OutsideLineStyle", 1);
- }
- ////设置表格列宽
- void QWord::setColumnWidth(int nTable, int column, int width)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (table) {
- table->querySubObject("Columns(int)", column)->setProperty("width", width);
- }
- }
- //在表格中插入图片
- void QWord::insertCellPic(int nTable, int row, int column, const QString &picPath)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (nullptr == table) {
- return;
- }
- QAxObject *range = table->querySubObject("Cell(int,int )", row, column)->querySubObject("Range");
- if (nullptr == range) {
- return;
- }
- range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath);
- }
- //设置内容粗体
- void QWord::setCellFontBold(int nTable, int row, int column, bool isBold)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (nullptr == table) {
- return;
- }
- table->querySubObject("Cell(int,int )", row, column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
- }
- //设置文字大小
- void QWord::setCellFontSize(int nTable, int row, int column, int size)
- {
- QAxObject *pTables = m_document->querySubObject("Tables");
- if (nullptr == pTables) {
- return;
- }
- QAxObject *table = pTables->querySubObject("Item(int)", nTable);
- if (nullptr == table) {
- return;
- }
- table->querySubObject("Cell(int,int)", row, column)
- ->querySubObject("Range")
- ->querySubObject("Font")
- ->setProperty("Size", size);
- }
- void QWord::setVisible(bool isVisible)
- {
- if (m_word != nullptr) {
- m_word->setProperty("Visible", isVisible);
- }
- }
- #endif
|