QT載入office檔案簡單方法 阿新 • • 發佈:2019-01-01 <?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>UTOfficeWidget</class> <widget class="QWidget" name="UTOfficeWidget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>UTOfficeWidget</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTextEdit" name="textEdit"/> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/></ui>二:標頭檔案 utofficewidget.h#ifndef UTOFFICEWIDGET_H#define UTOFFICEWIDGET_H#include <QWidget>#include<QAxObject>#include<QAxWidget>#include <QTextStream>#include<QFile>#include "ui_utofficewidget.h" class UTOfficeWidget : public QWidget{Q_OBJECTpublic:UTOfficeWidget(QWidget *parent = 0);~UTOfficeWidget();public://開啟檔案 ;*.doc;*.docx;*.docm;*.xls;*.xlsx;*.xlsm;*.xlsb,*.ppt;*.pptx;*.pptm;*.txt;*.xml;void Open(QString filePath);//關閉 是否需要儲存 預設不儲存void Close(bool isSave=false);//儲存void Save();//另存為。。void SaveAs(QString newFilePath);private://文件開啟控制元件QAxWidget* officeContent;//當前開啟檔案的路徑QString currentFilePath;Ui::UTOfficeWidget ui;};#endif // UTOFFICEWIDGET_H三:CPP檔案:#include "utofficewidget.h"UTOfficeWidget::UTOfficeWidget(QWidget *parent): QWidget(parent),officeContent(NULL){ui.setupUi(this);}UTOfficeWidget::~UTOfficeWidget(){Close();}void UTOfficeWidget::Open( QString filePath ){if (filePath.isEmpty()){return;}if (officeContent){Close();}if (filePath.endsWith(".docx") ||filePath.endsWith(".doc") || filePath.endsWith(".doc")){officeContent=new QAxWidget ("Word.Document", ui.textEdit);officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));officeContent->setControl (filePath);officeContent-> show ();}else if (filePath.endsWith(".xlsx") || filePath.endsWith(".xls") ||filePath.endsWith(".xlsm")){officeContent=new QAxWidget ("Excel.WorkBook", ui.textEdit);officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));officeContent->setControl (filePath);officeContent-> show ();}else if (filePath.endsWith(".ppt") || filePath.endsWith(".pptx") ||filePath.endsWith(".pptm")){//"Powerpoint.Presentation"officeContent=new QAxWidget ("Powerpoint.Application",ui.textEdit);//pptPresentation->setControl("{048EB43E-2059-422F-95E0-557DA96038AF}"); //pptPresentation->setControl("{91493441-5A91-11CF-8700-00AA0060263B}");officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));QAxObject *presentations = officeContent->querySubObject( "Presentations" ); presentations->dynamicCall("Open(QString)",filePath);}else if (filePath.endsWith(".txt") || filePath.endsWith(".xml")){QFile sfile(filePath);if (sfile.open(QFile::ReadOnly | QFile::Text)){QTextStream in(&sfile);ui.textEdit->setText(in.readAll());sfile.close();}}else{return;}currentFilePath=filePath;}void UTOfficeWidget::Close( bool isSave/*=false*/ ){if (officeContent){if (currentFilePath.endsWith(".ppt") || currentFilePath.endsWith(".pptx") ||currentFilePath.endsWith(".pptm")){officeContent->dynamicCall("Quit(void)");}else if (currentFilePath.endsWith(".txt") || currentFilePath.endsWith(".xml")){ui.textEdit->clear();}else{officeContent->dynamicCall("Close(bool)",true);}}}void UTOfficeWidget::Save(){}void UTOfficeWidget::SaveAs( QString newFilePath ){}四:呼叫測試: