qt之导出word

前言:

qt导出word

环境:

qt5.7(MinGW)

正文:

word.h

#ifndef QWORD_H
#define QWORD_H
#include 
#include 
#include 
#include 
#include 
#include 

class QWord : public QObject
{
    Q_OBJECT

public:
    QWord(QObject *parent = 0);
    ~QWord();

public:
    QAxObject* getDocuments(){return m_documents;}
    QAxObject* getDocument(){return m_document;}
    QAxObject* getWordApp(){return m_word;}
public:
    /**************************************************************************/
    /* 文件   操作                                                            */
    /**************************************************************************/
    void save();				//保存操作内容
    void close();				//关闭 退出 析构时候也会自动调用一次
    void saveAs();				//新建excel另存为
    bool createNewWord();		//创建一个新的excel
    void openWord();			//打开一个现有的excel
    bool createNewWord(const QString& filePath );		//创建一个新的excel

    void setTableAutoFitBehavior(int flag);				//表格自动拉伸列 0固定  1根据内容调整  2 根据窗口调整
    void setPageOrientation(int flag);					//设置页面0为纵向wdOrientPortrait  1为横向wdOrientLandscape
    void setWordPageView(int flag);						//设置页面视图,是web视图wdWebView 6还是页面视图wdPrintView 3 或者阅读视图wdReadingView 7
    void setFontSize(int fontsize);						//设置字体大小
    void setFontName(QString& fontName);				//设置字体	比如 “宋体”
    void setFontBold(bool flag);						//字体加粗
    void setParagraphAlignment(int flag);				//设置选中位置文字居中 0 ,居左 1,居右 2
    void setColumnWidth(int column, int width);			//设置列宽
    void setCellString(int row, int column, const QString& text);
    void setCellFontBold(int row, int column, bool isBold);			//设置内容粗体  isBold控制是否粗体
    void setCellFontSize(int row, int column, int size);			//设置文字大小
    void setOptionCheckSpell(bool flags);							//设置审阅的拼写检查  true开启检查  false 取消检查

    QString GetText();												//获取内容
    void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);
    void setSelectionRange(int start,int end);						//"SetRange(1, 9)"第1个字符后开始,到第9个字符结束范围
    QVariant getCellValue(int row, int column);						//获取单元格内容
    int getTableCount();											//获取word中表格总数


    QString getStrErrorInfo(){return m_strError;}					//获取代码中出现的错误信息可以用QMessageBox::information打印 在cpp不用QMessageBox 是怕你们在线程中调导出报表
    void deleteSelectColumn(int column);							//删除指定的列
    void moveForEnd();												//移动选定对象到文档末尾
    void insertCellPic(int row,int column,const QString& picPath);	//单元格插入图片
    void intsertTable(int row,int column);							//插入一个几行几列表格
    void insertMoveDown();											//插入回车
    void insertText(const QString& text);							//插入文字

    void inserttestdata(testDataType &str);
    void insertselfdata(selfDataType &str);
private:
    QAxObject* m_word;
    QAxObject* m_documents;
    QAxObject* m_document;
    QString  m_fileName;
    QString  m_saveName;
    QString  m_strError;

};

#endif // QWORD_H

word.cpp

#include "qword.h"
#include 
#include 
#include 
#include 
#include 
QWord::QWord(QObject *parent)
{
    m_word = new QAxObject(parent);
    m_documents = NULL;
    m_document = NULL;
}

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

bool QWord::createNewWord()		//创建一个新的word
{
    //这种方式有些bug,选择路径取消会导致程序崩溃,直接传入路径,自己操作路径则不会出现问题
    /*QString defaultFileName = tr("测试记录%1").arg(QDateTime::currentDateTime().toString("yyyy年MM月dd日hh时mm分"));
    m_saveName=QFileDialog::getSaveFileName(0,"日志信息",defaultFileName,tr("*.doc"));
    QFile file(m_saveName);
    if(file.exists())
    {
        m_strError += "错误:目标文件已存在!";
        return false;
    }
    if(!m_saveName.isEmpty())
    {
        if(!m_word->setControl("Word.Application"))
        {
            m_strError += "错误:获取word组件失败,请确定是否安装了word!";
            return false;
        }
        m_word->setProperty("Visible",false);
        m_word->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
        m_documents = m_word->querySubObject("Documents");
        m_documents->dynamicCall("Add (void)");
        m_document = m_word->querySubObject("ActiveDocument");//获取当前激活的文档
        return true;
    }else
    {
        m_strError += "错误:文件名为空";
        return false;
    }*/
}
bool QWord::createNewWord(const QString& filePath )
{
    m_saveName = filePath;
    QFile file(m_saveName);
    if(file.exists())
    {
        m_strError += "错误:目标文件已存在!";
        return false;
    }
    if(!m_saveName.isEmpty())
    {
        if(!m_word->setControl("Word.Application"))
        {
            m_strError += "错误:获取word组件失败,请确定是否安装了word!\n";
            return false;
        }
        m_word->setProperty("Visible",false);
        m_word->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
        m_documents = m_word->querySubObject("Documents");
        if(!m_documents)
        {
            m_strError += "获取文档失败!\n";
            return false;
        }
        m_documents->dynamicCall("Add (void)");
        m_document = m_word->querySubObject("ActiveDocument");//获取当前激活的文档
        return true;
    }else
    {
        m_strError += "错误:文件名为空";
        return false;
    }
}
//保存
void QWord::save()
{
    if(m_document)
        m_document->dynamicCall("Save()");
    else
        return;
}
//关闭word
void QWord::close()				//关闭 退出 析构时候也会自动调用一次
{
    if(!m_saveName.isEmpty())		//如果不为空  则为新建
    {
        saveAs();
        m_saveName = "";
    }
    if(m_document)
        m_document->dynamicCall("Close (boolean)",false);
    if(m_word)
        m_word->dynamicCall("Quit (void)");
    if(m_documents)
        delete m_documents;
    if(m_word)
        delete m_word;
    m_document = NULL;
    m_documents = NULL;
    m_word = NULL;
}
//另存为
void QWord::saveAs()
{
    if(m_document)
        m_document->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(m_saveName));
    else
        return;
}
//设置页面1 横向还是 0竖向
void QWord::setPageOrientation(int flag)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QString page;
    switch (flag)
    {
    case 0:
        page = "wdOrientPortrait";
        break;
    case 1:
        page = "wdOrientLandscape";
        break;
    }
    selection->querySubObject("PageSetUp")->setProperty("Orientation",page);
}
//设置页面视图
void QWord::setWordPageView(int flag)
{
    QAxObject* viewPage = m_word->querySubObject("ActiveWindow");
    QString view;
    switch (flag)
    {
    case 1:
        view = "wdNormalView";
        break;
    case 2:
        view = "wdOutlineView";
        break;
    case 3:
        view = "wdPrintView";
        break;
    case 4:
        view = "wdPrintPreview";
        break;
    case 5:
        view = "wdMasterView";
        break;
    case 6:
        view = "wdWebView";
        break;
    case 7:
        view = "wdReadingView";
        break;
    case 8:
        view = "wdConflictView";
        break;
    }
    viewPage->querySubObject("View")->setProperty("Type",view);
}
//插入回车
void QWord::insertMoveDown()
{
    QAxObject* selection  = m_word->querySubObject("Selection");
    selection->dynamicCall("TypeParagraph(void)");
}
//插入文字
void QWord::insertText(const QString& text)
{
    QAxObject* selection  = m_word->querySubObject("Selection");
    selection->dynamicCall("TypeText(const QString&)",text);
}
//获取文本
QString QWord::GetText()
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QString str = selection->dynamicCall("GetText(void)").toString();
    return str;
}
//设置文字对齐方向
void QWord::setParagraphAlignment(int flag)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    if(flag == 0)
    {
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter");
    }else if(flag == 1)
    {
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphJustify");
    }else if(flag == 2)
    {
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight");
    }
}
//设置字体大小
void QWord::setFontSize(int fontsize)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    selection->querySubObject("Font")->setProperty("Size",fontsize);
}
//设置字体加粗
void QWord::setFontBold(bool flag)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    selection->querySubObject("Font")->setProperty("Bold",flag);
}
//设置字体名称
void QWord::setFontName(QString& fontName)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    selection->querySubObject("Font")->setProperty("Name",fontName);
}
//未知
void QWord::setSelectionRange(int start,int end)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    selection->dynamicCall("SetRange(int, int)", start,end);	//第1个字符后开始,到第9个字符结束范围
}
//未知
void QWord::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)
{
    QAxObject* range = m_document->querySubObject("Range");
    *topLeftRow = range->property("Row").toInt();
    *topLeftColumn = range->property("Column").toInt();

    QAxObject *rows = range->querySubObject("Rows");
    *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;

    QAxObject *columns = range->querySubObject("Columns");
    *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
}
//插入表格
void QWord::intsertTable(int row,int column)
{
    QAxObject* tables = m_document->querySubObject("Tables");
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* range = selection->querySubObject("Range");
    QVariantList params;
    params.append(range->asVariant());
    params.append(row);
    params.append(column);
    tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
    QAxObject* table = selection->querySubObject("Tables(1)");
    table->setProperty("Style","网格型");
}
//设置表格列宽
void QWord::setColumnWidth(int column, int width)		//设置列宽
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    table->querySubObject("Columns(int)",column)->setProperty("Width",width);
}
//设置表格某行某列的字符串
void QWord::setCellString(int row, int column, const QString& text)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
}
//设置表格内容粗体  isBold控制是否粗体
void QWord::setCellFontBold(int row, int column, bool isBold)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
}
//设置表格文字大小
void QWord::setCellFontSize(int row, int column, int size)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->querySubObject("Font")->setProperty("Size", size);
}
//获取单元格内容 此处对于Excel来说列和行从1开始最少
QVariant QWord::getCellValue(int row, int column)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    return table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->property("Text");
}
//获取表格数量
int QWord::getTableCount()
{
    QAxObject* tables = m_document->querySubObject("Tables");
    int val = tables->property("Count").toInt();
    return val;
}
//移动选定对象到文档末尾
void QWord::moveForEnd()
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QVariantList params;
    params.append(6);
    params.append(0);
    selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
}
//表格插入图片
void QWord::insertCellPic(int row,int column,const QString& picPath)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    QAxObject* range = table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range");
    range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picPath);
}
//未知
void QWord::setTableAutoFitBehavior(int flag)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    if(0 <= flag & flag <= 2 )
        table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
}
//删除选中列
void QWord::deleteSelectColumn(int column)
{
    QAxObject* selection = m_word->querySubObject("Selection");
    QAxObject* table = selection->querySubObject("Tables(1)");
    QAxObject* columns = table->querySubObject("Columns(int)",column);
    columns->dynamicCall("Delete()");
}

void QWord::setOptionCheckSpell(bool flags)
{
    QAxObject* opetions = m_word->querySubObject("Options");
    if(!opetions)
        return;
    opetions->setProperty("CheckGrammarAsYouType",flags);
    opetions->setProperty("CheckGrammarWithSpelling",flags);
    opetions->setProperty("ContextualSpeller",flags);
    opetions->setProperty("CheckSpellingAsYouType",flags);
}

调用程序.cpp

      
    QString dir = QFileDialog::getExistingDirectory(this, tr("选择信息导出文件目录"),"/home",QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
    QString filePath = QString(dir+"/"+"测试数据%1.docx").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
    QFile file(filePath);
    if(dir.isEmpty())
        massage_dialog(1,"提示","数据保存失败!未选择保存路径!",0,0);
    else if(file.exists())
        massage_dialog(1,"提示","数据保存失败!保存路径下存在相同文件!",0,0);


  QWord word;
        word.createNewWord(filePath);   //创建一个新的word
        word.inserttestdata(testdata);      //写入数据
        word.close();                   //关闭word
        massage_dialog(1,"提示","数据测试文件生成成功!",0,0);

inserttestdata是自己写的方法,传参是一个结构体,提前把需要传的数据存储到结构体当中

void QWord::insertselfdata(selfDataType &str)
{
    //先创建固定表头等内容
    setPageOrientation(0);			//页面纵向
    setWordPageView(3);			//页面视图
    insertMoveDown();				//插入回车
    setFontSize(25);				//字体大小
    setParagraphAlignment(0);		//下面文字置中
    setFontBold(true);				//字体加粗
    insertText(QString("xxxxxx输出报表"));//插入文字
    setFontBold(false);			//字体加粗
    insertMoveDown();              //插入回车
    setParagraphAlignment(1);		//下面文字置左
    setFontSize(16);               //字体大小
    insertText(QString("xx人员:%1").arg(str.user));//插入文字
    setFontSize(12);				//字体大小
    insertMoveDown();              //插入回车
    insertText(QString("——————————————————————————————————"));//插入文字
    insertMoveDown();              //插入回车
    setFontSize(16);               //字体大小
    insertText(QString("被测设备编号:%1").arg(str.number));//插入文字
    insertMoveDown();                         //插入回车
    insertText(tr("xx日期:%1").arg(QDateTime::currentDateTime().toString("yyyy年MM月dd日 hh时:mm分")));//插入文字
    setFontSize(12);               //字体大小
    insertMoveDown();              //插入回车
    insertText(QString("——————————————————————————————————"));//插入文字
    insertMoveDown();              //插入回车

    //添加自检数据内容
    QString tabname[24]={xxxx};
    insertText(tr("xxxx检测结果"));
    insertMoveDown();                       //插入回车
    for(int i=0;i<24;i++){
        insertText(tr("%1:%2").arg(tabname[i]).arg(str.value[i]));         //插入文字
        insertMoveDown();              //插入回车
    }
    insertText(QString("——————————————————————————————————"));//插入文字
    insertMoveDown();              //插入回车
}

 

你可能感兴趣的:(qt,qt,word)