解析表格
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QAxObject>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QDebug>
#include <QSet>
#include <QPoint>
#include <QFile>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass CellInfo:public QObject{Q_OBJECT
public:explicit CellInfo(QObject* parent=nullptr);~CellInfo();QPair<int, int> id;QString text;QString bgColor;QString fgColor;int rowIndex = 1;int colIndex = 1;QString width = 0;QString heigth = 0;QString tPoint = 0 ;QString lPoint = 0 ;QString rPoint = 0 ;QString bPoint = 0 ;int colSpan=1;int rowSpan=1;QString type;QList<QString> typeDataList;
};class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
public:void displayWordTableInQTableWidget1(const QString& filePath);void displayWordTableInQTableWidget2(const QString& filePath);void displayExcelTableInQTableWidget1(const QString& filePath);
private:Ui::MainWindow *ui;QString filePath;QString excelPath;QString resTxt;QString cellWordOpenXMLtxt;QString xMLtxt;
};
#endif
源代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->filePath = "C:\\Users\\tirklee\\Desktop\\text.docx";this->excelPath = "C:\\Users\\tirklee\\Desktop\\1111.xlsx";this->resTxt = "C:\\Users\\tirklee\\Desktop\\Res.txt";this->cellWordOpenXMLtxt = "C:\\Users\\tirklee\\Desktop\\cellWordOpenXMLtxt.txt";this->xMLtxt = "C:\\Users\\tirklee\\Desktop\\XMLtxt.txt";displayExcelTableInQTableWidget1(excelPath);}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::displayWordTableInQTableWidget1(const QString &filePath)
{QAxObject word("Word.Application");word.setProperty("Visible", false);QAxObject* documents = word.querySubObject("Documents");QAxObject* document = documents->querySubObject("Open(const QString&)", filePath);if (!document) {qDebug() << "Failed to open the document.";return;}QAxObject* tables = document->querySubObject("Tables");int tableCount = tables->dynamicCall("Count").toInt();if (tableCount < 1) {qDebug() << "No table found in the document.";document->dynamicCall("Close()");word.dynamicCall("Quit()");return;}QAxObject* table = tables->querySubObject("Item(int)", 1);QAxObject* Rows = table->querySubObject("Rows");int rowCount = Rows->property("Count").toInt();QAxObject* Columns = table->querySubObject("Columns");int columnCount = Columns->property("Count").toInt();ui->tableWidget->setRowCount(rowCount);ui->tableWidget->setColumnCount(columnCount);QMap<QPair<int, int>, CellInfo*> cellMap;QSet<double> xPointSet;QSet<double> yPointSet;bool ok = true;QMap<int,int> zWidthMap;QMap<int,int> zHeightMap;for (int row = 1; row <= rowCount; ++row) {for (int col = 1; col <= columnCount && ok; ++col) {QAxObject* cell = table->querySubObject("Cell(int, int)", row, col);if (!cell) {ok = false;continue;}int rowSpan = 0;int colSpan = 0;for (int colX = 1; colX <= columnCount && ok; ++colX) {QAxObject* Column = Columns->querySubObject("Item(int)",colX);if (!Column || colX<col) {continue;}QAxObject* Cells = Column->querySubObject("Cells");if(!Cells){continue;}int cellCount = Cells->property("Count").toInt();for(int cellIndex=1;cellIndex<cellCount;cellIndex++){QAxObject* cellX = Column->querySubObject("Cells(int)",col);if(!cellX){continue;}int columnIndex = cellX->property("ColumnIndex").toInt();int rowIndex = cellX->property("RowIndex").toInt();if(row==rowIndex && columnIndex==col){colSpan = colSpan+1;}}}for (int rowX = 1; rowX <= rowCount; ++rowX) {QAxObject* Row = Rows->querySubObject("Item(int)",rowX);if (!Row || rowX<row) {continue;}QAxObject* Cells = Row->querySubObject("Cells");if(!Cells){continue;}int cellCount = Cells->property("Count").toInt();for(int cellIndex=1;cellIndex<cellCount;cellIndex++){QAxObject* cellX = Row->querySubObject("Cells(int)",col);if(!cellX){continue;}int columnIndex = cellX->property("ColumnIndex").toInt();int rowIndex = cellX->property("RowIndex").toInt();if(row==rowIndex && columnIndex==col){rowSpan = rowSpan+1;}}}CellInfo* cellInfo = new CellInfo;cellInfo->rowSpan = rowSpan;cellInfo->colSpan = colSpan;cellInfo->rowIndex = row;cellInfo->colIndex = col;QPair<int, int> point = QPair<int, int>(row,col);cellInfo->id = point;cellInfo->width = QString::number(cell->property("Width").toDouble(),'f',2);cellInfo->heigth = QString::number(cell->property("Height").toDouble(),'f',2);double zWidth = zWidthMap.value(row)+cellInfo->width.toInt();double zHeight = zHeightMap.value(col)+cellInfo->heigth.toInt();zWidthMap.insert(row,zWidth);zHeightMap.insert(col,zHeight);if(col>1){int lCIndex = col-1;const QPair<int, int> lPoint = QPair<int, int>(row,lCIndex);if(cellMap.contains(lPoint)){CellInfo* cellInfoL = cellMap.value(lPoint);cellInfo->lPoint = cellInfoL->rPoint;}}if(row>1){int tRIndex = row-1;QPair<int, int> tPoint = QPair<int, int>(tRIndex,col);if(cellMap.contains(tPoint)){CellInfo* cellInfoT = cellMap.value(tPoint);cellInfo->tPoint = cellInfoT->bPoint;}}double lPoint = cellInfo->lPoint.toDouble();double tPoint = cellInfo->tPoint.toDouble();double width = cellInfo->width.toDouble();double heigth = cellInfo->heigth.toDouble();cellInfo->rPoint = QString::number(lPoint+width,'f',2);cellInfo->bPoint = QString::number(tPoint+heigth,'f',2);xPointSet.insert(cellInfo->lPoint.toDouble());xPointSet.insert(cellInfo->rPoint.toDouble());yPointSet.insert(cellInfo->tPoint.toDouble());yPointSet.insert(cellInfo->bPoint.toDouble());QAxObject* range = cell->querySubObject("Range");QString cellText = range->property("Text").toString().trimmed();cellInfo->text = cellText;cellMap.insert(point, cellInfo);delete range;delete cell;}ok = true;}QList<double> xPointList = xPointSet.values();QList<double> yPointList = yPointSet.values();QList<int> zWidthList = zWidthMap.values();QList<int> zHeightList = zHeightMap.values();std::sort(xPointList.begin(), xPointList.end());std::sort(yPointList.begin(), yPointList.end());std::sort(zWidthList.begin(), zWidthList.end());std::sort(zHeightList.begin(), zHeightList.end());int rmZxCount = xPointList.size() - columnCount;int rmZyCount = yPointList.size() - rowCount;int rmxCount = rmZxCount>1?rmZxCount-1:0;int rmyCount = rmZyCount>1?rmZyCount-1:0;for(int rmZxIndex=0;rmZxIndex<rmxCount;rmZxIndex++){xPointList.takeLast();}for(int rmZyIndex=0;rmZyIndex<rmyCount;rmZyIndex++){yPointList.takeLast();}QList<QPair<int, int>> pointList = cellMap.keys();for (QPair<int, int>& point : pointList) {CellInfo* cellInfo = cellMap.value(point);cellInfo->rowSpan = 0;cellInfo->colSpan = 0;double lPoint = cellInfo->lPoint.toDouble();double rPoint = cellInfo->rPoint.toDouble();double tPoint = cellInfo->tPoint.toDouble();double bPoint = cellInfo->bPoint.toDouble();for(double xPointSIndex:xPointList){if(xPointSIndex>lPoint && xPointSIndex<=rPoint){cellInfo->colSpan = cellInfo->colSpan+1;}}for(double yPointSIndex:yPointList){if(yPointSIndex>tPoint && yPointSIndex<=bPoint){cellInfo->rowSpan = cellInfo->rowSpan+1;}}ui->tableWidget->setItem(cellInfo->rowIndex - 1, cellInfo->colIndex - 1, new QTableWidgetItem(cellInfo->text));ui->tableWidget->setSpan(cellInfo->rowIndex - 1, cellInfo->colIndex - 1,cellInfo->rowSpan,cellInfo->colSpan);}document->dynamicCall("Close()");word.dynamicCall("Quit()");
}void MainWindow::displayWordTableInQTableWidget2(const QString &filePath)
{QFile resTxtFile(this->resTxt);QFile cellWordOpenXMLTxtFile(this->cellWordOpenXMLtxt);QFile xMLTxtFile(this->xMLtxt);QStringList resTxtList;QStringList cellWordOpenXMLList;QStringList xMLList;if(!resTxtFile.open(QIODevice::WriteOnly)){return;}if(!cellWordOpenXMLTxtFile.open(QIODevice::WriteOnly)){return;}if(!xMLTxtFile.open(QIODevice::WriteOnly)){return;}QTextStream resTxtFileOut(&resTxtFile);QTextStream cellWordOpenXMLTxtFileOut(&cellWordOpenXMLTxtFile);QTextStream xMLTxtFileOut(&xMLTxtFile);QAxObject word("Word.Application");word.setProperty("Visible", false);QAxObject* documents = word.querySubObject("Documents");QAxObject* document = documents->querySubObject("Open(const QString&)", filePath);if (!document) {qDebug() << "Failed to open the document.";return;}QAxObject* tables = document->querySubObject("Tables");int tableCount = tables->dynamicCall("Count").toInt();if (tableCount < 1) {qDebug() << "No table found in the document.";document->dynamicCall("Close()");word.dynamicCall("Quit()");return;}double xOrg = 0;double yOrg = 0;for (int tableIndex = 1; tableIndex <= tableCount; ++tableIndex) {QAxObject* table = tables->querySubObject("Item(int)", tableIndex);qDebug() << "Processing Table Index:" << tableIndex;QAxObject* rangeTable = table->querySubObject("Range");int maxTableCol = rangeTable->dynamicCall("Information(wdMaximumNumberOfColumns)").toInt();int maxTableRow = rangeTable->dynamicCall("Information(wdMaximumNumberOfRows)").toInt();int rowCount = table->querySubObject("Rows")->property("Count").toInt();int columnCount = table->querySubObject("Columns")->property("Count").toInt();for (int row = 1; row <= rowCount; ++row) {for (int col = 1; col <= columnCount; ++col) {QAxObject* cell = table->querySubObject("Cell(int, int)", row, col);if (!cell) {continue;}QAxObject* range = cell->querySubObject("Range");if(!range){continue;}QString WordOpenXML = range->property("WordOpenXML").toString();qDebug()<<WordOpenXML;QFile dell("C:\\Users\\tirklee\\Desktop\\cell("+QString::number(row)+","+QString::number(col)+").xml");if(dell.open(QIODevice::WriteOnly)){QTextStream dellOut(&dell);dellOut<<WordOpenXML<<endl;}dell.close();QString XML = range->property("XML").toString();cellWordOpenXMLList.append(WordOpenXML);xMLList.append(XML);double width = cell->property("Width").toDouble();double height = cell->property("Height").toDouble();int maxCol = range->dynamicCall("Information(wdMaximumNumberOfColumns)").toInt();int maxRow = range->dynamicCall("Information(wdMaximumNumberOfRows)").toInt();int startRow = range->dynamicCall("Information(wdStartOfRangeRowNumber)").toInt();int endRow = range->dynamicCall("Information(wdEndOfRangeRowNumber)").toInt();int startCol = range->dynamicCall("Information(wdStartOfRangeColumnNumber)").toInt();int endCol = range->dynamicCall("Information(wdEndOfRangeColumnNumber)").toInt();double startRowX = range->dynamicCall("Information(wdVerticalPositionRelativeToPage)").toDouble();double endRowX = startRowX+height;double startColX = range->dynamicCall("Information(wdHorizontalPositionRelativeToPage)").toInt();double endColX = startColX+width;if(col==1){yOrg = startColX;}if(row==1){xOrg = startRowX;}double top = startRowX-yOrg;double bottom = endRowX-yOrg;double left = startColX-xOrg;double rigth = endColX-xOrg;int rowSpan = endRow - startRow + 1;int colSpan = endCol - startCol + 1;
QStringList readLine = QStringList()<< "Cell at (" << QString::number(row) << ", " << QString::number(col) << ") has RowSpan: " << QString::number(rowSpan) << " and ColumnSpan: " << QString::number(colSpan);resTxtList.append(readLine.join(""));resTxtFileOut<<readLine.join("")<<endl;cellWordOpenXMLTxtFileOut<<WordOpenXML<<endl;xMLTxtFileOut<<XML<<endl;qDebug() << readLine;delete cell;}}delete table;}resTxtFile.close();cellWordOpenXMLTxtFile.close();xMLTxtFile.close();document->dynamicCall("Close()");word.dynamicCall("Quit()");
}void MainWindow::displayExcelTableInQTableWidget1(const QString &filePath)
{QAxObject* excel = new QAxObject("Excel.Application");excel->dynamicCall("SetVisible(bool Visible)", false);QAxObject* workbooks = excel->querySubObject("Workbooks");QAxObject* workbook = workbooks->querySubObject("Open(const QString&)", filePath);QAxObject* sheets = workbook->querySubObject("Worksheets");QAxObject* sheet = sheets->querySubObject("Item(int)", 1); QAxObject* usedRange = sheet->querySubObject("UsedRange");if (!usedRange) {qDebug() << "无法获取已使用的范围。";return;}QAxObject* rows = usedRange->querySubObject("Rows");int rowsCount = rows->property("Count").toInt();QAxObject* cols = usedRange->querySubObject("Columns");int colsCount = cols->property("Count").toInt();QAxObject* cells = usedRange->querySubObject("Cells");ui->tableWidget->setRowCount(rowsCount);ui->tableWidget->setColumnCount(colsCount);for (int row = 1; row <= rowsCount; ++row) {for (int col = 1; col <= colsCount; ++col) {QAxObject* cell = cells->querySubObject("Item(int, int)", row, col);bool isMerged = cell->property("MergeCells").toBool();int rowSpan = 1;int colSpan = 1;if (isMerged) {QAxObject* mergeArea = cell->querySubObject("MergeArea");QAxObject* rowsRange = mergeArea->querySubObject("Rows");QAxObject* colsRange = mergeArea->querySubObject("Columns");rowSpan = rowsRange->property("Count").toInt();colSpan = colsRange->property("Count").toInt();qDebug() << "单元格 (" << row << ", " << col << ") 合并了 " << rowSpan << " 行和 " << colSpan << " 列。";}QString cellText = cell->property("Text").toString().trimmed();ui->tableWidget->setItem(row-1,col-1,new QTableWidgetItem(cellText));ui->tableWidget->setSpan(row-1,col-1,rowSpan,colSpan);delete cell;}}delete usedRange;delete rows;delete cols;delete cells;workbook->dynamicCall("Close()");excel->dynamicCall("Quit()");delete excel;
}CellInfo::CellInfo(QObject *parent)
{Q_UNUSED(parent);
}CellInfo::~CellInfo()
{}