一、源码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;private:void readFile();bool isXlsxFile(const QString &filePath);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QFileInfo>
#include <QDebug>
#include <QAxObject>
#include <QVariant>
#include <QMap>
#include <QStandardItemModel>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);readFile();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::readFile()
{connect(ui->pushButton, &QPushButton::clicked, this, [=](){QString currentpath = QDir::homePath();QString openfiletitle = "请选择文件";QString filefilter = "all(*.*)";QString filepath = QFileDialog::getOpenFileName(this, openfiletitle, currentpath, filefilter);if(filepath.isEmpty()){QMessageBox::warning(this, "警告!!", "文件夹不能为空!");return;}QFileInfo info(filepath);//获取文件信息ui->lineEdit->setText(info.fileName());//excel表格读取QAxObject *excel = new QAxObject(this);excel->dynamicCall("Visible(bool)", false);if (isXlsxFile(filepath)){excel->setControl("Excel.Application");// 加载 Microsoft Excel 控件}else{excel->setControl("ket.Application"); // 加载 WPS Excel 控件}excel->setProperty("Visible", false); // 不显示 Excel 窗体QAxObject* workBooks = excel->querySubObject("WorkBooks"); //获取工作簿集合workBooks->dynamicCall("Open(const QString&)", filepath); //打开打开已存在的工作簿QAxObject* workBook = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿QAxObject* sheets = workBook->querySubObject("Sheets"); //获取工作表集合,Sheets也可换用WorkSheetsQAxObject* sheet = workBook->querySubObject("WorkSheets(int)", 1);//获取工作表集合的工作表1,即sheet1//获取该sheet的使用范围对象(一般读取 Excel 时会选取全部范围)QAxObject* usedRange = sheet->querySubObject("UsedRange");/*//获取 sheet 的指定范围(一般写入数据时,会选取指定范围)QAxObject* usedRange = sheet->querySubObject("Range(const QString)", "A1:C12");//获取 sheet 的指定范围(一般修改数据时,会选取指定单元格)QAxObject* usedRange = sheet->querySubObject("Range(QVariant,QVariant)", "A6");*///读取单元格内容QVariant var = usedRange->dynamicCall("Value");delete usedRange;// qDebug() << var;//数据转换QList<QList<QVariant>> xls_data;QVariantList varRows = var.toList();if (varRows.isEmpty()) {return;}const int rowCount = varRows.size();QVariantList rowData;for (int i = 0; i < rowCount; i++){rowData = varRows[i].toList();int clumnCount = rowData.size();if(rowData.count() > clumnCount){clumnCount = rowData.count();}xls_data.push_back(rowData);// qDebug() << rowData;}//写进UI表格// 创建模型QStandardItemModel *model = new QStandardItemModel(rowCount, rowData.size(), this);for (int i = 0; i < xls_data.count(); i++) {for (int j = 0; j < xls_data.at(i).count(); j++) {QStandardItem *item = new QStandardItem(QString(xls_data.at(i).at(j).toByteArray()));model->setItem(i, j, item);
// delete item;}}ui->tableView->setModel(model);// 隐藏行号和列号ui->tableView->verticalHeader()->hide();ui->tableView->horizontalHeader()->hide();});
}bool MainWindow::isXlsxFile(const QString &filePath)
{QFileInfo fileInfo(filePath);QString extension = fileInfo.suffix().toLower();// 列出常见的文本文件扩展名static const QStringList textExtensions = {"txt", "csv", "log", "xml", "json", "xlsx"};return textExtensions.contains(extension);
}
注意,QAxObject使用需要在pro文件添加axcontainer模块。