QT 生成word 文档的简单说明

哎哎7年前C++4267

很简单,只需要向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();
}


标签: qtword

相关文章

QString与QStringList的转换,提取字符串,合并字符串

QString提取字符,变为多个字符串的常用方法,split还有其参数,具体查看assistantQString str = "a,,b,c";QSt...

好久没更新了,这次介绍 qDebug()的所有输出转移至文件中

#include <QtDebug> #include <QFile> #include <QTextStream> #defi...

使用QT设计师,快速将 ACTIONS 放入toolbar中

使用QT设计师,快速将 ACTIONS 放入toolbar中

  网上很多例子,都是手动敲代码在Qt界面工具栏中插入Action,实际QTDesigner本身带有其功能,而且非常方便。  插入完界面后,我们还可以去看它生成的代码,了解一下如何手动插入工具栏按钮。...

【QT】代码或者程序出现乱码的问题

  1.str = QString("%1 %2 (%3s-%4s)").arg("permissive").arg("society")....

[QT]qt 程序退出代码。

  1.关闭主窗口并退出程序是    QApplication::exit()     2.如果是QDialog,就accept() 或 reje...

Gist一直可用

136 人赞同了该回答Github作为代码分享平台在开发者中非常流行。此平台托管了包括游戏、书籍以至于字体在内的一千两百多万个项目(现在更多),这使其成为互联网上最大的代码库。Github还提供另一个...