在label.pro中添加axcontainer:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void openfile();
void okbtn();
private:
Ui::MainWindow *ui;
QString filepath;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qaxobject.h>
#include "qt_windows.h"
#include "qfiledialog.h"
#include "qdebug.h"
#include "qdatetime.h"
#include "qmessagebox.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->openbtn,SIGNAL(clicked()),this,SLOT(openfile()));
connect(ui->ok,SIGNAL(clicked()),this,SLOT(okbtn()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openfile()
{
filepath = QFileDialog::getOpenFileName(this,tr("打开源文件"),"./",tr("excel files(*.xls *.xlsx)"));
ui->openlabel->setText(filepath);
}
void MainWindow::okbtn()
{
if(filepath.length() == 0)
{
QMessageBox Msg(QMessageBox::Critical, tr("错误"), tr("请先打开源文件!"));
Msg.exec();
}
int runrow[30] = {
0};
int columnrow[4] = {
0};
HRESULT r = OleInitialize(0);
if(r != S_OK && r != S_FALSE)
{
qDebug("Qt:Could not initialize OLE(error %x)",(unsigned int)r);
}
// step1:连接控件
QAxObject* excel = new QAxObject( "Excel.Application"); // 连接Excel控件
excel->dynamicCall("SetVisible (bool Visible)", "false"); // 不显示窗体
excel->setProperty("DisplayAlerts", false); // 不显示任何警告信息。如果为true, 那么关闭时会出现类似"文件已修改,是否保存"的提示
// step2: 打开工作簿
QAxObject* workbooks = excel->querySubObject( "Workbooks" );// 获取工作簿集合<