资源简介
实现了tableview连接数据库后导出为excel文件和excel文件的导入
代码片段和文件信息
#include “excelengine.h“
#include
#include
#include
#include
#include
ExcelEngine::ExcelEngine(Qobject *parent) : Qobject(parent)
{
pExcel = NULL;
pWorkbooks = NULL;
pWorkbook = NULL;
pWorksheet = NULL;
sXlsFile = ““;
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
bIsOpen = false;
bIsValid = false;
bIsANewFile = false;
bIsSaveAlready = false;
// HRESULT r = OleInitialize(0);
// if (r != S_OK && r != S_FALSE)
// {
// qDebug(“Qt: Could not initialize OLE (error %x)“ (unsigned int)r);
// }
}
ExcelEngine::ExcelEngine(QString xlsFile)
{
pExcel = NULL;
pWorkbooks = NULL;
pWorkbook = NULL;
pWorksheet = NULL;
sXlsFile = xlsFile;
nRowCount = 0;
nColumnCount = 0;
nStartRow = 0;
nStartColumn = 0;
bIsOpen = false;
bIsValid = false;
bIsANewFile = false;
bIsSaveAlready = false;
// HRESULT r = OleInitialize(0);
// if (r != S_OK && r != S_FALSE)
// {
// qDebug(“Qt: Could not initialize OLE (error %x)“ (unsigned int)r);
// }
}
ExcelEngine::~ExcelEngine()
{
if ( bIsOpen )
{
//析构前,先保存数据,然后关闭workbook
Close();
}
//OleUninitialize();
}
/**
*@brief 打开sXlsFile指定的excel报表
*@return true : 打开成功
* false: 打开失败
*/
bool ExcelEngine::Open(UINT nSheet bool visible)
{
if(bIsOpen)
{
Close();
}
nCurrSheet = nSheet;
bIsVisible = visible;
if(NULL == pExcel)
{
pExcel = new QAxobject(“Excel.Application“);
if(pExcel)
{
bIsValid = true;
}
else
{
bIsValid = false;
bIsOpen = false;
return bIsOpen;
}
pExcel->dynamicCall(“SetVisible(bool)“ bIsVisible);
}
if(!bIsValid)
{
bIsOpen = false;
return bIsOpen;
}
if(sXlsFile.isEmpty())
{
bIsOpen = false;
return bIsOpen;
}
/*如果指向的文件不存在,则需要新建一个*/
QFile f(sXlsFile);
if(!f.exists())
{
bIsANewFile = true;
}
else
{
bIsANewFile = false;
}
if(!bIsANewFile)
{
pWorkbooks = pExcel->querySubobject(“WorkBooks“); //获取工作簿
pWorkbook = pWorkbooks->querySubobject(“Open(QString QVariant)“sXlsFileQVariant(0)); //打开xls对应的工作簿
}
else
{
pWorkbooks = pExcel->querySubobject(“WorkBooks“); //获取工作簿
pWorkbooks->dynamicCall(“Add“); //添加一个新的工作薄
pWorkbook = pExcel->querySubobject(“ActiveWorkBook“); //新建一个xls
}
pWorksheet = pWorkbook->querySubobject(“WorkSheets(int)“ nCurrSheet);//打开第一个sheet
//至此已打开
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 18360 2019-10-31 16:12 导入导出类\excelengine.cpp
文件 2388 2019-10-15 00:23 导入导出类\excelengine.h
- 上一篇:Spark.txt
- 下一篇:手机物流APP的设计与实现
评论
共有 条评论