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();//游標跳出表格
/*然後,可以做其他操作了*/
}