在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" );// 获取工作簿集合
//新建
workbooks->dynamicCall("Add"); // 新建一个工作簿
QAxObject* dworkbook = excel->querySubObject("ActiveWorkBook"); // 获取当前工作簿
//打开现成的工作簿
QAxObject* sworkbook = workbooks->querySubObject( "Open(const QString&)", QDir::toNativeSeparators(filepath));
//QAxObject* dworkbook = workbooks->querySubObject( "Open(const QString&)", "E:\\qwer.xlsx");
// step3: 打开sheet工作表
QAxObject* ssheets = sworkbook->querySubObject( "Worksheets" );
QAxObject* dsheets = dworkbook->querySubObject( "Worksheets" );
// step4: 统计sheet工作表个数
int scount = ssheets->dynamicCall("Count()").toInt();
for (int i=2; i<=scount; i++) //循环遍历sheet工作表
{
//获取当前工作表
QAxObject* ssheet = ssheets->querySubObject( "Item( int )", i );
QAxObject* dsheet = dsheets->querySubObject( "Item( int )", i-1 );
//获取行数,列数
QAxObject* usedrange = ssheet->querySubObject("UsedRange"); // sheet范围
int intRowStart = usedrange->property("Row").toInt(); // 起始行数
int intColStart = usedrange->property("Column").toInt(); // 起始列数
QAxObject *rows = usedrange->querySubObject("Rows"); // 行
int rowCount = rows->property("Count").toInt(); // 行数
QAxObject *columns = usedrange->querySubObject("Columns"); // 列
int columnCount = columns->property("Count").toInt(); // 列数
//得到行区间
int j = 0;
QAxObject* scell;
for(int row = intRowStart;row<=rowCount;row++)
{
scell = ssheet->querySubObject("Cells(int, int)", row, intColStart);
QString str = scell->dynamicCall("Value2()").toString();
if(strcmp(str.toStdString().c_str(),"序号") == 0)
{
if(i==2)
runrow[j] = row + 1;
if(i==3)
runrow[j] = row + 2;
j++;
}
if(str == QObject::tr("制表"))
{
runrow[j] = row - 1;
j++;
}
}
//得到列区间
for(int column = intColStart;column<=columnCount;column++)
{
if(i==2)
scell = ssheet->querySubObject("Cells(int, int)", runrow[0]-1, column);
if(i==3)
scell = ssheet->querySubObject("Cells(int, int)", runrow[0]-2, column);
QString str = scell->dynamicCall("Value2()").toString();
if(str == "K3物料编码")
columnrow[0] = column;
if(str == "名称")
columnrow[1] = column;
if(str == "型号规格" || str == "加工\n类型")
columnrow[2] = column;
if(str == "数\n量" || str == "数量")
columnrow[3] = column;
}
//写入
QAxObject* cell1;
QString strstr[4];
int writerow = 1;
for(int jj = 0 ; jj<j ; jj+=2)
{
if(runrow[jj] == 0 && runrow[jj + 1] == 0)
break;
for(int row = runrow[jj]; row <= runrow[jj+1]; row++)
{
scell = ssheet->querySubObject("Cells(int, int)", row, columnrow[0]); //获单元格值
strstr[0] = scell->dynamicCall("Value2()").toString();
scell = ssheet->querySubObject("Cells(int, int)", row, columnrow[1]); //获单元格值
strstr[1] = scell->dynamicCall("Value2()").toString();
scell = ssheet->querySubObject("Cells(int, int)", row, columnrow[2]); //获单元格值
strstr[2] = scell->dynamicCall("Value2()").toString();
scell = ssheet->querySubObject("Cells(int, int)", row, columnrow[3]); //获单元格值
strstr[3] = scell->dynamicCall("Value2()").toString();
if(strstr[0].length()==0 && strstr[1].length()==0 && strstr[2].length()==0 && strstr[3].length()==0)
continue;
if(strstr[0] == "以下空白" || strstr[1] == "以下空白" || strstr[2] == "以下空白" || strstr[3] == "以下空白")
break;
if(strstr[0].length()==0 && strstr[1].length()==0 && strstr[2].length()==0)
continue;
cell1 = dsheet->querySubObject("Cells(int, int)", writerow,1); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant("物料编码")); // 设置单元格的值
cell1->setProperty("ColumnWidth", 9); //设置单元格列宽
cell1->setProperty("HorizontalAlignment", -4108); //左对齐(xlLeft):-4131 居中(xlCenter):-4108 右对齐(xlRight):-4152
cell1->setProperty("VerticalAlignment", -4108); //上对齐(xlTop)-4160 居中(xlCenter):-4108 下对齐(xlBottom):-4107
cell1 = dsheet->querySubObject("Cells(int, int)", writerow,2); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant(strstr[0])); // 设置单元格的值
cell1->setProperty("ColumnWidth", 17);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+1,1); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant("物料名称")); // 设置单元格的值
cell1->setProperty("ColumnWidth", 9);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+1,2); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant(strstr[1])); // 设置单元格的值
cell1->setProperty("ColumnWidth", 17);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+2,1); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant("物料规格")); // 设置单元格的值
cell1->setProperty("ColumnWidth", 9);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+2,2); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant(strstr[2])); // 设置单元格的值
cell1->setProperty("ColumnWidth", 17);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+3,1); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant("数量")); // 设置单元格的值
cell1->setProperty("ColumnWidth", 9);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
cell1 = dsheet->querySubObject("Cells(int, int)", writerow+3,2); //获单元格值
cell1->dynamicCall("SetValue(conts QVariant&)", QVariant(strstr[3])); // 设置单元格的值
cell1->setProperty("ColumnWidth", 17);
cell1->setProperty("HorizontalAlignment", -4108);
cell1->setProperty("VerticalAlignment", -4108);
writerow += 5;
}
}
}
QString filenametime;
QFileInfo fileinfo = QFileInfo(filepath);
QString file_path = fileinfo.absolutePath();//不带文件名的绝对路径
filenametime = file_path;
filenametime += "/";
QDateTime curDateTime=QDateTime::currentDateTime();
filenametime += curDateTime.toString("yyMMddhhmmz");
filenametime += ".xlsx";
dworkbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(filenametime));
dworkbook->dynamicCall("Close()");
sworkbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
OleUninitialize();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui