Qt 使用QAxObject操作Word
因為最近項中需要生成報表,所以網上查了資料,因為VB看不懂所以總結的也不多,在實現中沒有使用標簽只是以光標的位置來插入,包括:創建文件,排版方式,添加文字,添加圖片,添加表格,表格添加文字(圖片),光標移動到尾部,光標跳轉(類似tab)等
添加 QT += axcontainer
QAxObject *m_pWord; ? ? ?//指向整個Word應用程序
 QAxObject *m_pWorkDocuments; ?//指向文檔集,Word有很多文檔
 QAxObject *m_pWorkDocument; ? //指向m_sFile對應的文檔,就是要操作的文檔
/*
 ? ? 創建word
 */
void MainWindow::open()
 {
 ? ? m_pWord = new QAxObject();
 ? ? bool flag = m_pWord->setControl( "Word.Application" );
 ? ? if(!flag)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? m_pWord->setProperty("Visible", true);
? ? QAxObject *document = m_pWord->querySubObject("Documents");
 ? ? if(!document)
 ? ? {
 ? ? ? ? return ;
 ? ? }
? ? //獲取當前激活的文檔
 ? ? m_pWorkDocument = m_pWord->querySubObject("ActiveDocument");
 }
/*
 ?* 排版方式
 ?* 縱行、橫行
 */
void MainWindow::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 MainWindow::pageWidth()
 {
 ? ? int width;
 ? ? QAxObject* selection = m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? width = selection->querySubObject("PageSetup")->property("PageWidth").toInt();;
 ? ? return width;
 }
/*
 *設置字號
 */
void MainWindow::setFontSize(int size)
 {
 ? ? QAxObject* selection = m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? selection->querySubObject("Font")->setProperty("Size",size);
 }
/*
 ? ?設置對齊方式
 */
void MainWindow::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 MainWindow::typeText(QString text)
 {
 ? ? QAxObject* selection ?= m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? selection->dynamicCall("TypeText(const QString&)",text);
 }
 /*
 ? ? 插入圖片
 */
void MainWindow::AddPicture(QString file)
 {
 ? ? QAxObject* selection ?= m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? QString filename = file;
 ? ? filename.replace("/","\\");
 ? ? QAxObject *Inlineshapes = selection->querySubObject("InlineShapes");
 ? ? Inlineshapes->dynamicCall("AddPicture(const QString&)",filename);
 ? ? delete Inlineshapes;
 }
/*
 ? ? 插入回車
 */
void MainWindow::insertEnter()
 {
 ? ? QAxObject* selection ?= m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? selection->dynamicCall("TypeParagraph(void)");
 }
/*
 ?* 光標移到末尾,跳出單元格
 */
 void MainWindow::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* MainWindow::createTable(int row, int column,QStringList headList)
 {
 ? ? 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);
 ? ? }
 ? ? return table;
 }
/*
 ? ?填充表格
 */
void MainWindow::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 MainWindow::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 MainWindow::moveRight()
 {
 ? ? QAxObject* selection ?= m_pWord->querySubObject("Selection");
 ? ? if(!selection)
 ? ? {
 ? ? ? ? return;
 ? ? }
 ? ? selection->dynamicCall("MoveRight(int)",1);
 }
/*特殊樣例*/
void MainWindow::Example()
 {
 ? ? QStringList header;
? ? newTable(1,2,header);//創建表格
? ? typeText(tr("文字1"));//添加文字
 ? ? insertEnter();//換行
 ? ? insertText("");
 ? ? AddPicture("圖像1");//插入圖片
? ? moveRight();//將光標插入到下個單元格中
? ? typeText(tr("文字2"));
 ? ? insertEnter();
 ? ? insertText("");
 ? ? AddPicture("圖像2");
? ? moveForEnd();//光標跳出表格
? ? /*然后,可以做其他操作了*/
}
總結
以上是生活随笔為你收集整理的Qt 使用QAxObject操作Word的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 软件设计师考试感想随笔
- 下一篇: java中ejb项目_创建EJB项目
