QtExcel / QXlsx

Excel file(*.xlsx) reader/writer library using Qt 5 or 6. Descendant of QtXlsxWriter.
https://qtexcel.github.io/QXlsx/
MIT License
1.15k stars 355 forks source link

Reading EXECL document, the number of lines and columns are incorrect #207

Open xiaochong-xiaogan opened 2 years ago

xiaochong-xiaogan commented 2 years ago

The document pointer is obtained, but there is only one row and the number of columns is 0,In fact, the EXECL file has 238 lines and 34 columns

EllaXue2020 commented 2 years ago

i have same issue

adengzx commented 2 years ago

same

j2doll commented 1 year ago

Dear @xiaochong-xiaogan

Can you show me some example code?

wuxianggujun commented 2 weeks ago

Dear  亲爱的@xiaochong-xiaogan

Can you show me some example code?你能给我看一些示例代码吗?

Mine will obviously have more than 20 columns, but will only read 1 column

wuxianggujun commented 2 weeks ago

Dear @xiaochong-xiaogan

Can you show me some example code? I tested it and found that this code cannot correctly read the number of file columns


void XlsxTableView::loadXlsx(const QString &fileName) {
if (xlsxDocument) {
int ret = QMessageBox::question(this, tr("打开新文件"), tr("当前已打开文件,是否关闭当前文件并打开新文件?"),
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes) {
clearTabs();
delete xlsxDocument;
//xlsxDocument->deleteLater();
xlsxDocument = nullptr;
} else {
return;
}
}
xlsxDocument = new QXlsx::Document(fileName);

if (!xlsxDocument->isLoadPackage()) {
    QMessageBox::warning(this, tr("打开文件失败"), tr("打开的Excel文件不是有效的XLSX文件!"));
    delete xlsxDocument;
    xlsxDocument = nullptr;
    return;
}
auto sheetNames = xlsxDocument->sheetNames();
qsizetype sheetCount = sheetNames.size();
for (int i = 0; i < sheetCount; i++) {
    const QString &sheetName = sheetNames.at(i);

    // 激活当前工作表
    if (!xlsxDocument->selectSheet(sheetName)) {
        QMessageBox::warning(this, tr("错误"), tr("无法选择工作表 %1").arg(sheetName));
        continue;
    }

    auto *tableView = new QTableView(this);
    const QXlsx::CellRange cellRange = xlsxDocument->dimension();
    int rowCount = cellRange.rowCount();
    int columnCount = cellRange.columnCount();

    // 打印行数和列数,调试用
    qDebug() << "Sheet:" << sheetName << " Rows:" << rowCount << " Columns:" << columnCount;

    auto *model = new QStandardItemModel(rowCount, columnCount, this);

    // 填充 model 数据
    for (int row = 1; row <= rowCount; ++row) {
        for (int col = 1; col <= columnCount; ++col) {
            if (const std::shared_ptr<QXlsx::Cell> cell = xlsxDocument->cellAt(row, col)) {
                auto *item = new QStandardItem(cell->value().toString());
                // 设置单元格格式
                const QVariant format = QVariant::fromValue(cell->format());
                item->setData(format);
                model->setItem(row - 1, col - 1, item);
            }
        }
    }
    tableView->setModel(model);
    tabWidget->addTab(tableView, sheetName);
}

}


I had originally written this reading code, but I found this bug while testing multiple documents. This is just a snippet of part of the file, not the whole thing, because it's a log file from my work. Sorry, it's not convenient to show it.

<?xml version="1.0" encoding="UTF-8"?>

告警记录 -- 历史告警 保存时间:2024-10-17 15:18:59 用户名:gxb574883 维护状态 属于 普通。 发生时间 = 3天。 级别告警ID名称网元类型告警源MO对象定位信息发生时间 (NT)清除时间 (NT)确认时间 (ST)清除用户确认用户清除状态RRU名称RF站点名称CELL名称确认状态BBU名称eNodeB IDgNodeB ID日志流水号用户自定义标识设备告警流水号附加信息维护状态所属子网 ```