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,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}@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}@{1,1}@{1,1}@{1,1}@{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();
}
注意合并单元格时若正序合并,前面合并的单元格会影响后面单元格的合并,导致后面单元格的起始坐标发生偏移,此处采用倒序进行合并。