Qt HTML报表

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDateTime>
#include<QDebug>
/*
<table>:这个标签用于定义一个HTML表格。
<tr>:代表“table row”,即表格的一行。所有单元格数据(无论是表头还是数据单元格)都必须放在<tr>标签内。
<th>:代表“table header”,即表头单元格。通常用于表示列的标题,文本通常呈现为粗体和居中。
<td>(table data,即表格数据单元格)不同,<th>通常用于标识列的标题。

<p>这是第一行。</p>  
<p>这是新的一行。</p>



 * */

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    QString  qstrFile=qApp->applicationDirPath()+"/test.html";
    QFile f1(qstrFile);
    f1.open(QFile::WriteOnly);
    QTextStream  ts(&f1);

    WriteHTML(ts);
      WirteTest1(ts);



    f1.close();


    QDesktopServices::openUrl(QUrl::fromLocalFile(qstrFile));

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::WriteHTML(QTextStream &ts)
{
    ts<<" <!DOCTYPE html>"<<endl;
    ts<<" <html>"<<endl;
    ts<<"<head>"<<endl;
    ts<<"<meta charset='UTF-8'>"<<endl;
    ts<<  "<title>AccoTest  </title>"<<endl;
  //  ts<<"<meta name='viewport' content='width=device-width, initial-scale=1.0'>"<<endl;
    ts<<"<style>"<<endl;
  ///  ts<<  "table{ width: 200px;height: 200px;border: solid 1px #000;border-collapse: collapse; }"<<endl;
  ///
  ///


    创建表格
   ts<<"table{text-align:left; border-collapse:collapse; margin-top:20px;}"<<endl;

    ts<<"tr,td,th {  border: solid 1px #000; }"<<endl;

    ts<<"</style>"<<endl;
    ts<<"</head>"<<endl;

    ts<<"<body>"<<endl;

    ///写在body内部
     ts<<"<tr>";
    ts<<"<th>";
    ts<<"AccoTest Correlation Version 1.0";
    ts<<"</th>";
    ts<<"</tr>"<<endl;


    ts<<"<p><tr>";
    ts<<"<th><strong>";
    ts<<"Data Time:   ";
    ts<<"</strong></th>";
    ts<<"<th>";
    QDateTime  dt=QDateTime::currentDateTime();
    ts<<dt.toString("yyyy/MM/dd hh:mm:ss");
    ts<<"</th>";
    ts<<"</tr></p>";

    ts<<"<p><tr>";
   ts<<"<th>";
   ts<<"PGS File:"<<"D:/test/acm.pgs";
   ts<<"</th>";
   ts<<"</tr></p>"<<endl;




    ts<< "<table>"<<endl;
    ts<<"<tr>  "<<endl;
    ts<<  "  <th><strong>Site </strong></th> "<<endl;
    ts<<  "  <th>ID</th>  "<<endl;
     ts<<  "  <th>NUM</th>  "<<endl;
    ts<<"  </tr>  "<<endl;


    ts<<"    <tr>"<<endl;
    ts<<  "   <td>1</td>"<<endl;
    ts<<   "  <td>2</td>"<<endl;
    ts<<  "   <td>3</td>"<<endl;
    ts<<" </tr>"<<endl;

    ts<<"    <tr>"<<endl;
    ts<<  "   <td>4</td>"<<endl;
    ts<<   "  <td>5</td>"<<endl;
    ts<<  "  <td>6</td>"<<endl;
    ts<<" </tr>"<<endl;

    ts<<"    <tr>"<<endl;
    ts<<  "   <td>7</td>"<<endl;
    ts<<   "  <td>8</td>"<<endl;
    ts<<  "  <td> 9</td>"<<endl;
    ts<<" </tr>"<<endl;
    ts<<"</table>"<<endl;





    ts<<"</body>"<<endl;


    ts <<"</html>"<<endl;



}

void MainWindow::WirteTest1(QTextStream &ts)
{
    ts<<"<!DOCTYPE html>"<<endl;
    ts<<"<html lang='en'>"<<endl;

    ts<<"<head>"<<endl;

    ts<<   " <meta charset='utf-8'>"<<endl;

    ts<< "<p align='center'><strong>1123</strong></p>"<<endl;
    ts<<"<p align='right'><em>111</em></p>"<<endl;
    ts<<"<p style='color: blue'>1111<p>"<<endl;
    ts<<" <p><u>555555</u></p>"<<endl;

    ts<<"<p style='color: blue'>99999'</p>"<<endl;
    ts<<"<p><del> 888888</del></p>"<<endl;



    ts<<"</body>"<<endl;
    ts<<"</html>"<<endl;


}

void MainWindow::on_pushButton_clicked()
{

    QString htmlPath=qApp->applicationDirPath();   ///路径
    /// htmlPath=qApp->applicationFilePath();  ///可执行文件的绝对路径 /xx.exe


    QDesktopServices::openUrl(QUrl::fromLocalFile(htmlPath+QDir::separator()+"1.html"));


}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QDesktopServices>
#include<QUrl>
#include<QDir>
#include<QFile>
#include<QTextStream>
#pragma  execution_character_set("utf-8")
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();



    void  WriteHTML(QTextStream & ts);
   void  WirteTest1(QTextStream & ts);
private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值