Qt修改word文件一般通过word的标签修改对应数据,先在word中设置书签名称,在通过QAxObject打开文件并修改对应值:
QAxWidget *word = new QAxWidget("Word.Application",0,Qt::MSWindowsOwnDC);
word->setProperty("Visible",false); //调用本地wps或者office 不显示
QAxObject *documents = word->querySubObject("Documents");
documents->dynamicCall("Open(QString)",QString::fromLocal8Bit(QString(filepaths).toLocal8Bit()));
//获取当前激活的文档
QAxObject *document = word->querySubObject("ActiveDocument");
//types 标签名称
QAxObject *bookmark_code = document->querySubObject("Bookmarks(QVariant)","types");
//选中标签,将字符textg插入到标签位置
if(!bookmark_code->isNull())
{
bookmark_code->dynamicCall("Select(void)");
//QString("%1(%2)").arg(_ajdh.typeName).arg(_ajdh.anjuanCode) 具体内容值
bookmark_code->querySubObject("Range")->setProperty("Text",QString("%1(%2)").arg(_ajdh.typeName).arg(_ajdh.anjuanCode));
}
delete bookmark_code;
//获取所有表列表
QAxObject* pTables = document->querySubObject("Tables");
//指定表 根据序号
QAxObject* table = pTables->querySubObject("Item(int)", 2);
if(table)
{
QAxObject* rows = table->querySubObject("Rows");
//获取指定行
QString sPos = QString("Item(%1)").arg(2);
QAxObject* row = rows->querySubObject(sPos.toStdString().c_str());
//qDebug()<<"插入行";
//QVariant param = row->asVariant();
//rows->dynamicCall("Add(Variant)", param);
if(row)
{
//修改值
table->querySubObject("Cell(int, int)", 2 + i, 1)
->querySubObject("Range")
->dynamicCall("SetText(QString)", QString("%1").arg(i+1,3,10,QLatin1Char('0')));
}
}
// 获取文档中名字为pic的标签
QAxObject*bookmark_pic=document->querySubObject("Bookmarks(QVariant)","pngS");
// 选中标签,将图片插入到标签位置
if(!bookmark_pic->isNull())
{
bookmark_pic->dynamicCall("Select(void)");
QAxObject *range;
range = bookmark_pic->querySubObject("Range");
QVariant tmp = range->asVariant();
QList<QVariant>qList;
//txmpath 图片路径
qList<<QVariant(QString::fromLocal8Bit(QString(txmpath).toLocal8Bit()));
qList<<QVariant(false);
qList<<QVariant(true);
qList<<tmp;
QAxObject *Inlineshapes = document->querySubObject("InlineShapes");
Inlineshapes->dynamicCall("AddPicture(const QString&, QVariant, QVariant ,QVariant)",qList);
delete Inlineshapes;
delete range;
}
delete bookmark_pic;
注意:打印时,必须事先能通过wps和office打印文件;
// 文件另存爲
// document->dynamicCall("SaveAs (const QString&)",QString("C:/Users/Administrator.WIN-9LOVCBSO17G/Desktop/printModel_create.docx"));
qDebug()<<"---";
//打印文件
document->dynamicCall("PrintOut");
//关闭 并清除内存
delete document;
documents->dynamicCall("Close()",true);
delete documents;
word->dynamicCall("Quit()");
delete word;
修改execl直接通过行,列修改对应值,不用插入标签
QAxObject *pApplication = pApplication = new QAxObject(); //Excel服务
pApplication->setControl("Excel.Application");
//设置窗口为可见
pApplication->setProperty("Visible",false);
//不显示所有警告
pApplication->setProperty("DisplayAlerts",false);
//直接显示打印预览不需要显sheet内容
pApplication->setProperty("ScreenUpdating",false);
if(pApplication)
{
//查询工作簿列表子对象
QAxObject *pWorkbooks =pApplication->querySubObject("WorkBooks"); //工作簿列表
if (pWorkbooks)
{
//当前软件中工作簿
pWorkbooks->dynamicCall("Open(QString)",QDir::toNativeSeparators(filepaths),QString(),false);
QAxObject *pWorkbook = pApplication->querySubObject("ActiveWorkBook"); //当前工作簿
if (pWorkbook)
{
//获取所有工作表列表
QAxObject *pWorkSheets = pWorkbook->querySubObject("Sheets"); //工作表列表
}
}
}
选择一个空白sheet页,重新添加
sheet索引从 1 开始
QAxObject* sheet = pWorkSheets->querySubObject("Item(int)", 2);//选择第二张表
if(sheet)
{
qDebug("[copy newSheet to workSheets]");
QVariant param = sheet->asVariant();//将第二张表转化为参数
QVariant cels= sheet->dynamicCall("Copy(const QVariant&)", param);//复制表至同一workSheets中
}
QAxObject* sheetDoc = pWorkSheets->querySubObject("Item(int)", 1);//选择第一张表
if(sheetDoc)
{
QAxObject * shapes = sheetDoc->querySubObject("Shapes");
if(shapes)
{
//图片路径
QFileInfo info(txmpath);
qDebug(("[txmpath] "+QDir::toNativeSeparators(info.absoluteFilePath())).toLocal8Bit());
//添加图片到指定位置 (30.61368,617.65934,161.00,56.692)左边缘,上边缘,宽度,高度,以磅为单位
//1厘米等于28.35磅
QVariant var= shapes->dynamicCall("AddPicture( QString&, bool, bool, double, double, double, double",QDir::toNativeSeparators(info.absoluteFilePath()),true,true,30.61368,617.65934,161.00,56.692);
//qDebug(var.toString().toUtf8());
delete shapes;
}
}
//案卷档号
QAxObject *mark_ajdh = sheetDoc->querySubObject(QString("Cells(%1,%2)").arg(4).arg(3).toLatin1());
if(mark_ajdh)
{
//Value 与Value2 是因为wps与office文件不一致
if(!mark_ajdh->setProperty("Value",QVariant(" "+ajdh)))
mark_ajdh->setProperty("Value2",QVariant(" "+ajdh));
delete mark_ajdh;
}
QAxObject* sheetTable = pWorkSheets->querySubObject("Item(int)", row);//选择第二张表
QAxObject* PageSetup=sheetTable->querySubObject("PageSetup");
if(PageSetup)
{
PageSetup->setProperty("CenterFooter",QVariant(QString("第 %1 页,共 %2 页").arg(row-1).arg(clo)));
delete PageSetup;
}
操作结束必须关闭和清除资源,防止操作过程中打开多个造成内存溢出
//qDebug("doit-over-");
//pWorkSheets->dynamicCall("PrintOut"); //开始打印
delete pWorkSheets;
pWorkbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(filepaths));
// pWorkbook->dynamicCall("PrintOut");
pWorkbook->dynamicCall("Close(Boolean)", false);
}
delete pWorkbook;
}
delete pWorkbooks;
pApplication->dynamicCall("Quit()");
}
delete pApplication;
//qDebug("end parint--");
使用QAxWidget和QAxObject修改word与execl,
实际上也是office和wps中的宏操作,需要用到的修改完全可以通过录制宏获取相关属性,按照对应属性修改相关值;
上文中修改页码数就是通过这种方式获取;
以wps为例: