資源簡介
使用Qt實現一個Excel讀取工具,顯示到界面。
代碼片段和文件信息
#include?“ExcelReadTool.h“
#include?
#include?
CExcelReadTool::CExcelReadTool(QWidget?*parent)
:?QMainWindow(parent)
?excel(nullptr)
?work_books(nullptr)
?work_book(nullptr)
?work_sheets(nullptr)
?work_sheet(nullptr)
{
ui.setupUi(this);
menuWidget()->hide();
//setFixedSize(size());
ui.tableWidget->horizontalHeader()->setSectionresizeMode(QHeaderView::Stretch);
ui.listWidget_time->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_address->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_vol->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_type->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_no->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_tool->setSelectionMode(QAbstractItemView::MultiSelection);
ui.listWidget_person->setSelectionMode(QAbstractItemView::MultiSelection);
connect(ui.pushButton?&QPushButton::clicked?this?&CExcelReadTool::OnPushButtonclicked);
connect(ui.pushButton_select?&QPushButton::clicked?this?&CExcelReadTool::OnFilterPushButtonclicked);
connect(ui.pushButton_clear?&QPushButton::clicked?this?&CExcelReadTool::OnClearButtonclicked);
}
CExcelReadTool::~CExcelReadTool()
{
if?(work_book?!=?nullptr)
{
work_book->dynamicCall(“Close()“);//關閉工作簿
}
if?(excel?!=?nullptr)
{
excel->dynamicCall(“Quit()“);//關閉excel
delete?excel;
excel?=?NULL;
}
}
void?CExcelReadTool::OnPushButtonclicked()
{
static?bool?bfirst?=?false;
if?(bfirst)
{
return;
}
QString?file_path?=?QFileDialog::getOpenFileName(this?QString()?QString()?“excel(*.xls?*.xlsx)“);
qDebug()?<“file?path:“?< if?(!file_path.isEmpty())
{
bfirst?=?true;
OpenExcel(file_path);
}
}
void?CExcelReadTool::OpenExcel(QString?filePath)
{
excel?=?new?QAxobject(this);
excel->setControl(“Excel.Application“);//連接Excel控件
excel->setProperty(“Visible“?false);
// excel->dynamicCall(“SetVisible?(bool?Visible)“?“false“);//不顯示窗體
excel->setProperty(“DisplayAlerts“?false);//不顯示任何警告信息。如果為true那么在關閉是會出現類似“文件已修改,是否保存”的提示
work_books?=?excel->querySubobject(“WorkBooks“);
work_books->dynamicCall(“Open(const?QString&)“?filePath);
work_book?=?excel->querySubobject(“ActiveWorkBook“);
work_sheets?=?work_book->querySubobject(“Sheets“);??//Sheets也可換用WorkSheets
int?sheet_count?=?work_sheets->property(“Count“).toInt();??//獲取工作表數目
if?(sheet_count?>?0)
{
work_sheet?=?work_book->querySubobject(“Sheets(int)“?1);
QVariant?var?=?readAll(work_sheet);
Variant2ListListVariant(var);
}
}
QVariant?CExcelReadTool::readAll(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;
}
re
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9439??2019-09-04?22:58??QtReadExcelTool\ExcelReadTool.cpp
?????文件????????870??2019-09-04?22:37??QtReadExcelTool\ExcelReadTool.h
?????文件???????6909??2020-10-23?00:27??QtReadExcelTool\ExcelReadTool.ui
?????文件????????186??2019-09-04?02:11??QtReadExcelTool\main.cpp
?????目錄??????????0??2020-10-23?00:26??QtReadExcelTool
-----------?---------??----------?-----??----
????????????????17404????????????????????5
- 上一篇:Tcp自定義命令調試工具
- 下一篇:c++ 虛擬攝像頭
評論
共有 條評論