QAxObject在Word光标或标签处插⼊⽂字、图⽚、表格、标题1等QAxObject操作word
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyText");//⽂本
textstyleselection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1");//标题1
#QAxObject在光标处插⼊
/*
* 排版⽅式
* 纵⾏、横⾏
*/
void WordEngine::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 WordEngine::pageWidth()
{
int width;
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return width;
}
width = selection->querySubObject("PageSetup")->property("PageWidth").toInt();;
return width;
}
/*
*设置字号
*/
void WordEngine::setFontSize(int size)
{
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
selection->querySubObject("Font")->setProperty("Size", size);
}
/*
设置对齐⽅式
*/
void WordEngine::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 WordEngine::typeText(QString text, int index, int WdBuiltinStyle, bool bold, int size)
{
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
setAlignment(index);
setFontSize(size);
if (bold)
selection->querySubObject("Range")->dynamicCall("SetBold(int)", true); //if(WdBuiltinStyle==0)
// selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyText"); if(WdBuiltinStyle==1)
selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1"); else if (WdBuiltinStyle == 2)
selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading2"); else if (WdBuiltinStyle == 3)
selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading3"); else if (WdBuiltinStyle == 4)
selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading4"); selection->dynamicCall("TypeText(const QString&)", text);
}
/*
插⼊图⽚
*/
void WordEngine::AddPicture(QString file)
{
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
setAlignment(0);
QString filename = file;
QAxObject *Inlineshapes = selection->querySubObject("InlineShapes"); Inlineshapes->dynamicCall("AddPicture(const QString&)", filename);
delete Inlineshapes;
}
/*
插⼊回车
*/
void WordEngine::insertEnter()
{
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
selection->dynamicCall("TypeParagraph(void)");
}
/*
* 光标移到末尾,跳出单元格
*/
void WordEngine::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* WordEngine::createTable(int row, int column, QStringList headList,bool SetLineStyleHide)
{
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);
table->querySubObject("Cell(int,int)", 1, i + 1)->querySubObject("Font")->setProperty("Size", 10);
}
if (!SetLineStyleHide)
{
for (int i = 1; i <= 6; i++)
{
QString str = QString("Borders(-%1)").arg(i);
QAxObject *borders = table->Latin1().constData());
borders->dynamicCall("SetLineStyle(int)", 1);
}
}
return table;
}
/*
填充表格
*/
void WordEngine::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 WordEngine::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 WordEngine::moveRight()
{
QAxObject* selection = m_pWord->querySubObject("Selection");
if (!selection)
{
return;
}
selection->dynamicCall("MoveRight(int)", 1);
}
#QAxObject在标签处插⼊
/*****标签处添加字符串
******input:(QString)word模板地址,(bool)⽣成word⽂档是否可视
******output:
*/
bool WordEngine::Open(QString sFile, bool bVisible)
{
//新建⼀个word应⽤程序
m_pWord = new QAxObject();
bool bFlag = m_pWord->setControl("word.Application");
if (!bFlag)
{
return false;
}
QObject::connect(m_pWord, SIGNAL(exception(int, QString, QString, QString)),
this, SLOT(saveLastError(int, QString, QString, QString)));
m_pWord->setProperty("Visible", bVisible);
//获取所有的⼯作⽂档
QAxObject *document = m_pWord->querySubObject("Documents");
if (!document)
{
return false;
}
//以⽂件template.dot为模版新建⼀个⽂档
document->dynamicCall("Add(QString)", sFile);
//获取当前激活的⽂档
m_pWorkDocument = m_pWord->querySubObject("ActiveDocument");
if (m_pWorkDocument)
m_bIsOpen = true;
else
m_bIsOpen = false;
return m_bIsOpen;
}
/
*****标签处添加字符串
******input:(QString)保存地址
******output:
*/
void WordEngine::save(QString sSavePath)
{
if (m_bIsOpen && m_pWorkDocument)
{
if (m_bNewFile) {
m_pWorkDocument->dynamicCall("Save()");
}
else {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。