最近要新建一个word并写入相关内容。于是写了一个测试的例子,采用的是QAxObject实现相关操作,需要在pro添加QT +=qaxcontainer,即添加ActiveQt Container模块。最重要的函数querySubObject和dynamicCall两个函数。当然最重要的是知道office组件提供了哪些函数和属性可以调用,查看office提供的相关函数以及说明。
具体代码如下
qwordengine.h
#ifndef QWORDENGINE_H
#define QWORDENGINE_H
#include
#include
#include
#include
#include
#include
#include
/*
* 微软的word的查看函数网站
* https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word?view=word-pia
*
* 参考https://blog.csdn.net/u010304326/article/details/82292195#comments
* 参考https://blog.csdn.net/qq_35192280/article/details/83021975
* https://blog.csdn.net/zy47675676/article/details/86251991 表格垂直居中、水平居中
*/
enum TITLE_NUMBER
{
TITLE_ONE = 0,
TITLE_TWO,
TITLE_THREE,
NORMAL
};
//MOVEEND_INDEX来自于https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word.wdunits?view=word-pia
enum MOVEEND_INDEX
{
wdParagraph = 4, //段落。
wdStory = 6 , //部分。
wdRow = 10, //行
wdParagraphFormatting = 14, //段落格式。
wdTable = 15 //表格
};
//文本对齐方式
enum WdParagraphAlignment
{
AlignParLeft = 0, //左对齐
AlignParCenter = 1, //居中对齐。
AlignParRight = 2, //右对齐。
AlignParJustify = 3, //完全两端对齐。
};
class QWordEngine : public QObject
{
Q_OBJECT
public:
QWordEngine(QObject *parent = NULL);
QWordEngine(QString filename,QObject *parent= NULL);
~QWordEngine();
//打开文件 bVisable 是否显示弹窗
bool open(bool bvisable = false);
bool open(const QString& strFile, bool bVisable = false);
///////////////////////////////////////////////////////////////////////////
//关闭文件
bool close();
bool isOpen();
//保存
void save();
bool saveAs(const QString& strSaveFile);
//////////////////////////////////////////////////////////////////////////
//添加文本 titlestr 添加的文本 number 标题还是正文,默认是正文
bool addText(QString titlestr,TITLE_NUMBER number = NORMAL ,WdParagraphAlignment alignment = AlignParLeft);
//默认是黑色,问题:没有实现在一行实现不同颜色
bool addText(QString titlestr,QFont font,QColor fontcolor = Qt::black);
//返回QAxObject方便设置颜色以及其他样式
QAxObject* addText2(QString titlestr);
//
//////////////////////////////////////////////////////////////////////////
//功能:插入回车
bool insertEnter();
//////////////////////////////////////////////////////////////////////////
//光标移动到最后
bool moveToEnd();
bool moveToEnd(MOVEEND_INDEX wd);
//移动到表格的最下面一行
bool moveToEnd(QAxObject *table);
//////////////////////////////////////////////////////////////////////////
//表格操作====================================================================
/******************************************************************************
* 函数:insertTable
* 功能:创建表格
* 参数:nStart 开始位置; nEnd 结束位置; row 行; column 列
* 返回值: QAxObject*
*****************************************************************************/
QAxObject* insertTable(int nStart, int nEnd, int row, int column);
/******************************************************************************
*创建表格
*QStringList headList 添加表头
******************************************************************************/
QAxObject* createTable(int row, int column,QStringList headList = QStringList());
//设置列宽
void setColumnWidth(QAxObject *table, int column, int width);
// 为表格添加行
void addTableRow(QAxObject *table, int nRow, int rowCount);
void appendTableRow(QAxObject *table,int rowCount);
/******************************************************************************
* 函数:setCellString
* 功能:设置表格内容
* 参数:table 表格; row 行数; column 列数; text 插入文本 row 和 column从0开始
*****************************************************************************/
void setCellString(QAxObject *table, int row, int column, const QString& text);
// 设置内容粗体 isBold控制是否粗体
void setCellFontBold(QAxObject *table, int row, int column, bool isBold);
// 设置文字大小
void setCellFontSize(QAxObject *table, int row, int column, int size);
// 在表格中插入图片
void insertCellPic(QAxObject *table, int row,int column,const QString& picPath);
/******************************************************************************
* 函数:MergeCells
* 功能:合并单元格
* 参数:table 表格; nStartRow 起始单元格行数; nStartCol ; nEndRow ; nEndCol
*****************************************************************************/
void MergeCells(QAxObject *table, int nStartRow,int nStartCol,int nEndRow,int nEndCol);
//===============================================================================
//插入图片 picPath 图片路径
void insertPic(QString picPath);
//文字对齐方式
void setAlignment(int index);
//设置颜色 不能直接利用QColor需要将颜色转成int值
void setColor(QColor color);
void setColor(QAxObject *obj,QColor color);
void setBgColor(QAxObject *obj,QColor color);
//设置字号
void setFontSize(int size);
private:
void writeFile( QString savestr,QString filename );
QString getTitleStr(TITLE_NUMBER number); //返回标题字符串
void setPropraty(QAxObject *axobj,QString proname,QVariant provalue); //设置某个对象的某个属性值
int colorToInt(QColor color); //将颜色转化成整数,因为QColor("blue").value()是255,不是想要的结果
private:
QString m_filename;
bool m_bOpened;
QAxObject *m_wordDocuments;
QAxWidget *m_wordWidget;
WdParagraphAlignment m_paralignment; //文本对齐方式
};
#endif // QWORDENGINE_H
qwordengine.cpp
#include "qwordengine.h"
QWordEngine::QWordEngine(QObject *parent)
: QObject(parent),m_bOpened(false)
{
}
QWordEngine::QWordEngine( QString filename,QObject *parent/*= NULL*/ )
:QObject(parent), m_filename(filename) ,m_bOpened(false)
{
}
QWordEngine::~QWordEngine()
{
close();
}
bool QWordEngine::open( bool bvisable )
{
m_bOpened = false;
m_wordWidget = new QAxWidget;
bool bFlag = m_wordWidget->setControl("Word.Application" );
//if(!bFlag)
//{
// // 用wps打开
// bFlag = m_wordWidget->setControl( "kwps.Application" );
// if(!bFlag)
// {
// return m_bOpened;
//
// }
//}
m_wordWidget->setProperty("Visible", bvisable);
//获取所有的工作文档
QAxObject *document = m_wordWidget->querySubObject("Documents");
if(!document)
{
return m_bOpened;
}
//新建一个文档页
document->dynamicCall("Add()");
//获取当前激活的文档
m_wordDocuments = m_wordWidget->querySubObject("ActiveDocument");
if (m_wordDocuments)
m_bOpened = true;
else
m_bOpened = false;
return m_bOpened;
}
bool QWordEngine::open( const QString& strFile, bool bVisable /*= false*/ )
{
m_filename = strFile;
close();
return open(bVisable);
}
bool QWordEngine::close()
{
if (m_bOpened)
{
if (m_wordDocuments)
{
m_wordDocuments->dynamicCall("Close (boolean)", true);
}
if(m_wordWidget)
{
m_wordWidget->dynamicCall("Quit()");//退出word
m_wordWidget->close();
}
if(m_wordDocuments)
delete m_wordDocuments;
if(m_wordWidget)
delete m_wordWidget;
m_bOpened = false;
}
return m_bOpened;
}
bool QWordEngine::isOpen()
{
return m_bOpened;
}
void QWordEngine::save()
{
//QDir dir;
//QString dstPath = dir.currentPath() + "/" + QString::fromLocal8Bit("测试报告3")+".doc";
QVariant newFileName(m_filename);//保存路径及名称
QVariant fileFormat(1);//文件格式
m_wordDocuments->dynamicCall("SaveAs(const QVariant&, const QVariant&)", newFileName, fileFormat);
m_wordDocuments->dynamicCall("Close (boolean)", true);
}
bool QWordEngine::saveAs(const QString& strSaveFile)
{
return m_wordDocuments->dynamicCall("SaveAs (const QString&)",
strSaveFile).toBool();
}
bool QWordEngine::addText( QString titlestr,TITLE_NUMBER number /*= NORMAL*/,WdParagraphAlignment alignment /*= AlignParLeft*/ )
{
if (!m_bOpened) return false;
QAxObject *selection = NULL;
selection = m_wordWidget->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 QWordEngine::addText( QString titlestr,QFont font,QColor fontcolor )
{
if (!m_bOpened) return false;
QAxObject *selection = NULL;
selection = m_wordWidget->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* QWordEngine::addText2( QString titlestr )
{
QAxObject *selection = NULL;
if (!m_bOpened) return selection;
selection = m_wordWidget->querySubObject("Selection");
if (selection)
{
selection->querySubObject("Range")->setProperty("Text", titlestr); //方式1
}
return selection;
}
bool QWordEngine::insertEnter()
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
if (selection)
{
selection->dynamicCall("TypeParagraph(void)");
return true;
}
return false;
}
bool QWordEngine::moveToEnd()
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
QVariantList params;
params << wdStory << 0;
if (selection)
{
selection->dynamicCall("EndOf(QVariant&, QVariant&)", params);
return true;
}
return false;
}
bool QWordEngine::moveToEnd( MOVEEND_INDEX wd )
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
QVariantList params;
params << wd << 0;
selection->dynamicCall("EndOf(QVariant&, QVariant&)", params);
return true;
}
bool QWordEngine::moveToEnd( QAxObject *table )
{
if (!table) return false;
moveToEnd(wdTable);
moveToEnd();
return true;
}
QAxObject* QWordEngine::createTable( int row, int column,QStringList headList )
{
QAxObject* selection = m_wordWidget->querySubObject("Selection");
if (!selection) return NULL;
selection->dynamicCall("InsertAfter(QString&)", "\r\n");
QAxObject *range = selection->querySubObject("Range");
QAxObject *tables = m_wordDocuments->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;iquerySubObject("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;
}
QAxObject* QWordEngine::insertTable(int nStart, int nEnd, int row, int column)
{
QAxObject* ptst = m_wordDocuments->querySubObject( "Range( Long, Long )",
nStart, nEnd );
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QVariantList params;
params.append(ptst->asVariant());
params.append(row);
params.append(column);
if( pTables )
{
QAxObject *table = pTables->querySubObject( "Add(QAxObject*, Long ,Long )",params);
table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);
return table;
}
return NULL;
}
void QWordEngine::setCellString(QAxObject *table, int row, int column, const QString& text)
{
if(table)
{
QAxObject *cell = table->querySubObject("Cell(int, int)",row+1,column+1);
QAxObject *range = table->querySubObject("Range");
range->dynamicCall("SetText(QString)", text);
range->dynamicCall("SetBold(int)", false);
}
}
void QWordEngine::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());
}
/******************************************************************************
* 函数:setColumnWidth
* 功能:设置表格列宽
* 参数:table 表格; column 列数; width 宽度
*****************************************************************************/
void QWordEngine::setColumnWidth(QAxObject *table, int column, int width)
{
table->querySubObject("Columns(int)", column+1)->setProperty("Width", width);
}
/******************************************************************************
* 函数:addTableRow
* 功能:为表格添加行
* 参数:table 表格; nRow 插入行; rowCount 插入的行数
*****************************************************************************/
void QWordEngine::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 QWordEngine::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);
}
}
/******************************************************************************
* 函数:setCellFontBold
* 功能:设置内容粗体 isBold控制是否粗体
* 参数:table 表格; row 插入行; column 列数; isBold 是否加粗
*****************************************************************************/
void QWordEngine::setCellFontBold(QAxObject *table, int row, int column, bool isBold)
{
table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")
->dynamicCall("SetBold(int)", isBold);
}
/******************************************************************************
* 函数:setCellFontSize
* 功能:设置文字大小
* 参数:table 表格; row 插入行; column 列数; size 字体大小
*****************************************************************************/
void QWordEngine::setCellFontSize(QAxObject *table, int row, int column, int size)
{
table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")
->querySubObject("Font")->setProperty("Size", size);
}
/******************************************************************************
* 函数:insertCellPic
* 功能:在表格中插入图片
* 参数:table 表格; row 插入行; column 列数; picPath 图片路径
*****************************************************************************/
void QWordEngine::insertCellPic(QAxObject *table, int row, int column,
const QString& picPath)
{
QAxObject* range = table->querySubObject("Cell(int, int)", row, column)
->querySubObject("Range");
range->querySubObject("InlineShapes")
->dynamicCall("AddPicture(const QString&)", picPath);
}
void QWordEngine::insertPic( QString picPath )
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");
QVariant tmp = selection->asVariant();
QListqList;
qList<querySubObject("InlineShapes");
Inlineshapes->dynamicCall("AddPicture(const QString&, QVariant, QVariant ,QVariant)",qList);
}
void QWordEngine::setColor( QColor color )
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
setColor(selection,color);
//if (selection)
//{
// selection->querySubObject("Range")->querySubObject("Font")->setProperty("Color", QVariant(color.value()));
// selection->querySubObject("Range")->setProperty("Text", "");
//}
}
void QWordEngine::setColor( QAxObject *obj,QColor color )
{
if (!obj) return;
obj->querySubObject("Range")
->querySubObject("ParagraphFormat")
->querySubObject("Shading")
->setProperty("ForegroundPatternColor",colorToInt(color));
}
void QWordEngine::setBgColor( QAxObject *obj,QColor color )
{
if (!obj) return;
obj->querySubObject("Range")
->querySubObject("ParagraphFormat")
->querySubObject("Shading")
->setProperty("BackgroundPatternColor",colorToInt(color));
}
//设置对齐方式
void QWordEngine::setAlignment(int index)
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
if (!selection) return;
selection->querySubObject("ParagraphFormat")->setProperty("Alignment",index);
}
void QWordEngine::setFontSize( int size )
{
QAxObject *selection = m_wordWidget->querySubObject("Selection");
if (!selection) return;
selection->querySubObject("Font")->setProperty("Size",size);
}
QString QWordEngine::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;
}
void QWordEngine::setPropraty( QAxObject *axobj,QString proname,QVariant provalue )
{
if (!axobj) return;
axobj->setProperty(proname.toStdString().c_str(),proname);
}
int QWordEngine::colorToInt( QColor color )
{
int sum = 0;
int r = color.red() << 16;
int g = color.green() << 8;
int b = color.blue() ;
int al = color.alpha() <<24;
sum = al + r + g + b;
return sum;
}
void QWordEngine::writeFile( QString savestr,QString filename )
{
QFile savefile(filename);
savefile.open(QFile::WriteOnly);
QTextStream saveteam(&savefile);
saveteam.setCodec("UTF-8");
saveteam << savestr;
savefile.close();
}
测试如下:
QWordEngine word("E:/Qt/docrwtest/ce2.docx");
bool ok = word.open();
//设置标题
word.addText("这是标题1",TITLE_ONE);
word.insertEnter();
word.addText("这是标题2",TITLE_TWO);
word.insertEnter();
//设置正文
word.addText("测试中!这是正文",NORMAL);
word.insertEnter();
//设置字体
word.addText(" font 测试默认是黑色",QFont("Times", 16, QFont::Bold));
word.insertEnter();
word.addText("font 测试",QFont("Times", 16, QFont::Bold),QColor("red"));
word.insertEnter();
word.setColor(QColor("black"));
//创建表格
QStringList headlist;
headlist << "head1hhhhhhhhhhhhhhhhhh" << "head2" << "head3" << "head4" << "head5";
QAxObject *table = word.createTable(5,5,headlist);
//QAxObject *table = word.insertTable(50,55,4,4);
//word.setCellString(table,2,1,"cessss");
//word.setCellString(table,2,2,"cessss");
//word.MergeCells(table,2,1,3,3);
//word.setColumnWidth(table,0,300);
//word.addTableRow(table,2,3);
//word.appendTableRow(table,3);
//QString str = table->generateDocumentation();
//word.insertPic("E:/Qt/docrwtest/2.png");
word.moveToEnd(table);
word.setAlignment(1);
//int sss = QColor("blue").value();
QAxObject *textobj = word.addText2("cessssssss");
word.setColor(textobj,QColor("blue"));
word.setBgColor(textobj,QColor("green"));
word.save();
word.close();