用Qt操作Word文档

用Qt操作Word文档

2016-01-27 11:01:23 mingxinke 阅读数 5259更多

分类专栏: QT

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/mingxinke/article/details/50592733

由于项目需要通过dot模板导出doc的文档,所以就查了资料写了一个操作word 的类,只实现了部分功能,其代码如下:

#include "wordengine.h"
#include "qt_windows.h"

WordEngine::WordEngine(QObject *parent) :
    QObject(parent),
    m_bOpened(false),
    m_wordDocuments(NULL),
    m_wordWidget(NULL)
{
}

WordEngine::WordEngine(const QString& strFilePath, QObject *parent):
    QObject(parent),
    m_bOpened(false),
    m_strFilePath(strFilePath),
    m_wordDocuments(NULL),
    m_wordWidget(NULL)
{
}

WordEngine::~WordEngine()
{
    close();
}

/******************************************************************************
 * 函数:open
 * 功能:打开文件
 * 参数:bVisable 是否显示弹窗
 * 返回值: bool
 *****************************************************************************/
bool WordEngine::open(bool bVisable)
{
    //新建一个word应用程序,并设置为可见
    m_wordWidget = new QAxObject();
    bool bFlag = m_wordWidget->setControl( "word.Application" );
    if(!bFlag)
    {
        // 用wps打开
        bFlag = m_wordWidget->setControl( "kwps.Application" );
        if(!bFlag)
        {
            return false;
        }
    }
    m_wordWidget->setProperty("Visible", bVisable);
    //获取所有的工作文档
    QAxObject *document = m_wordWidget->querySubObject("Documents");
    if(!document)
    {
        return false;
    }
    //以文件template.dot为模版新建一个文档
    document->dynamicCall("Add(QString)", m_strFilePath);
    //获取当前激活的文档
    m_wordDocuments = m_wordWidget->querySubObject("ActiveDocument");
    m_bOpened = true;
    return m_bOpened;
}

/******************************************************************************
 * 函数:open
 * 功能:打开文件
 * 参数:strFilePath 文件路径;bVisable 是否显示弹窗
 * 返回值:bool
 *****************************************************************************/
bool WordEngine::open(const QString& strFilePath, bool bVisable)
{
    m_strFilePath = strFilePath;
    close();
    return open(bVisable);
}

/******************************************************************************
 * 函数:close
 * 功能:关闭文件
 * 参数:无
 * 返回值:bool
 *****************************************************************************/
bool WordEngine::close()
{
    if (m_bOpened)
    {
        m_wordDocuments->dynamicCall("Close (bool)", false);
        m_wordWidget->dynamicCall("Quit()");
        m_bOpened = false;
        delete m_wordWidget;
        m_wordWidget = NULL;
    }

    return m_bOpened;
}

/******************************************************************************
 * 函数:isOpen
 * 功能:获得当前的打开状态
 * 参数:无
 * 返回值:bool
 *****************************************************************************/
bool WordEngine::isOpen()
{
    return m_bOpened;
}

/******************************************************************************
 * 函数:save
 * 功能:保存文件
 * 参数:无
 * 返回值:void
 *****************************************************************************/
void WordEngine::save()
{
    m_wordDocuments->dynamicCall("Save()");
}

/******************************************************************************
 * 函数:saveAs
 * 功能:另存文件
 * 参数:strSaveFile 文件路径
 * 返回值:void
 *****************************************************************************/
bool WordEngine::saveAs(const QString& strSaveFile)
{
    return m_wordDocuments->dynamicCall("SaveAs (const QString&)",
                                 strSaveFile).toBool();
}

/******************************************************************************
 * 函数:setMarks
 * 功能:设置标签内容
 * 参数:strMark 标签名;strContent 文本
 * 返回值:bool
 *****************************************************************************/
bool WordEngine::setMarks(const QString& strMark, const QString& strContent)
{
    QAxObject* bookmarkCode = NULL;
    bookmarkCode = m_wordDocuments->querySubObject("Bookmarks(QVariant)", strMark);
    //选中标签,将字符textg插入到标签位置
    if(bookmarkCode)
    {
        bookmarkCode->dynamicCall("Select(void)");
        bookmarkCode->querySubObject("Range")->setProperty("Text", strContent);
        return true;
    }

    return false;
}

/******************************************************************************
 * 函数:intsertTable
 * 功能:创建表格
 * 参数:nStart 开始位置; nEnd 结束位置; row hang; column 列
 * 返回值: void
 *****************************************************************************/
void WordEngine::intsertTable(int nStart, int nEnd, int row, int column)
{   
      QAxObject* ptst = m_wordDocuments->querySubObject( "Range( Long, Long )",
                                                         nStart, nEnd );
       QAxObject* pTable = m_wordDocuments->querySubObject( "Tables" );
       QVariantList params;
       params.append(ptst->asVariant());
       params.append(row);
       params.append(column);
       if( pTable )
       {
         pTable->dynamicCall( "Add(QAxObject*, Long ,Long )",params);
       }
//       QAxObject* table = selection->querySubObject("Tables(1)");
//       table->setProperty("Style", "网格型");
}

/******************************************************************************
 * 函数:setColumnWidth
 * 功能:设置表格列宽
 * 参数:nTable 表格; column 列数; width 宽度
 * 返回值:void
 *****************************************************************************/
void WordEngine::setColumnWidth(int nTable, int column, int width)
{
    QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = pTables->querySubObject("Item(int)", nTable);
    table->querySubObject("Columns(int)", column)->setProperty("Width", width);
}

/******************************************************************************
 * 函数:setCellString
 * 功能:设置表格内容
 * 参数:nTable 表格; row 行数; column 列数; text 插入文本
 * 返回值:void
 *****************************************************************************/
void WordEngine::setCellString(int nTable, int row, int column, const QString& text)
{
    QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = pTables->querySubObject("Item(int)", nTable);
    if(table)
    {
        table->querySubObject("Cell(int, int)", row, column)
                ->querySubObject("Range")
                ->dynamicCall("SetText(QString)", text);
    }
}

/******************************************************************************
 * 函数:addTableRow
 * 功能:为表格添加行
 * 参数:nTable 表格; nRow 插入行; rowCount 插入的行数
 * 返回值:void
 *****************************************************************************/
void WordEngine::addTableRow(int nTable, int nRow, int rowCount)
{
    QString sTableNo = QString("Item(%1)").arg(nTable);
    QAxObject* tables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = tables->querySubObject(sTableNo.toStdString().c_str());
    QAxObject* rows = table->querySubObject("Rows");

    int Count = rows->dynamicCall("Count").toInt();
    if(0 < nRow && nRow < Count)
    {
        for(int i = 0; i < rowCount; ++i)
        {
            QString sPos = QString("Item(%1)").arg(nRow + i);
            QAxObject* row = rows->querySubObject(sPos.toStdString().c_str());
            QVariant param = row->asVariant();
            rows->dynamicCall("Add(Variant)", param);
        }
    }
}

/******************************************************************************
 * 函数:setCellFontBold
 * 功能:设置内容粗体  isBold控制是否粗体
 * 参数:nTable 表格; row 插入行; column 列数; isBold 是否加粗
 * 返回值:void
 *****************************************************************************/
void WordEngine::setCellFontBold(int nTable, int row, int column, bool isBold)
{
    QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = pTables->querySubObject("Item(int)", nTable);
    table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")
            ->dynamicCall("SetBold(int)", isBold);
}

/******************************************************************************
 * 函数:setCellFontSize
 * 功能:设置文字大小
 * 参数:nTable 表格; row 插入行; column 列数; size 字体大小
 * 返回值:void
 *****************************************************************************/
void WordEngine::setCellFontSize(int nTable, int row, int column, int size)
{
    QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = pTables->querySubObject("Item(int)", nTable);
    table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")
            ->querySubObject("Font")->setProperty("Size", size);
}

/******************************************************************************
 * 函数:insertCellPic
 * 功能:在表格中插入图片
 * 参数:nTable 表格; row 插入行; column 列数; picPath 图片路径
 * 返回值:void
 *****************************************************************************/
void WordEngine::insertCellPic(int nTable, int row, int column,
                               const QString& picPath)
{
    QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
    QAxObject* table = pTables->querySubObject("Item(int)", nTable);
    QAxObject* range = table->querySubObject("Cell(int, int)", row, column)
            ->querySubObject("Range");
    range->querySubObject("InlineShapes")
            ->dynamicCall("AddPicture(const QString&)", picPath);
}
#ifndef WORDENGINE_H
#define WORDENGINE_H

#include <QObject>
#include <QAxObject>
#include <QAxWidget>

class WordEngine : public QObject
{
    Q_OBJECT
public:
    explicit WordEngine(QObject *parent = 0);
    WordEngine(const QString& strFile, QObject *parent = 0);
    ~WordEngine();

    bool open(bool bVisable = false);
    bool open(const QString& strFile, bool bVisable = false);
    bool close();
    bool isOpen();
    void save();
    bool saveAs(const QString& strSaveFile);
    // 设置标签内容
    bool setMarks(const QString& strMark, const QString& strContent);
    // 创建表格
    void intsertTable(int nStart, int nEnd, int row, int column);
    //设置列宽
    void setColumnWidth(int nTable, int column, int width);
    // 设置表格内容
    void setCellString(int nTable, int row, int column, const QString& text);
    // 为表格添加行
    void addTableRow(int nTable, int nRow, int rowCount);
    // 设置内容粗体  isBold控制是否粗体
    void setCellFontBold(int nTable, int row, int column, bool isBold);
    // 设置文字大小
    void setCellFontSize(int nTable, int row, int column, int size);
    // 在表格中插入图片
    void insertCellPic(int nTable, int row,int column,const QString& picPath);

private:
    bool m_bOpened;
    QString m_strFilePath;
    QAxObject *m_wordDocuments;
    QAxObject *m_wordWidget;

signals:

public slots:

};

#endif // WORDENGINE_H
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值