您的位置:首页 > 移动开发 > Objective-C

qt QAxObject word文档添加页码(页眉和页脚)

2017-07-05 15:11 591 查看
今天偶然一个朋友问起QAxObject 操作word文档添加页码的问题,因为利用QAxObject 做过ppt转pdf的经过,所以花了时间做了一下这个。算是第一次写博客,写的不明白,请大家轻喷,谢谢。

QString
QAxBase::generateDocumentation()
Returns a rich text string with documentation for the wrapped COM object. Dump the string to an HTML-file, or use it in e.g. aQTextBrowser
widget.
generateDocumentation可以打印出来不同的接口的相应的属性,槽函数,以及信号函数。在generateDocumentation打印出来的html文件,不想assistant有很详细的说明,所以我们可以同时借助VBAWD10.CHM。
下面就是pagenumber的一些相关信息,虽然不是C++语言,但是语言都是相同的大概的意思应该可以看明白。根据下列的提示信息可以有相应的步骤去设置pagenumbers.



QString filepath = QFileDialog::getOpenFileName(this, tr("Save orbit"), ".", tr("Microsoft Office 2007 (*.docx)"));//获取保存路径  

if (!filepath.isEmpty())
{
QVariantList params;
QAxWidget *excel = new QAxWidget(this);
excel->setControl("Word.Application");//连接Excel控件  
QAxObject *document = excel->querySubObject("Documents");
connect(excel, SIGNAL(exception(int, QString, QString, QString)), this, SLOT(someSlot(int, QString, QString, QString)));
QString text = document->generateDocumentation();
QFile file("test.html");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput(&file);
txtOutput << text << endl;

file.close();

excel->dynamicCall("SetVisible (bool Visible)", "true");//true显示office窗体,false不显示
excel->setProperty("DisplayAlerts", true);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示  
QAxObject *presentation = document->querySubObject("Open(const QString&)", filepath);
QString text2 = presentation->generateDocumentation();
QFile file2("test2.html");
if (!file2.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput2(&file2);
txtOutput2 << text2 << endl;
file2.close();

QString name = "Sections";
QAxObject *presentation1 = presentation->querySubObject(name.toStdString().c_str());
QString text3 = presentation1->generateDocumentation();
QFile file3(QString("test_%1.html").arg(name));
if (!file3.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput3(&file3);
txtOutput3 << text3 << endl;
file3.close();

QAxObject *presentation2 = presentation1->querySubObject("First");
QString text4 = presentation2->generateDocumentation();
QFile file4("test4.html");
if (!file4.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput4(&file4);
txtOutput4 << text4 << endl;
file4.close();

QAxObject *presentation3 = presentation2->querySubObject("Headers");//添加页眉
//QAxObject *presentation3 = presentation2->querySubObject("Footers");//添加页脚
QString text5 = presentation3->generateDocumentation();
QFile file5("test5.html");
if (!file5.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput5(&file5);
txtOutput5 << text5 << endl;
file5.close();

params.clear();
params << 1;
QAxObject *presentation4 = presentation3->querySubObject("Item(WdHeaderFooterIndex)", params);
QString text6 = presentation4->generateDocumentation();
QFile file6("test6.html");
if (!file6.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput6(&file6);
txtOutput6 << text6 << endl;
file6.close();

QAxObject *presentation5 = presentation4->querySubObject("PageNumbers");
QString text7 = presentation5->generateDocumentation();
QFile file7("test7.html");
if (!file7.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput7(&file7);
txtOutput7 << text7 << endl;
file7.close();

params.clear();
params << 2;
params << 1;
QAxObject *presentation6 = presentation5->querySubObject("Add(QVariant&, QVariant&)", params);
QString text8= presentation6->generateDocumentation();
QFile file8("test8.html");
if (!file8.open(QIODevice::WriteOnly | QIODevice::Text))
{
//cout << "Open failed." << endl;
//return -1;
}
QTextStream txtOutput8(&file8);
txtOutput8 << text8 << endl;

file8.close();

presentation->dynamicCall("SaveAs(const QString&)", filepath);
delete presentation;
presentation = NULL;
excel->dynamicCall("Quit()");
delete excel;

excel = NULL;

哈哈,这只是QAxObject  操作office的冰山一角的冰山一角,但是如果要做这方面的话,就用generateDocumentation和VBAWD10.CHM吧,绝对的好帮手。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: