qt通过QAxObject操作word插入表格及数据,图片生成文档

/*直接操作word文档,插入表格,字段,图片等*/

QString HeadPath = HeadFilePath + "/" + insdata.RPolitName +"--"+ QDateTime::currentDateTime().toString("yyyy-MM-dd") +".docx";

//创建word文档

QAxObject* pWordWidget = new QAxObject();

bool flag = pWordWidget->setControl("Word.Application");//初始化COM对象, 新建一个word应用程序

if (!flag)

{

flag = pWordWidget->setControl("kwps.Application");//尝试用wps打开

if (!flag)

return;

}

pWordWidget->setProperty("Visible", false);//设置为可见, false则不会在界面打开显示

QAxObject* pDocuments = pWordWidget->querySubObject("Documents");//获取所有的工作文档(返回一个指向QAxObject包含的COM对象)

if (!pDocuments)

return;

QAxObject* pDocument = pDocuments->querySubObject("Add()");

//先创建一个文档

QAxObject* pActiveDocument = pWordWidget->querySubObject("ActiveDocument");//获取当前激活的文档

//插入文字

QAxObject* pSelection = pWordWidget->querySubObject("Selection"); //设置排版方式

//QAxObject* pSelection = pDocument->querySubObject("Range"); //Selection

if (!pSelection)

return;

pSelection->querySubObject("PageSetup")->setProperty("Orientation", "wdOrientPortrait");

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleTitle");//设置标题

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 20);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", ("222222222"));

pSelection->dynamicCall("InsertAfter(const QString&)", "\n");

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

//第一项 222222222

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1");//设置标题1

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 16);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", ("1.222222222"));

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

/*插入表格**************************************以下飞行基本信息表格********************************************************/

//222222222

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdAlignParagraphCenter");//居中

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 14);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", +(" 222222222: "));

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

//插入表格

QAxObject* pRangef = pSelection->querySubObject("Range");

QAxObject* pTablefs = pActiveDocument->querySubObject("Tables");

QAxObject* pTablef = pTablefs->querySubObject("Add(QVariant, int, int)", pRangef->asVariant(), 12, 2); //map.size 行,2列

pTablef->setProperty("Style", "网格型");

pTablef->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);//表格自动拉伸 0固定 1根据内容调整 2根据窗口调整

pTablef->setProperty("VerticalAlignment", 1);//上下居中

//设置格式

int rows = 0;

int nColumn = 0;

for (rows < 12; rows++;)

{

for (nColumn < 2; nColumn++;)

{

pTablef->querySubObject("Cell(Long, Long)", rows + 1, nColumn)->querySubObject("Range")

->querySubObject("ParagraphFormat")

->dynamicCall("Alignment", "wdAlignParagraphCenter");//居中

pTablef->querySubObject("Cell(Long, Long)", rows + 1, nColumn)->querySubObject("Range")

->querySubObject("Font")

->setProperty("Color", "wdColorBlack");//设置字体颜色

pTablef->querySubObject("Cell(Long, Long)", rows + 1, nColumn)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Name", ("宋体"));//设置字体

pTablef->querySubObject("Cell(Long, Long)", rows + 1, nColumn)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Size", 12);//设置字体大小

}

}

//填充内容

pTablef->querySubObject("Cell(Long, Long)", 1, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 2, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 3, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 4, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 5, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 6, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 7, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 8, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 9, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 10, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 11, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 12, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("222222222:"));//设置表格内容

//填充结构体

pTablef->querySubObject("Cell(Long, Long)", 1, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RPolitName));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 2, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RValueNumber));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 3, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IFlyModle));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 4, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.Imount));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 5, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.ILandingModle));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 6, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IOils));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 7, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RMessionNumber));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 8, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RDatetimes));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 9, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IDefect));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 10, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IWeather));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 11, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IWindSpeedValue));//设置表格内容

pTablef->querySubObject("Cell(Long, Long)", 12, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.IWindWrection));//设置表格内容

//退出表格

QVariantList params1;

params1.append(6);

params1.append(0);

pSelection->dynamicCall("EndOf(QVariant&, QVariant&)", params1).toInt();

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

//第二项 数据显示

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1");//设置标题1

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 16);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", ("2.数据显示"));

pSelection->dynamicCall("TypeParagraph(void)"); //插入回车

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyTextFirstIndent2");//设置首行缩进

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 14);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", ("用222222222线的差异。"));

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

/***********************************************以下为插入图片区域*************************************************************/

//插入图片标题

for (size_t i = 0; i < pngFileList.size(); i++)

{

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading2");

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 14);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", (FileList[i]));

pSelection->querySubObject("ParagraphFormat")->dynamicCall("SpaceBefore", 0);

pSelection->querySubObject("ParagraphFormat")->dynamicCall("SpaceAfter", 0);//段前段后设置为0

pSelection->querySubObject("ParagraphFormat")->dynamicCall("LineSpaceingRule", 1); //行距设置为单倍行距

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

QAxObject *inlineShapes = pSelection->querySubObject("InlineShapes");

//QString pPicPath = savePngFilePath + "/RAXPITCH.png";

QString pPicPath = pngFolderPath + "/" + pngFileList[i];

qDebug() << "插入的图片路径" << pPicPath;

QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", pPicPath);

shape->dynamicCall("Height", 290);

shape->dynamicCall("Width", 408);

shape->dynamicCall("Select");

shape->setProperty("range", pSelection->property("Range"));

shape->dynamicCall("Cut()");

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

}

//第三项 222222222

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1");//设置标题1

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 14);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", ("3.222222222"));

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

/*插入表格*******************************************以下评分表格************************************************************/

//表标题

pSelection->dynamicCall("setStyle(WdBuiltinStyle)", "wdAlignParagraphCenter");//居中

pSelection->querySubObject("Font")->setProperty("Name", ("宋体"));//设置字体

pSelection->querySubObject("Font")->setProperty("Size", 14);//设置字体大小

pSelection->dynamicCall("TypeText(const QString&)", +(" 222222222: "));

pSelection->dynamicCall("TypeParagraph(void)");//插入回车

//设置表头

QStringList headList;

headList << ("1111") << ("222222222");

//插入表格

QAxObject* pRange = pSelection->querySubObject("Range");

QAxObject* pTables = pActiveDocument->querySubObject("Tables");

QAxObject* pTable = pTables->querySubObject("Add(QVariant, int, int)", pRange->asVariant(), 7, headList.length()); //map.size 行,2列

pTable->setProperty("Style", "网格型");

pTable->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);//表格自动拉伸 0固定 1根据内容调整 2根据窗口调整

pTable->setProperty("VerticalAlignment", 1);//上下居中

//设置表头

for (int i = 0; i < headList.size(); i++)

{

pTable->querySubObject("Cell(Long, Long)", 1, i + 1)

->querySubObject("Range")

->dynamicCall("SetText(QString)", headList.at(i));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 1, i + 1)

->querySubObject("Range")

->dynamicCall("SetBold(int)", true);//加粗

pTable->querySubObject("Cell(Long, Long)", 1, i + 1)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Name", ("宋体"));//设置字体

pTable->querySubObject("Cell(Long, Long)", 1, i + 1)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Size", 14);//设置字体大小

pTable->querySubObject("Cell(Long, Long)", 1, i + 1)

->querySubObject("Range")

->querySubObject("ParagraphFormat")

->dynamicCall("Alignment", "wdAlignParagraphCenter");//设置表格左右居中

}

//设置格式

int row = 0;

int Column = 1;

for (row < 8; row++;)

{

for (Column < 3; Column++;)

{

pTable->querySubObject("Cell(Long, Long)", row + 2, Column)->querySubObject("Range")

->querySubObject("ParagraphFormat")

->dynamicCall("Alignment", "wdAlignParagraphCenter");//居中

pTable->querySubObject("Cell(Long, Long)", row + 2, Column)->querySubObject("Range")

->querySubObject("Font")

->setProperty("Color", "wdColorBlack");//设置字体颜色

pTable->querySubObject("Cell(Long, Long)", row + 2, Column)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Name", ("宋体"));//设置字体

pTable->querySubObject("Cell(Long, Long)", row + 2, Column)

->querySubObject("Range")

->querySubObject("Font")

->setProperty("Size", 12);//设置字体大小

}

}

//填充内容

pTable->querySubObject("Cell(Long, Long)", 2, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 3, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 4, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 5, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 6, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

pTable->querySubObject("Cell(Long, Long)", 7, 1)->querySubObject("Range")

->dynamicCall("SetText(QString)", ("111111111111:"));//设置表格内容

//填充结构体

pTable->querySubObject("Cell(Long, Long)", 2, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RgradeCurise));

pTable->querySubObject("Cell(Long, Long)", 3, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RgradeDecline));

pTable->querySubObject("Cell(Long, Long)", 4, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RgradeLand));

pTable->querySubObject("Cell(Long, Long)", 5, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RgradeSelf));

pTable->querySubObject("Cell(Long, Long)", 6, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RgradeTeacher));

pTable->querySubObject("Cell(Long, Long)", 7, 2)->querySubObject("Range")

->dynamicCall("SetText(QString)", (insdata.RGradeTotal));

//退出表格

QVariantList params;

params.append(6);

params.append(0);

pSelection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();

pActiveDocument->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(HeadPath));//保存至新建的文件夹

pActiveDocument->dynamicCall("Close()");

pWordWidget->dynamicCall("Quit()");

//时间可以修改为以输入的时间为命名

//QFile::copy("D: / Users / 111111111111 / Data_Ayalize_Modle0725.docx", "D: / Users /+filename");

QMessageBox::information(nullptr, "成功", "已成功生成评分报告!");

}

}

相关推荐
宽广17 分钟前
java aspose word 模板根据数据导出pdf
java·开发语言·pdf·c#·word
lljss202022 分钟前
C# 56. Tcp Server
c#
数据小爬虫@1 小时前
利用Python爬虫技术获取商品销量详情
开发语言·爬虫·python
字节高级特工1 小时前
C++---入门
开发语言·c++·算法
乌云大帝1 小时前
IIS服务器部署C# WebApi程序,客户端PUT,DELETE请求无法执行
运维·服务器·c#·webapi
北辰浮光2 小时前
[JavaScript]Ajax-异步交互技术
开发语言·javascript·ajax
亦是远方2 小时前
C++编译过程
java·开发语言·c++
程序猿000001号2 小时前
使用Python的Turtle库绘制图形:初学者指南
开发语言·python