QT 生成word 文档的简单说明

哎哎7年前C++4923

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

相关文章

[WINDDK]系统驱动开发,出现 __IN_ 未定义的类型解决方案。 vs2008 winddk

1>stdafx.cpp1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\excpt.h(60) : error C206...

[QT] qtableview 中的 currentchange 信号与槽

[QT] qtableview 中的 currentchange 信号与槽

为了实现当鼠标选中某行时,进行一次数据分析,并在侧栏进行显示。该函数功能已经实现,但是使用键盘控制光标移动时,侧栏不能进行数据分析。查找QT手册发现currentChange 完美的符合了我的需求。所...

QT 的冒泡排序

qt中冒泡排序的做法,突然想到的内容。闲来无事便上传。  QList<QString> OExpression::getSortedList(QList<QString&g...

[CPP]string类型应用

  之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作 为一个类出现,他集成的操作函数足以完成我们大多数情况下(...

C语言位运算,判断一个字节的某位是否为1

直接上代码 char data = 0x01; //需要比较的数据 char temp = 0x01;  ...

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

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