1.程序界面創(chuàng)建Qt桌面應(yīng)用程序項(xiàng)目,項(xiàng)目名稱為“WordWriteTable”。設(shè)計(jì)程序界面,向Word輸出表格及顯示程序界面如下圖所示。 2.全局變量及方法“mainwindow.h”頭文件的代碼如下: #include <QMainWindow>
#include <QMessageBox>
#include <QAxObject>
#include <QAxWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
typedef struct record
{
QString year; //年份
QString total; //高考人數(shù)
QString admit; //錄取人數(shù)
QString rate; //錄取率
} Record;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btnWrite_clicked(); // “寫入”按鈕單擊事件槽
void view_Word(QString& filename); //在Qt界面預(yù)覽Word表格
private:
Ui::MainWindow *ui;
QAxObject *myword; //Word應(yīng)用程序指針
QAxObject *mydocs; //文檔集指針
QAxObject *document; //文檔指針
QAxObject *mytable; //Word中表格指針
QList<Record> myrecord; //表格記錄列表
QAxWidget *mywidget; //Qt界面上的Word可視化部件
};
3. 數(shù)據(jù)準(zhǔn)備在構(gòu)造函數(shù)中讀取已有文件的數(shù)據(jù),為輸出做準(zhǔn)備: MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
myword = new QAxObject("Word.Application"); //創(chuàng)建 Word 應(yīng)用程序?qū)ο?font>
mydocs = myword->querySubObject("Documents"); //獲取文檔集
mydocs->dynamicCall("Open(const QString&)", "D:\\Temp\\1977-2019歷年全國高考人數(shù)和錄取率統(tǒng)計(jì).docx"); //打開源文檔
document = myword->querySubObject("ActiveDocument");
mytable = document->querySubObject("Tables(int)", 1);
int rows = mytable->querySubObject("Rows")->dynamicCall("Count").toInt();
//獲取表格總行數(shù)
for(int i = rows-4; i < rows + 1; i++)
{
Record oneRec; //表格記錄結(jié)構(gòu)
QAxObject *infocol = mytable->querySubObject("Cell(int,int)", i, 1);
QString year = infocol->querySubObject("Range")->property("Text").toString();
oneRec.year = year; //獲取年份
infocol = mytable->querySubObject("Cell(int,int)", i, 2);
QString total = infocol->querySubObject("Range")->property("Text").toString();
oneRec.total = total; //獲取高考人數(shù)
infocol = mytable->querySubObject("Cell(int,int)", i, 3);
QString admit = infocol->querySubObject("Range")->property("Text").toString();
oneRec.admit = admit; //獲取錄取人數(shù)
infocol = mytable->querySubObject("Cell(int,int)", i, 4);
QString rate = infocol->querySubObject("Range")->property("Text").toString();
oneRec.rate = rate; //獲取錄取率
myrecord.append(oneRec); //添加進(jìn)記錄列表
}
delete mytable;
mytable = nullptr;
document->dynamicCall("Close()");
myword->dynamicCall("Quit()");
}
4. 填寫表格將數(shù)據(jù)寫入到Word表格中,并在界面上預(yù)覽: void MainWindow::on_btnWrite_clicked()
{
myword = new QAxObject("Word.Application"); //創(chuàng)建 Word 應(yīng)用程序?qū)ο?font>
mydocs = myword->querySubObject("Documents"); //獲取文檔集
mydocs->dynamicCall("Add(void)"); //新建一個(gè)文檔
document = myword->querySubObject("ActiveDocument");
QAxObject *tables = document->querySubObject("Tables"); //表格集指針
QAxObject *paragraph = myword->querySubObject("Selection"); //文本段指針
paragraph->dynamicCall("TypeText(const QString&)","2015-2019年高考人數(shù)和錄取率"); //先輸出表格標(biāo)題
QAxObject *range = paragraph->querySubObject("Range");
QVariantList paras;
paras.append(range->asVariant());
paras.append(6); //創(chuàng)建表格為 6 行
paras.append(4); //創(chuàng)建表格為 4 列
tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)",paras);
mytable = paragraph->querySubObject("Tables(int)", 1);
mytable->setProperty("Style","網(wǎng)格型"); //設(shè)置表格為帶網(wǎng)格邊框
QAxObject *Borders = mytable->querySubObject("Borders");
Borders->setProperty("InsideLineStyle", 1);
Borders->setProperty("OutsideLineStyle", 1);
QAxObject *cell; //單元格對象指針
/**循環(huán)控制輸出表格內(nèi)容*/
for(int i = 0; i < 6; i++){
if (i == 0){
for(int j=0; j<4; j++)
{
cell= mytable->querySubObject("Cell(int,int)",(i+1),(j+1))->querySubObject("Range");
switch (j){
case 0: cell->setProperty("Text","年份"); break;
case 1: cell->setProperty("Text","高考人數(shù)(萬)"); break;
case 2: cell->setProperty("Text","錄取人數(shù)(萬)"); break;
case 3: cell->setProperty("Text","錄取率"); break;
default: break;
}
}
}else{
for(int j = 0; j < 4; j++){
cell = mytable->querySubObject("Cell(int,int)",(i+1),(j+1))->querySubObject("Range");
switch (j) {
case 0: cell->setProperty("Text", myrecord[i-1].year); break;
case 1: cell->setProperty("Text", myrecord[i-1].total); break;
case 2: cell->setProperty("Text", myrecord[i-1].admit); break;
case 3: cell->setProperty("Text", myrecord[i-1].rate); break;
default: break;
}
}
}
}
document->dynamicCall("SaveAs(const QString&)", "D:\\Temp\\2015-2019年全國高考錄取人數(shù)統(tǒng)計(jì).doc"); //保存表格
QMessageBox::information(this, tr("完畢"),tr("表格已輸出至 Word 文檔。"));
delete mytable;
mytable = nullptr;
delete paragraph;
paragraph = nullptr;
document->dynamicCall("Close()");
myword->dynamicCall("Quit()");
QString fname = "D:\\Temp\\2015-2019年全國高考錄取人數(shù)統(tǒng)計(jì).doc";
view_Word(fname); //在 Qt 界面上預(yù)覽
}
void MainWindow::view_Word(QString& filename)
{
mywidget = new QAxWidget("Word.Application", ui->labView);
mywidget->dynamicCall("SetVisible(bool Visible)", "false");
mywidget->setProperty("DisplayAlerts", false);
mywidget->setGeometry(ui->labView->geometry().x(), ui->labView->geometry().y(),
ui->labView->width(), ui->labView->height());
mywidget->setControl(filename);
mywidget->show();
}
5.運(yùn)行效果運(yùn)行程序,單擊“輸入”按鈕,彈出消息框提示自己輸出至Word文檔的表格,單擊“確定”按鈕,界面上會顯示出Word文檔中的表格,如下圖所示。 程序運(yùn)行后在“D:\Temp\”路徑下生成Word文檔“2015-2019年全國高考錄取人數(shù)統(tǒng)計(jì).doc”,打開后可以看到Qt在其中寫入的表格,如下圖所示。 以上系列文章系統(tǒng)地介紹了Qt對Excel和Word的操作,在工作中高效地可以靈活運(yùn)用這些方法來操作Office,實(shí)地完成文檔的制作。 —————————————————
|