123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272 |
- #include "qword.h"
- #include <QDateTime>
- #include <QFileDialog>
- #include <QFile>
- #include <QTextStream>
- #include <ActiveQt/QAxObject>
- #include <ActiveQt/QAxWidget>
- #include<QTextStream>
- #include <ActiveQt/QAxBase>
- #include <QTextFormat>
- 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_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(NULL== selection)
- {
- return;
- }
- QString page;
- switch (flag)
- {
- case 0:
- page = "wdOrientPortrait";
- break;
- case 1:
- page = "wdOrientLandscape";
- break;
- }
- selection->querySubObject("PageSetUp")->setProperty("Orientation",page);
- delete selection;
- }
- void QWord::setWordPageView(int flag)
- {
- QAxObject* viewPage = m_word->querySubObject("ActiveWindow");
- if(NULL== 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);
- delete viewPage;
- }
- void QWord::insertMoveDown() //插入回车
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- selection->dynamicCall("TypeParagraph(void)");
- delete selection;
- }
- void QWord::insertText(const QString& text)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- selection->dynamicCall("TypeText(const QString&)",text);
- delete selection;
- }
- QString QWord::GetText()
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- QString str;
- if(NULL!= selection)
- {
- str = selection->dynamicCall("GetText(void)").toString();
- }
- delete selection;
- return str;
- }
- //设置选中位置文字居中 0 ,居左 1,居右 2
- void QWord::setParagraphAlignment(int flag)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== 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");
- }
- delete selection;
- }
- void QWord::setParagraphFormatOutlineLevel(QString level)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- // QString wdOutlineLevel="wdOutlineLevel";
- selection->querySubObject("ParagraphFormat")->setProperty("OutlineLevel",level);
- delete selection;
- }
- void QWord::setRowAlignment(int tableIndex,int row,int flag)
- {
- QAxObject* tables = m_document->querySubObject("Tables");
- if(NULL== tables)
- {
- return;
- }
- QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- if(NULL== table )
- {
- return;
- }
- QAxObject* Row= table->querySubObject("Rows(int)",row);
- if(NULL== Row)
- {
- return;
- }
- QAxObject* range = Row->querySubObject("Range");
- if(NULL== 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::setRowAlignment(int row,int flag)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== table)
- {
- return;
- }
- // QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- // if(NULL== table )
- // {
- // return;
- // }
- QAxObject* Row= table->querySubObject("Rows(int)",row);
- if(NULL== Row)
- {
- return;
- }
- QAxObject* range = Row->querySubObject("Range");
- if(NULL== 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(NULL== selection)
- {
- return;
- }
- selection->querySubObject("Font")->setProperty("Size",fontsize);
- delete selection;
- }
- void QWord::setFontBold(bool flag)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- selection->querySubObject("Font")->setProperty("Bold",flag);
- delete selection;
- }
- void QWord::setFontName(QString& fontName)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- selection->querySubObject("Font")->setProperty("Name",fontName);
- delete selection;
- }
- void QWord::setSelectionRange(int start,int end)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== 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(NULL== range)
- {
- return;
- }
- *topLeftRow = range->property("Row").toInt();
- if(NULL== topLeftRow)
- {
- return;
- }
- *topLeftColumn = range->property("Column").toInt();
- if(NULL== topLeftColumn)
- {
- return;
- }
- QAxObject *rows = range->querySubObject("Rows");
- if(NULL== rows)
- {
- return;
- }
- *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;
- if(NULL== bottomRightRow)
- {
- return;
- }
- QAxObject *columns = range->querySubObject("Columns");
- if(NULL== columns)
- {
- return;
- }
- *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
- if(NULL== bottomRightColumn)
- {
- return;
- }
- }
- void QWord::intsertTable(int row,int column)
- {
- QAxObject* tables = m_document->querySubObject("Tables");
- if(NULL== tables)
- {
- return;
- }
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- QAxObject* range = selection->querySubObject("Range");
- if(NULL== 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(NULL== table)
- {
- return;
- }
- table->setProperty("Style","网格型");
- QAxObject* Borders = table->querySubObject("Borders");
- if(NULL== 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(NULL== tables)
- {
- return;
- }
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- QAxObject* range = selection->querySubObject("Range");
- if(NULL== 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(NULL== table)
- {
- return;
- }
- table->setProperty("Style","网格型");
- QAxObject* Borders = table->querySubObject("Borders");
- if(NULL== 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(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== 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(NULL==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(NULL==tables)
- {
- return;
- }
- QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- if(NULL== table)
- {
- return;
- }
- if(table)
- {
- QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
- QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
- if(NULL==StartCell)
- {
- return;
- }
- if(NULL== 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(NULL==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(NULL==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(NULL== 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(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== 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(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== 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(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== 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(NULL!=selection&&NULL!=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(NULL!= tables)
- {
- val = tables->property("Count").toInt();
- }
- return val;
- }
- void QWord::moveForEnd()
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== 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(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== table)
- {
- return;
- }
- QAxObject* range = table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range");
- if(NULL== range)
- {
- return;
- }
- range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picPath);
- }
- void QWord::setTableAutoFitBehavior(int flag)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== table)
- {
- return;
- }
- if(0 <= flag &&flag <= 2 )
- table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
- }
- void QWord::deleteSelectColumn(int column)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL== selection)
- {
- return;
- }
- QAxObject* table = selection->querySubObject("Tables(1)");
- if(NULL== table)
- {
- return;
- }
- QAxObject* columns = table->querySubObject("Columns(int)",column);
- if(NULL== 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(NULL== tables)
- {
- return;
- }
- QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- if(NULL== table)
- {
- return;
- }
- QAxObject* rows =table->querySubObject("Rows");
- if(NULL== 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 (NULL != 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(NULL==tables)
- {
- return;
- }
- QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
- if(NULL==table)
- {
- return;
- }
- //QAxObject* rows =table->querySubObject("Rows");
- QAxObject* selection = m_word->querySubObject("Selection");
- if(NULL==selection)
- {
- return;
- }
- QAxObject* range = selection->querySubObject("Range");
- if(NULL==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(NULL==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(NULL== 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(NULL== pTables)
- {
- return;
- }
- QAxObject* table=pTables->querySubObject("Item(int)",nTable);
- if(NULL== table)
- {
- return;
- }
- QAxObject* range=table->querySubObject("Cell(int,int )",row,column)->querySubObject("Range");
- if(NULL== 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(NULL== pTables)
- {
- return;
- }
- QAxObject* table =pTables->querySubObject("Item(int)",nTable);
- if(NULL== 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(NULL== pTables)
- {
- return;
- }
- QAxObject* table =pTables->querySubObject("Item(int)",nTable);
- if(NULL== table)
- {
- return;
- }
- table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->querySubObject("Font")->setProperty("Size",size);
- }
- void QWord::setVisible(bool isVisible)
- {
- if(m_word != NULL)
- {
- m_word->setProperty("Visible",isVisible);
- }
- }
- void QWord::AddPicture(QString file)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(!selection)
- {
- return;
- }
- QString filename = file;
- filename.replace("/","\\");
- QAxObject *Inlineshapes = selection->querySubObject("InlineShapes");
- Inlineshapes->dynamicCall("AddPicture(const QString&)",filename);
- delete Inlineshapes;
- delete selection;
- }
- void QWord::insertEnter()
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(!selection)
- {
- return;
- }
- selection->dynamicCall("TypeParagraph(void)");
- }
- QAxObject* QWord::createTableWithColor(int row, int column)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(!selection)
- {
- return NULL;
- }
- selection->dynamicCall("InsertAfter(QString&)", "\r\n");
- QAxObject *range = selection->querySubObject("Range");
- QAxObject *tables = m_document->querySubObject("Tables");
- QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column);
- table->setProperty("Style","网格型");
- QAxObject* Borders = table->querySubObject("Borders");
- if(NULL== Borders)
- {
- return NULL;
- }
- Borders->setProperty("InsideLineStyle",1);
- Borders->setProperty("OutsideLineStyle",1);
- table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
- //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
- table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", wdAutoFitContent);
- // //设置表头
- // 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);
- // }
- // //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
- // table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);
- delete range;
- return table;
- }
- QAxObject* QWord::createTable(int row, int column)
- {
- QAxObject* selection = m_word->querySubObject("Selection");
- if(!selection)
- {
- return NULL;
- }
- selection->dynamicCall("InsertAfter(QString&)", "\r\n");
- QAxObject *range = selection->querySubObject("Range");
- QAxObject *tables = m_document->querySubObject("Tables");
- QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column);
- table->setProperty("Style","网格型");
- QAxObject* Borders = table->querySubObject("Borders");
- if(NULL== Borders)
- {
- return NULL;
- }
- Borders->setProperty("InsideLineStyle",1);
- Borders->setProperty("OutsideLineStyle",1);
- //table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
- //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
- table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", wdAutoFitContent);
- // //设置表头
- // 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);
- // }
- // //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
- // table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);
- delete range;
- return table;
- }
- void QWord::setCellText(QAxObject *table, int row, int column, QString text,bool bold)
- {
- table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
- table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->dynamicCall("SetBold(int)", bold);
- //table->querySubObject("Cell(int,int)",row,column)->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", colorToInt(Qt::yellow));
- }
- void QWord::autoFitBehavior(QAxObject *table, int flag)
- {
- //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
- table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
- }
- void QWord::insertNewPage(void)
- {
- QAxObject* activeWindow = m_document->querySubObject( "ActiveWindow" );
- QAxObject* selection = activeWindow->querySubObject( "Selection" );
- selection->dynamicCall( "Collapse(int)", 0 );
- selection->dynamicCall( "InsertNewPage()" );
- selection->dynamicCall( "Collapse(int)", 0 );
- delete selection;
- delete activeWindow;
- }
- void QWord::setColor( QAxObject *obj,QColor color )
- {
- if (!obj) return;
- obj->querySubObject("Range")
- ->querySubObject("ParagraphFormat")
- ->querySubObject("Shading")
- ->setProperty("ForegroundPatternColor",colorToInt(color));
- }
- void QWord::setBgColor( QAxObject *obj,QColor color )
- {
- if (!obj) return;
- obj->querySubObject("Range")
- ->querySubObject("ParagraphFormat")
- ->querySubObject("Shading")
- ->setProperty("BackgroundPatternColor",colorToInt(color));
- }
- int QWord::colorToInt( QColor color )
- {
- int sum = 0;
- int b = color.red() << 16;
- int g = color.green() << 8;
- int r = color.blue() ;
- int al = color.alpha() <<24;
- sum = al + r + g + b;
- return sum;
- }
- /******************************************************************************
- * 函数:addTableRow
- * 功能:为表格添加行
- * 参数:table 表格; nRow 插入行; rowCount 插入的行数
- *****************************************************************************/
- void QWord::addTableRow(QAxObject *table, int nRow, int rowCount)
- {
- QAxObject* rows = table->querySubObject("Rows");
- int Count = rows->dynamicCall("Count").toInt();
- if (nRow > Count)
- {
- nRow = Count;
- }
- QAxObject* row = table->querySubObject("Rows(int)",nRow);
- if (row == NULL)
- {
- row = rows->querySubObject("Last");
- }
- if(0 <= nRow && nRow <=Count)
- {
- for(int i = 0; i < rowCount; ++i)
- {
- rows->dynamicCall("Add(QVariant)", row->asVariant());
- }
- }
- }
- void QWord::appendTableRow( QAxObject *table,int rowCount )
- {
- QAxObject* rows = table->querySubObject("Rows");
- int Count = rows->dynamicCall("Count").toInt();
- QAxObject* row = rows->querySubObject("Last");
- for(int i = 0; i < rowCount; ++i)
- {
- QVariant param = row->asVariant();
- rows->dynamicCall("Add(Variant)", param);
- }
- }
- /******************************************************************************
- * 函数:setColumnWidth
- * 功能:设置表格列宽
- * 参数:table 表格; column 列数; width 宽度
- *****************************************************************************/
- void QWord::setColumnWidth(QAxObject *table, int column, int width)
- {
- table->querySubObject("Columns(int)", column+1)->setProperty("Width", width);
- }
- void QWord::MergeCells(QAxObject *table, int nStartRow,int nStartCol,int nEndRow,int nEndCol)
- {
- QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow+1,nStartCol+1);
- QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow+1,nEndCol+1);
- StartCell->dynamicCall("Merge(LPDISPATCH)",EndCell->asVariant());
- }
- QString QWord::getTitleStr( TITLE_NUMBER number )
- {
- QString str;
- switch (number)
- {
- case TITLE_ONE : str = "标题 1"; break;
- case TITLE_TWO : str = "标题 2"; break;
- case TITLE_THREE : str = "标题 3"; break;
- default: str = "正文";break;
- }
- return str;
- }
- bool QWord::addText1( QString titlestr,TITLE_NUMBER number /*= NORMAL*/,WdParagraphAlignment alignment /*= AlignParLeft*/ )
- {
- if (!m_bOpened) return false;
- QAxObject *selection = NULL;
- selection = m_word->querySubObject("Selection");
- if (selection)
- {
- selection->querySubObject("Range")->setProperty("Text", titlestr);
- selection->querySubObject("Range")->dynamicCall("SetStyle(QVariant)", getTitleStr(number));
- selection->querySubObject("ParagraphFormat")->setProperty("Alignment",alignment); //文本位置设置
- //背景色
- //selection->querySubObject("Range")
- // ->querySubObject("ParagraphFormat")
- // ->querySubObject("Shading")
- // ->setProperty("BackgroundPatternColor",QColor("blue").value());
- //moveToEnd();
- return true;
- }
- return false;
- }
- bool QWord::addText( QString titlestr,QFont font,QColor fontcolor )
- {
- if (!m_bOpened) return false;
- QAxObject *selection = NULL;
- selection = m_word->querySubObject("Selection");
- if (selection)
- {
- //selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", fo); //不好用
- //selection->querySubObject("Range")->querySubObject("Font")->dynamicCall("Size", 20);
- selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", QVariant(font.pointSize()));
- selection->querySubObject("Range")->querySubObject("Font")->setProperty("Color", colorToInt(fontcolor));
- if (font.weight() >= QFont::Bold)
- {
- selection->querySubObject("Range")->querySubObject("Font")->setProperty("Bold", true);
- }
- selection->querySubObject("Range")->setProperty("Text", titlestr); //方式1
- //selection->dynamicCall("TypeText(const QString&)",titlestr); //方式2 使用方法2不能设置字体
- // moveToEnd();
- return true;
- }
- return false;
- }
- QAxObject* QWord::addText2( QString titlestr )
- {
- QAxObject *selection = NULL;
- if (!m_bOpened) return selection;
- selection = m_word->querySubObject("Selection");
- if (selection)
- {
- selection->querySubObject("Range")->setProperty("Text", titlestr); //方式1
- }
- return selection;
- }
- void QWord::setTableBgColor(QAxObject *table, uint32_t color)
- {
- table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
- }
|