日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt 使用QAxObject操作Word

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。