QT 生成word 文档的简单说明
很简单,只需要向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(); }