QAxWidget打开word、pdf、excel、ppt
QAxWidget 打开word、pdf、excel、ppt
使⽤QAxWidget打开word,pdf,excel,其中word⾼亮关键字
打开txt并⾼亮关键字见另⼀⽂。
PPT操作⽂字颜⾊见另⼀⽂。
⽬前存在的问题:
进程内有问题调起来⼜崩溃消失了,会报错CoCreateInstance failure (没有注册类),暂时未解决。
2.ppt只能直接打开,⽆法内嵌进窗⼝。
3.word开启后进程可以正常关闭,excel和ppt暂⽆法关闭。
//打开指定⽂件,⾼亮关键字,只有word和txt实现⾼亮关键字
void openFile(QString strFileName, QStringList& strKeys)
{
closeFiles();
dsWith(".docx")|| dsWith(".doc"))
{
openWord(strFileName, strKeys);
}
else dsWith(".xlsx")|| dsWith(".xls"))
{
openExcel(strFileName, strKeys);
}
else dsWith(".pptx")|| dsWith(".ppt"))
{
openPpt(strFileName, strKeys);
}
else dsWith(".pdf"))
{
openPdf(strFileName, strKeys);
}
else dsWith(".txt"))
{
openTxt(strFileName, strKeys);
}
}
//删除进程,保证反复切换和打开各种⽂档时只有⼀个进程
void closeFiles()
{
if(this->m_pfileView !=nullptr)
{
//这样写可以真正关掉winword进程,但是如果开着其他的进程,都会被杀掉,⽽不是进杀掉qt打开的这个
/*QProcess process;
QString strCmd = "taskkill / /f";
strCmd = "taskkill / /f";
strCmd = "taskkill / /f";
strCmd = "taskkill / /f";
process.close();
m_pfileView->close();
m_pfileView->clear();
delete m_pfileView;
m_pfileView = nullptr;*/
if(dsWith(".docx")|| dsWith(".doc"))
{
QAxObject *app = m_pfileView->querySubObject("Application");
if(app !=nullptr)
{
m_pfileView->dynamicCall("Close(boolean)",false);
m_pfileView->dynamicCall("Close(boolean)",false);
m_pfileView->close();
m_pfileView->clear();
app->dynamicCall("Quit ()");
delete app;
app =NULL;
}
}
else if(dsWith(".xlsx")|| dsWith(".xls"))
{
//可能⽆法关闭
m_pfileView->dynamicCall("Quit()");
}
else if(dsWith(".pptx")|| dsWith(".ppt"))
{
//可能⽆法关闭
m_pfileView->dynamicCall("Quit()");
}
QFile fileTemp(m_strSaveFileName);
if(m_pPlainTextEdit !=nullptr)
{
delete m_pPlainTextEdit;
m_pPlainTextEdit =nullptr;
}
}
操作word,实现⾼亮关键字,实际上是把关键字出来标记后保存为临时⽂件。
// ⾼亮操作函数
/
/ dirName:是待检查⽂件的路径;keyWords:是要查的关键字(查的关键字可能是多个)
bool highLightKeyWordsinWordFile(QString dirName, QStringList keyWords)
{
int lastSeparatorIndex = dirName.lastIndexOf(QDir::separator());
m_strSaveFileName = QApplication::applicationDirPath()+"/"+ dirName.mid(lastSeparatorIndex +1);// 另存为的路径
QAxWidget wordApplication("Word.Application");
QAxObject *documents = wordApplication.querySubObject("Documents");
documents->dynamicCall("Open(const QString&)", dirName);
wordApplication.setProperty("Visible",QVariant(false));
QAxObject* m_doc = wordApplication.querySubObject("ActiveDocument");// 获取当前⼯作簿
QAxObject* pRange = m_doc->querySubObject("Content()");
if(NULL!= pRange)
{
// 查关键字
QAxObject *pFind = pRange->querySubObject("Find()");
QStringList keyWordsPosition;
if(NULL!= pFind)
{
pFind->dynamicCall("ClearFormatting()");
pFind->setProperty("Format",true);
pFind->setProperty("MatchCase",false);
pFind->setProperty("MatchWholeWord",false);
pFind->setProperty("MatchByte",true);
pFind->setProperty("MatchWildcards",false);
pFind->setProperty("MatchSoundsLike",false);
pFind->setProperty("MatchAllWordForms",false);
for(int i =0; i < keyWords.size();++i){
// 到关键字所在的位置,得到⼀个position,将position添加到keyWordsPosition中。
QString keyWord = keyWords.at(i);
QStringList position =findKeyWordsPosition(pRange->property("Text").toString(), keyWord);
if(!ains("")){
if(!ains("")){
keyWordsPosition << position;
pFind->setProperty("Text", keyWord);
pFind->dynamicCall("Execute()");
while(pFind->property("Found").toBool())
{
bool isHighlight = pFind->parent()->setProperty("HighlightColorIndex","wdYellow");                            pFind->dynamicCall("Execute()");
if(!isHighlight)
{
delete pFind;
pFind =NULL;
delete pRange;
pRange =NULL;
m_doc->dynamicCall("Close(boolean)",true);
wordApplication.dynamicCall("Quit ()");
delete m_doc;
m_doc =NULL;
return false;
}
}
}
}
}
if(keyWordsPosition.size()>=1){
QString fileName = dirName.mid(lastSeparatorIndex +1);
QString filePath = dirName.mid(0, lastSeparatorIndex +1);
m_doc->dynamicCall("SaveAs(const QString)", m_strSaveFileName);
}
delete pFind;
pFind =NULL;
delete pRange;
pRange =NULL;
m_doc->dynamicCall("Close(Boolean)",true);
m_doc->dynamicCall("Quit()");
delete m_doc;
m_doc =NULL;
return true;
}
return true;
}
QStringList findKeyWordsPosition(QString fileContent, QString keyWord)
{
QStringList resList;
ains(keyWord)){
qDebug()<< QObject::tr("包含⼦字符串: %1").arg(keyWord);
int startIndex =0;
/
/        int count = 0;
int keyWordsLen = keyWord.length();
QRegExp rx(QObject::tr("[,。:\r]?([^,。:\r]*(%1)[^,。:\r]*)[,。:\r]?").arg(keyWord));
while((startIndex = rx.indexIn(fileContent, startIndex))!=-1){
QString resStr = rx.cap(1).mid(0);// 提取⼦字符串所在的语句
ains("\r"))
resList << resStr;
// 到⼦字符串
int findIndex = fileContent.indexOf(keyWord, startIndex);
//            qDebug() << QObject::tr("keyWords 出现的位置 : %1").arg(findIndex);
startIndex = findIndex + keyWordsLen;
startIndex = findIndex + keyWordsLen;
//            qDebug() << QObject::tr("keyWords 出现的次数 : %1").arg(++count);无法打开文件
qDebug()<<"\n";
}
return resList;
}
return resList <<"";
}
void openWord(const QString & str, QStringList& strKeyList)
{
highLightKeyWordsinWordFile(m_strFileName, strKeyList);
m_pfileView =new QAxWidget("Word.Application", ui.m_pFileWidget);
if(m_pfileView ==nullptr)
{
return;
}
m_pfileView->resize(width(), ui.m_pFileWidget->height());
m_pfileView->dynamicCall("SetVisible (bool Visible)","false");//不显⽰窗体
m_pfileView->setFocusPolicy(Qt::StrongFocus);
QAxObject *documents = m_pfileView->querySubObject("Documents");
m_pfileView->setControl(str);
documents->setProperty("DisplayAlerts",false);
m_pfileView->setProperty("DisplayAlerts",false);
m_pfileView->setProperty("DisplayHorizontalScrollBar",true);// 显⽰滚动条
//本来是想把⽂本的显⽰百分⽐调整下,但是并不⽣效。
/*QAxObject* m_doc = m_pfileView->querySubObject("ActiveWindow");    // 获取当前⼯作簿        QAxObject* pActivePane = m_doc->querySubObject("ActivePane");
QAxObject* pView = pActivePane->querySubObject("View");
QAxObject* pZoom = pView->querySubObject("Zoom");
pZoom->setProperty("Percentage", 150);*/
//等待⼀会,不然显⽰出来会特别⼩
wait(500);
m_pfileView->show();
m_pfileView->raise();
}
打开excel
void openExcel(const QString & str, QStringList& strKeyList)
{
m_pfileView =new QAxWidget("Excel.Application", ui.m_pFileWidget);
if(m_pfileView ==nullptr)
{
return;
}
QFileInfo info(str);
if(!ists())
{
qDebug()<<"ists not "<<"\n";
}
m_pfileView->setControl(QDir::toNativeSeparators(info.absoluteFilePath()));
m_pfileView->dynamicCall("SetVisible (bool Visible)",false);
m_pfileView->resize(width(), ui.m_pFileWidget->height());
wait(500);
m_pfileView->show();
m_pfileView->raise();
}
打开pdf
void openPdf(const QString & str, QStringList& strKeyList)
{
m_pfileView =new QAxWidget(ui.m_pFileWidget);
if(m_pfileView ==nullptr)
{
return;
}
if(!m_pfileView->setControl("Adobe PDF Reader"))
{
return;
}
m_pfileView->dynamicCall("LoadFile(const QString&)", str);
m_pfileView->dynamicCall("SetVisible (bool Visible)","false");
m_pfileView->setProperty("DisplayAlerts",false);
m_pfileView->setProperty("DisplayScrollBars",true);// 显⽰滚动条
m_pfileView->resize(width(), ui.m_pFileWidget->height());
m_pfileView->show();
m_pfileView->raise();
}
打开ppt
void openPpt(const QString & str, QStringList& strKeyList)
{
m_pfileView =new QAxWidget("Powerpoint.Application",ui.m_pFileWidget);
if(m_pfileView == nullptr)
{
return;
}
QFileInfo info(str);
if(!ists())
{
qDebug()<<"ists not "<<"\n";
}
QAxObject *presentations = m_pfileView->querySubObject("Presentations");        presentations->dynamicCall("Open(QString)", str);
m_pfileView->dynamicCall("SetVisible (bool Visible)","false");
m_pfileView->resize(width(), ui.m_pFileWidget->height());
wait(500);
m_pfileView->show();
m_pfileView->raise();
}