Open xiaochong-xiaogan opened 2 years ago
i have same issue
same
Dear @xiaochong-xiaogan
Can you show me some example code?
Dear 亲爱的@xiaochong-xiaogan
Can you show me some example code?你能给我看一些示例代码吗?
Mine will obviously have more than 20 columns, but will only read 1 column
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"?>
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