QAxObject实现读写excel

源代码如下:

#include "widget.h"

#include "ui_widget.h"

#include "QToolBox"

#include "QTabWidget"

#include "QPushButton"

#include "QDebug"

#include "QDir"

#include

#include

#include

#include

#include

Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget)

{

ui->setupUi(this);

this->resize(800,500);

}

Widget::~Widget()

{

delete ui;

}

void Widget::on_openFile_clicked()

{

QAxObject *excel = nullptr;

QAxObject *workbooks = nullptr;

QAxObject *workbook = nullptr;

QAxObject *worksheet = nullptr;

QAxObject *usedRange = nullptr;

复制代码
try {
    // 1. 启动Excel COM对象
    excel = new QAxObject("Excel.Application", this);
    if (excel->isNull()) {
        qDebug() << "启动失败:请确认电脑已安装 Office 或 WPS";
        return;
    }
    excel->setProperty("Visible", false);       // 后台运行,不弹窗口
    excel->setProperty("DisplayAlerts", false); // 关闭所有提示弹窗

    // 2. 打开指定的Excel文件(注意转Windows原生路径格式)
    workbooks = excel->querySubObject("Workbooks");
    // 在构造函数或按钮槽函数里调用,传入你的Excel文件绝对路径
    QString dir = QDir::currentPath();
    QString filter = "*.xlsx";
    QString filePath = QFileDialog::getOpenFileName(this,"file",dir,filter);
    workbook  = workbooks->querySubObject("Open(const QString&)",
                                         QDir::toNativeSeparators(filePath));
    if (!workbook || workbook->isNull()) {
        qDebug() << "文件打开失败,请检查路径:" << filePath;
        return;
    }

    // 3. 获取第1个工作表
    worksheet = workbook->querySubObject("Worksheets(int)", 1);

    // 4. 获取表格已使用的区域,得到总行数、总列数
    usedRange = worksheet->querySubObject("UsedRange");
    if (!usedRange || usedRange->isNull()) {
        qDebug() << "表格为空,无内容可读取";
        return;
    }
    int rowCount = usedRange->querySubObject("Rows")->property("Count").toInt();
    int colCount = usedRange->querySubObject("Columns")->property("Count").toInt();
    qDebug() << QString("=== 表格共 %1 行,%2 列 ===").arg(rowCount).arg(colCount);

    // 先清空表格原有内容
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(3); // 设置总列数

    for (int rowExcel = 1; rowExcel <= rowCount; rowExcel++)
    {
        // QTableWidget 行下标 = Excel行 - 1
        int tableRow = rowExcel - 1;
        // 给表格新增一行
        ui->tableWidget->insertRow(tableRow);

        QString line;
        for (int colExcel = 1; colExcel <= colCount; colExcel++)
        {
            int tableCol = colExcel - 1;

            // 读取Excel单元格文本
            QAxObject *cell = worksheet->querySubObject("Cells(int,int)", rowExcel, colExcel);
            QString cellText = cell->property("Value").toString();
            delete cell;

            // 新建表格单元格条目,填入文本
            QTableWidgetItem *item = new QTableWidgetItem(cellText);
            ui->tableWidget->setItem(tableRow, tableCol, item);

            line += cellText + "\t";
        }
        qDebug() << QString("第%1行: ").arg(rowExcel) << line;
    }
}
catch (...) {
    qDebug() << "读取Excel发生异常";
}

// ========== 必须倒序释放所有COM对象,否则Excel进程会后台残留 ==========
if (usedRange) delete usedRange;
if (worksheet) delete worksheet;
if (workbook) {
    workbook->dynamicCall("Close()"); // 关闭文件,不保存修改
    delete workbook;
}
if (workbooks) delete workbooks;
if (excel) {
    excel->dynamicCall("Quit()");
    delete excel;
}

}

void Widget::on_saveFile_clicked()

{

}

2)创建两个按钮和一个tableWidget部件;

3)测试如下:

相关推荐
迷茫不知归路2 小时前
简单认识一种外设硬件:温度传感器ds18b20
嵌入式硬件
z20348315202 小时前
CLion开发STM32——文件管理
stm32·单片机·嵌入式硬件·clion
weixin_452077642 小时前
STM32 烧录程序后上电不工作问题集合
stm32·单片机·嵌入式硬件
沸速存储2 小时前
内存技术的未来:DDR6、CAMM2与CXL将如何改变计算架构
科技·嵌入式硬件·架构·计算机外设·电脑
LCG元3 小时前
STM32多传感器数据采集系统设计:I2C+ADC+UART+低功耗工程实战
stm32·单片机·嵌入式硬件
国科安芯3 小时前
MCU芯片AS32X601如何使用 DMA 单次模式搬运 ADC 多通道采样数据?
单片机·嵌入式硬件
俊基科技4 小时前
1厘米的声学突围:A-29P如何在回音与噪声的双重围剿中“猎杀“纯净人声
嵌入式硬件·嵌入式开发·硬件开发·ai降噪·回声消除·语音模组·远扬拾音
E_ICEBLUE4 小时前
Python 拆分 Excel 文件:使用 Spire.XLS 实现按工作表、行、列和条件拆分
python·excel
蓝创工坊Blue Foundry5 小时前
批量提取图片中的数字:怎样整理成一张可核对的 Excel
python·ai·ocr·excel·paddlepaddle