需求
想把QTableWidget/QTableView数据导出为Excel表格数据,使用QXlsx.
参考文章:详解 Qt 中的 QXlsx-优快云博客
QT代码
bool tableToExcelData(QTableWidget* tableWidget, const QString& toFilePath)
{
if (toFilePath.isEmpty())
return false;
QXlsx::Document* doc = new QXlsx::Document(toFilePath);
doc->addSheet(QStringLiteral("sheet1"));
QColor borderColor = QColor(Qt::gray);
int rowNum = 1;
//设置列表头
QXlsx::Format titleStyle;
titleStyle.setFontSize(10);
titleStyle.setFontBold(true);
titleStyle.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
titleStyle.setVerticalAlignment(QXlsx::Format::AlignVCenter);
titleStyle.setFillPattern(QXlsx::Format::PatternSolid);
titleStyle.setPatternBackgroundColor(Qt::yellow);
titleStyle.setBorderStyle(QXlsx::Format::BorderThin);
titleStyle.setBorderColor(borderColor);
doc->setRowHeight(rowNum, 20);
for (int i = 0; i < tableWidget->columnCount(); ++i)
{
QString headerText = tableWidget->horizontalHeaderItem(i)->text();;
doc->setColumnWidth(i + 1, 30);
doc->write(rowNum, i + 1, headerText, titleStyle);
}
rowNum++;
//数据
for (int row = 0; row < tableWidget->rowCount(); ++row)
{
//设置数据
QXlsx::Format dataRowStyle;
dataRowStyle.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
dataRowStyle.setVerticalAlignment(QXlsx::Format::AlignTop);
dataRowStyle.setBorderStyle(QXlsx::Format::BorderThin);
dataRowStyle.setBorderColor(borderColor);
doc->setRowHeight(rowNum, 20);
for (int col = 0; col < tableWidget->columnCount(); ++col)
{
QString value = QString(tableWidget->item(row, col)->text() + "\t");
doc->write(rowNum, col + 1, value, dataRowStyle);
}
rowNum++;
}
doc->save();
return true;
}
bool tableViewToExcelData(QTableView* tableView, const QString& toFilePath)
{
if (toFilePath.isEmpty())
return false;
QXlsx::Document* doc = new QXlsx::Document(toFilePath);
doc->addSheet(QStringLiteral("sheet1"));
QColor borderColor = QColor(Qt::gray);
int rowNum = 1;
//设置列表头
QXlsx::Format titleStyle;
titleStyle.setFontSize(10);
titleStyle.setFontBold(true);
titleStyle.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
titleStyle.setVerticalAlignment(QXlsx::Format::AlignVCenter);
titleStyle.setFillPattern(QXlsx::Format::PatternSolid);
titleStyle.setPatternBackgroundColor(Qt::yellow);
titleStyle.setBorderStyle(QXlsx::Format::BorderThin);
titleStyle.setBorderColor(borderColor);
doc->setRowHeight(rowNum, 20);
QAbstractItemModel* model = tableView->model();
if (!model)
return false;
for (int i = 0; i < model->columnCount(); ++i)
{
QString headerText = model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();;
doc->setColumnWidth(i + 1, 30);
doc->write(rowNum, i + 1, headerText, titleStyle);
}
rowNum++;
//数据
for (int row = 0; row < model->rowCount(); ++row)
{
//设置数据
QXlsx::Format dataRowStyle;
dataRowStyle.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
dataRowStyle.setVerticalAlignment(QXlsx::Format::AlignTop);
dataRowStyle.setBorderStyle(QXlsx::Format::BorderThin);
dataRowStyle.setBorderColor(borderColor);
doc->setRowHeight(rowNum, 20);
for (int col = 0; col < model->columnCount(); ++col)
{
QModelIndex index = model->index(row, col);
QString value = model->data(index).toString();
doc->write(rowNum, col + 1, value, dataRowStyle);
}
rowNum++;
}
doc->save();
return true;
}
设置了导出表格头的样式。