5

QT 生成word 文档的简单说明

 3 years ago
source link: https://ii74.com/post/12.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

QT 生成word 文档的简单说明

哎哎5年前 (2016-07-21)C++927

很简单,只需要向QTextStream中输入即可。

直接上源码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qfiledialog.h"
#include <QDateTime>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QString filename = QFileDialog::getSaveFileName(0,"","","*.doc",0,0);
    QString html;
    html += "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type  content=\"text/html; charset=gb2312\" >"; //这句可加可不加。主要是因为我在word里把doc另存为html文件后,看到有这么个头标签,由此想到直接将html文档保存为doc文件。
    html = ui->textEdit->toHtml();
    QFile outFile(filename);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append );
    QTextStream ts(&outFile);
    ts<<html<<endl;
    outFile.close();
}
阅读剩余的29%

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK