• 大小: 1.79MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-07
  • 语言: 其他
  • 标签: C++  Qt  Excel  

资源简介

采用Qt中QAxObject和QAxWidget实现Excel表格嵌入进Qt程序中,实现Excel的读写操作

资源截图

代码片段和文件信息

#include “ExcelOper.h“
#include ject>
#include 
#include 

ExcelOper::ExcelOper(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_axWidget = nullptr;

connect(ui.createBtn SIGNAL(clicked()) this SLOT(onCreateBtn()));
connect(ui.writeBtn SIGNAL(clicked()) this SLOT(onWriteBtn()));
connect(ui.readBtn SIGNAL(clicked()) this SLOT(onReadBtn()));
connect(ui.clearBtn SIGNAL(clicked()) this SLOT(onClear()));
}

ExcelOper::~ExcelOper()
{
this->close();
}

void ExcelOper::onCreateBtn()
{
this->close();
if (m_axWidget == nullptr)
{
m_axWidget = new QAxWidget(this);
}
//m_axWidget->setControl(“{00020906-0000-0000-C000-000000000046}“);//word
m_axWidget->setControl(“{00020832-0000-0000-C000-000000000046}“);//Excel



m_axWidget->setProperty(“DisplayAlerts“ true);
m_axWidget->setProperty(“Visible“ true);

ui.vLayout->addWidget(m_axWidget);

//需要改变窗口大小,否则Excel不能加载出来
QSize oldSize = this->size();
this->resize(oldSize.width() + 1 oldSize.height() + 1);
QApplication::processEvents();
this->resize(oldSize);
}

void ExcelOper::onWriteBtn()
{
QAxobject * worksheets = m_axWidget->querySubobject(“WorkSheets“);
// 获取第n个工作表
QAxobject * sheet = worksheets->querySubobject(“Item(int)“ 1);
const int row = 200000;
const int column = 5;
QVector> cells(row);
for (int i = 0; i < row; i++)
{
cells[i].resize(column);
for (int j = 0; j < column; j++)
{
//cells[i][j] = QString(“%1_%2“).arg(i).arg(j);
cells[i][j] = i + j;
}
}
this->writeSheet(sheet cells);

m_axWidget->setFocus();//若不设置焦点,Excel不能更新
}

void ExcelOper::onReadBtn()
{
QAxobject * worksheets = m_axWidget->querySubobject(“WorkSheets“);
//int num = worksheets->property(“Count“).toInt();
QAxobject * sheet = worksheets->querySubobject(“Item(int)“ 1);// 获取第n个工作表

qDebug() << “-----Read--start-----“;
QVariant all = this->readSheet(sheet);
qDebug() << “-----Read--convert-----“;
QVector> res;
fromVariant(all res);
qDebug() << “-----Read--finsh-----“;

m_axWidget->setFocus();
}

void ExcelOper::onClear()
{
QAxobject * worksheets = m_axWidget->querySubobject(“WorkSheets“);
QAxobject * sheet = worksheets->querySubobject(“Item(int)“ 1);
QAxobject *usedRange = sheet->querySubobject(“UsedRange“);
if (NULL != usedRange && !usedRange->isNull())
{
usedRange->dynamicCall(“Clear()“);
}

m_axWidget->setFocus();
}

QVariant ExcelOper::readSheet(QAxobject* sheet)
{
QVariant var;
if (sheet != NULL && !sheet->isNull())
{
QAxobject *usedRange = sheet->querySubobject(“UsedRange“);
if (NULL == usedRange || usedRange->isNull())
{
return var;
}
var = usedRange->dynamicCall(“Value“);
delete usedRange;
}
return var;
}

// 把QVariant转为QVector>
void ExcelOper::fromVariant(const QVariant &var QVector> &res)
{
QVariantList varRows = var

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-09-17 14:05  Qt中嵌入Excel\
     目录           0  2019-09-17 14:06  Qt中嵌入Excel\ExcelOper\
     目录           0  2019-09-17 14:05  Qt中嵌入Excel\ExcelOper\Debug\
     文件         200  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.log
     文件      329496  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.obj
     目录           0  2019-09-17 14:05  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\
     文件        5534  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\CL.command.1.tlog
     文件      100954  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\CL.read.1.tlog
     文件        2196  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\CL.write.1.tlog
     文件         184  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\ExcelOper.lastbuildstate
     文件       57198  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\ExcelOper.write.1u.tlog
     文件        1746  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\link.command.1.tlog
     文件        4024  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\link.read.1.tlog
     文件         666  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\link.write.1.tlog
     文件          86  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\moc.read.1u.tlog
     文件         226  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\moc.write.1u.tlog
     文件          90  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\rcc.read.1u.tlog
     文件         218  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\rcc.write.1u.tlog
     文件          88  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\uic.read.1u.tlog
     文件         210  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\ExcelOper.tlog\uic.write.1u.tlog
     文件       99793  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\main.obj
     文件      103462  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\moc_ExcelOper.obj
     文件        5509  2019-09-09 14:24  Qt中嵌入Excel\ExcelOper\Debug\qrc_ExcelOper.obj
     文件        1458  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\qt_work.log
     文件     1748992  2019-09-12 13:10  Qt中嵌入Excel\ExcelOper\Debug\vc140.pdb
     文件        4973  2019-09-12 11:26  Qt中嵌入Excel\ExcelOper\ExcelOper.cpp
     文件         747  2019-09-12 11:19  Qt中嵌入Excel\ExcelOper\ExcelOper.h
     文件          69  2019-09-09 14:05  Qt中嵌入Excel\ExcelOper\ExcelOper.qrc
     文件        1716  2019-09-12 11:20  Qt中嵌入Excel\ExcelOper\ExcelOper.ui
     文件        8460  2019-09-09 14:24  Qt中嵌入Excel\ExcelOper\ExcelOper.vcxproj
     目录           0  2019-09-17 14:05  Qt中嵌入Excel\ExcelOper\GeneratedFiles\
............此处省略12个文件信息

评论

共有 条评论