基于Qt中的QAxObject实现指定表格合并数据进行word表格的合并

基于Qt中的QAxObject实现指定表格合并数据进行word表格的合并

合并规则

cpp 复制代码
"{3,1}@{3,1}@{3,1}@{1,6}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,2}@{1,1}@{1,2}@{1,1}@{1,2}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{2,2}@{1,1}@{1,1}@{2,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{4,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
"{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}"

上方数据代表26行9列的表格,其中{}中的数据代表单元格的合并行数以及合并列数,例如{3,1}代表合并3行1列数据

代码

cpp 复制代码
#include <QAxObject>
#include <QApplication>
#include <QFile>
#include <QDebug>


QString getCellPoint(int rowIn, int colIn)
{
    QString rS = QString::number(rowIn);
    QString cS = QString::number(colIn);
    QString rSw = rS.rightJustified(2,'0');
    QString cSw = cS.rightJustified(2,'0');
    QString cellPoint = "("+rSw+","+cSw+")";
    return cellPoint;
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    //testPython();
    QString tableDataStr= "{3,1}@{3,1}@{3,1}@{1,6}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,2}@{1,1}@{1,2}@{1,1}@{1,2}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{2,2}@{1,1}@{1,1}@{2,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{4,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{3,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{2,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}#"
                          "{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}@{1,1}";
    QStringList rowList = tableDataStr.split("#");
    QMap<QString,QMap<QString,QVariant>> tableCellMap;
    QMap<QString,QStringList> acTableList;
    QMap<QString,QString> skipMap;
    int rowCount = rowList.size();
    int columnCount = 0;
    int rowIn = 1;

    for(QString rowS:rowList){
        QStringList colList = rowS.split("@");
        if(columnCount<colList.size()){
            columnCount = colList.size();
        }
        int colIn = 1;
        for(QString colS:colList){
            QString xy = getCellPoint(rowIn, colIn);
            QString cellSS = colS.mid(1,colS.length()-2);
            QStringList cellList = cellSS.split(",");
            int rowspan = cellList.at(0).toInt();
            int colspan = cellList.at(1).toInt();
            int mrc = rowspan-1;
            int mcc = colspan-1;
            int sr = rowIn;
            int sc = colIn;
            int er = sr+mrc;
            int ec = sc+mcc;
            QString endPoint = getCellPoint(er,ec);
            QString sxy = getCellPoint(sr, sc);
            QString exy = getCellPoint(er, ec);
            QMap<QString,QVariant> cellMap;
            cellMap.insert("x",rowIn);
            cellMap.insert("y",colIn);
            cellMap.insert("mrc",mrc);
            cellMap.insert("mcc",mcc);
            cellMap.insert("xy",xy);
            cellMap.insert("sr",sr);
            cellMap.insert("sc",sc);
            cellMap.insert("er",er);
            cellMap.insert("ec",ec);
            cellMap.insert("sxy",sxy);
            cellMap.insert("exy",exy);
            cellMap.insert("text",xy);
            cellMap.insert("rowspan",rowspan);
            cellMap.insert("colspan",colspan);
            if(!skipMap.contains(xy)){
                acTableList.insert(xy,QStringList());
                for(int rs=rowIn;rs<=er;rs++){
                    for(int cs=colIn;cs<=ec;cs++){
                        QString exy = getCellPoint(rs,cs);
                        if(exy!=xy){
                            skipMap.insert(exy,xy);
                            QStringList exyL = acTableList.value(xy);
                            if(!exyL.contains(exy)){
                                exyL.append(exy);
                                acTableList.insert(xy,exyL);
                            }
                        }
                    }
                }
            }
            tableCellMap.insert(xy,cellMap);
            colIn++;
        }
        rowIn++;
    }
    QString filepath = "C://Users//tirkl//Desktop//docTest//testTable//1.docx";
    QFile file(filepath);
    if(file.exists()){
        file.exists();
    }
    QAxObject m_word;
    bool flag = m_word.setControl("Word.Application");
    if (!flag)
    {
        flag = m_word.setControl("kwps.Application");//尝试用wps打开
        if (!flag)
            return  0;
    }
    QAxObject* m_documents = m_word.querySubObject("Documents");
    if(!m_documents){
        return 0;
    }
    //打开表格文档
    m_documents->dynamicCall("Add(void)");
    //表格文档对象
    QAxObject* tableDocument = m_word.querySubObject("ActiveDocument");
    if(!tableDocument){
        return 0;
    }
    QAxObject* Content = tableDocument->querySubObject("Content");
    if(!Content){
        return 0;
    }
    //表格范围
    QAxObject* Tables = tableDocument->querySubObject("Tables");
    if(!Tables){
        return 0;
    }

    QAxObject* Table = Tables->querySubObject("Add(QAxObject*,int&,int&)",Content->asVariant(),rowCount,columnCount);
    if(!Table){
        return 0;
    }
    QStringList acTableKeyList = acTableList.keys();
    while(!acTableKeyList.isEmpty()){
        QString xyKey = acTableKeyList.takeLast();
        QMap<QString,QVariant> cellMap = tableCellMap.value(xyKey);
        int sr = cellMap.value("sr").toInt();
        int sc = cellMap.value("sc").toInt();
        int er = cellMap.value("er").toInt();
        int ec = cellMap.value("ec").toInt();
        QString text = cellMap.value("text").toString();
        QAxObject* Cell = Table->querySubObject("Cell(int&,int&)",sr,sc);
        if(Cell){
            if(!(sr==er && sc==ec)){
                QAxObject* MergeCell = Table->querySubObject("Cell(int&,int&)",er,ec);
                if(MergeCell){
                    Cell->querySubObject("Merge(QAxObject*)",MergeCell->asVariant());
                }
            }
            QAxObject* CellRange = Cell->querySubObject("Range");
            if(CellRange){
                CellRange->setProperty("Text",text);
            }
        }

    }
    Table->setProperty("Style","网格型");
    tableDocument->querySubObject("SaveAs2(QString&)",filepath);
    //关闭表格文档
    tableDocument->dynamicCall("Close(boolean)", false);
    //退出word
    m_word.dynamicCall("Quit()");
    return app.exec();
}

注意合并单元格时若正序合并,前面合并的单元格会影响后面单元格的合并,导致后面单元格的起始坐标发生偏移,此处采用倒序进行合并。

合并效果

相关推荐
lzhdim2 小时前
SharpCompress:跨平台的 C# 压缩与解压库
开发语言·c#
嘿嘿嘿x32 小时前
Linux记录过程
linux·开发语言
默 语2 小时前
Records、Sealed Classes这些新特性:Java真的变简单了吗?
java·开发语言·python
止观止2 小时前
拥抱 ESNext:从 TC39 提案到生产环境中的现代 JS
开发语言·javascript·ecmascript·esnext
卷心菜狗2 小时前
Python进阶-深浅拷贝辨析
开发语言·python
时寒的笔记2 小时前
js逆向7_案例惠nong网
android·开发语言·javascript
Evand J2 小时前
【MATLAB例程】基于低精度IMU、GNSS的UAV初始航向(三维角度)校准的仿真,包含卡尔曼滤波、惯导解算与校正
开发语言·matlab·gnss·imu·卡尔曼滤波
feng_you_ying_li3 小时前
c++之哈希表的介绍与实现
开发语言·c++·散列表
网域小星球3 小时前
C 语言从 0 入门(十四)|文件操作:读写文本、保存数据持久化
c语言·开发语言·文件操作·fopen·fprintf
网域小星球3 小时前
C 语言从 0 入门(七)|字符数组与字符串完整精讲|VS2022 高质量实战
c语言·开发语言·字符串·vs2022·字符数组