qt 查询数据库word插入文字图片

QT操作 word ,需要先制作一个 word 的 *.dot 模板,模板里先定好格式,并插入书签。QT是对书签进行查找和插入。

*.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <qdebug.h>
#include <QAxWidget>
#include <QAxObject>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    void openDot();
    void saveAndQuitDot();
    void createDir();

    QSqlDatabase database;

    QString year="";
    QString month="";
    QString day="";
    QString pageno="";
    bool openState=0;

    QString dir_str=""; //存入位置
    QString dbPath="";

    QAxWidget *m_pWord=nullptr;         //word操作需要的东西
    QAxObject *m_pDocument=nullptr;   //word操作需要的东西

    QAxObject *pDocuments=nullptr;         //word操作需要的东西
    QAxObject *pBookMarkCode=nullptr;       //word操作需要的东西

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

void MainWindow::openDot()
{
    pDocuments = m_pWord->querySubObject("Documents");
    pDocuments->dynamicCall("Add(Qstring)", QString::fromLocal8Bit((QCoreApplication::applicationDirPath().replace("/","\\")+"\\111.dot").toLatin1()));//模版目录
    m_pDocument = m_pWord->querySubObject("ActiveDocument");//获取当前激活的文档
}

void MainWindow::saveAndQuitDot()
{
    QString kkk=dir_str+"\\%1.doc";
    m_pDocument->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(kkk.arg(year+month+day+"-"+pageno)));//“/”换成“\”;否则在windows下保存不成功
    m_pDocument->dynamicCall("Close(boolean)", true);//关闭

    //QString filepath = "D:/CODES/2019-11-12/build-untitled2-Desktop_Qt_5_13_1_MSVC2017_32bit-Debug/debug/%1.doc";
}

void MainWindow::createDir()
{
    QString applicationDirPathStr = QCoreApplication::applicationDirPath().replace("/","\\");
    dir_str = applicationDirPathStr+"\\"+year+"\\"+month+"\\"+day;
    // 检查目录是否存在,若不存在则新建
    QDir dir;
    if (!dir.exists(dir_str))
    {
        bool res = dir.mkpath(dir_str);
        qDebug() << "新建目录是否成功" << res;
    }
}


void MainWindow::on_pushButton_clicked()
{
    dbPath =QFileDialog::getOpenFileName(this,"select","D:/",tr("db(*.db);;"));
    if (dbPath.isEmpty())
    {
        QMessageBox::warning(this, "Warning!", "Failed to open the db!");
    }
    else
    {
        ui->lineEdit->setText(dbPath);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    //建立并打开数据库

    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName(dbPath);
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else
    {

        int a=1;  //20141201-2 里面第a条内容
        int ImageId=0;    //word里第几张图片
        m_pWord = new QAxWidget("Word.Application");//新建一个word应用程序
        m_pWord->setProperty("Visible", false);//不显示窗体


        QSqlQuery query;
        if(query.exec("select * from TB_MEMO"))
        {
            int b=0;
            while (query.next()) {
                if(query.value(2).toString().length()>8)
                {
                    saveAndQuitDot();       //20190101格式以外的文档不处理
                    break;
                }
                if(query.value(3).toString().length()>2)
                {
                    saveAndQuitDot();         //pageno格式不对的文档不处理
                    break;
                }
                if(query.value(3).toInt()!=pageno.toInt() || query.value(2).toInt()%100!=day.toInt())//pageno改变就保存文档
                {
                    if(openState==true)
                    {
                        saveAndQuitDot();
                        openState=false;
                        b++;
                    }
                    year=query.value(2).toString().mid(0,4);
                    month=query.value(2).toString().mid(4,2);
                    day=query.value(2).toString().mid(6,2);
                    qDebug()<<query.value(2).toString().mid(0,4);
                    qDebug()<<query.value(2).toString().mid(4,2);
                    qDebug()<<query.value(2).toString().mid(6,2);
                    pageno=query.value(3).toString();
                    qDebug()<<pageno;
                    if(b>100)
                    {break;}
                    createDir();
                    openDot();
                    openState=true;
                    a=1;
                    ImageId=0;
                }
                QString kk="title";
                qDebug()<<kk+QString::number(a*10+1);
                pBookMarkCode = m_pDocument->querySubObject("Bookmarks(QVariant)", kk+QString::number(a*10+1));//获取指定标签
                if (pBookMarkCode)
                {
                    pBookMarkCode->dynamicCall("Select(void)");//选择该指定标签
                    pBookMarkCode->querySubObject("Range")->setProperty("Text", query.value(5).toString());//往标签处插入文字
                }

                pBookMarkCode = m_pDocument->querySubObject("Bookmarks(QVariant)", kk+QString::number(a*10+2));
                if (pBookMarkCode)
                {
                    pBookMarkCode->dynamicCall("Select(void)");
                    pBookMarkCode->querySubObject("Range")->setProperty("Text", query.value(6).toString());
                }

                QString content=query.value(12).toString();
                if(content.contains("AddImage:=")){                        //数据库中查找图片名称路径
                    int begin= content.indexOf("AddImage:=");
                    int end= content.indexOf("EndImage");
                    QString imgpath=content.mid(begin+10,end-begin-10);
                    content=content.replace("AddImage:=","");
                    content=content.replace(imgpath,"");
                    content=content.replace("EndImage","");
                    qDebug() <<"imgpath==="<<"D:\\CODES\\2019-11-12\\build-Museum-Desktop_Qt_5_13_1_MinGW_32_bit-Release\\release\\"+imgpath;

                    pBookMarkCode = m_pDocument->querySubObject("Bookmarks(QVariant)", kk+QString::number(a*10+3));//获取指定标签
                    if (pBookMarkCode)
                    {
                        ImageId++;
                        pBookMarkCode->dynamicCall("Select(void)");        //插入图片
                        QAxObject *Inlineshapes = m_pDocument->querySubObject("InlineShapes");
                        Inlineshapes->dynamicCall("AddPicture(const QString&)",QDir::toNativeSeparators(("D:\\CODES\\2019-11-12\\build-Museum-Desktop_Qt_5_13_1_MinGW_32_bit-Release\\release\\"+imgpath)));
                        QAxObject* inlineshape = m_pDocument->querySubObject("InlineShapes(int)", ImageId);
                        //选中当前图片
                        inlineshape->dynamicCall("Select(void)");
                        //调整图片大小
                        QPixmap image1("D:\\CODES\\2019-11-12\\build-Museum-Desktop_Qt_5_13_1_MinGW_32_bit-Release\\release\\"+imgpath);
                        inlineshape->setProperty("Width",image1.width());
                        qDebug() <<image1.width()<<"-----"<<image1.height();
                        inlineshape->setProperty("Height",image1.height());
                        delete Inlineshapes;
                    }

                    pBookMarkCode = m_pDocument->querySubObject("Bookmarks(QVariant)", kk+QString::number(a*10+4));
                    if (pBookMarkCode)
                    {
                        pBookMarkCode->dynamicCall("Select(void)");
                        pBookMarkCode->querySubObject("Range")->setProperty("Text", content+query.value(7).toString());
                    }

                }else{
                    pBookMarkCode = m_pDocument->querySubObject("Bookmarks(QVariant)", kk+QString::number(a*10+4));
                    if (pBookMarkCode)
                    {
                        pBookMarkCode->dynamicCall("Select(void)");
                        pBookMarkCode->querySubObject("Range")->setProperty("Text", query.value(12).toString()+query.value(7).toString());
                    }
                }

                a++;
            }
        }
        else
        {
            qDebug() << "Failed *" ;
        }


        m_pWord->setProperty("DisplayAlerts", false);
        m_pWord->dynamicCall("Quit()");//退出
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值