首页 > 其他分享 >Qt自定义插件写Excel表格

Qt自定义插件写Excel表格

时间:2024-03-29 09:55:22浏览次数:15  
标签:插件 file 自定义 CopyAllFileThread Excel QString strTargetPath include strPath

网上找到一个开源的Qt插件,下载下来,生成头文件和库文件,可以不依赖电脑是否安装Excel软件,是否有Excel驱动,可以直接输出excel文档

https://github.com/dbzhang800/QtXlsxWriter/tree/master/src/xlsx

#include "CopyAllFileThread.h"
#include<QFileInfo>
#include "LocalDb.h"
#include <QDir>
#include "xlsxabstractooxmlfile.h"
#include "xlsxabstractsheet.h"
#include "xlsxcell.h"
#include "xlsxcellformula.h"
#include "xlsxcellrange.h"
#include "xlsxcellreference.h"
#include "xlsxchart.h"
#include "xlsxchartsheet.h"
#include "xlsxconditionalformatting.h"
#include "xlsxdatavalidation.h"
#include "xlsxdocument.h"
#include "xlsxformat.h"
#include "xlsxglobal.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include "xlsxworksheet.h"
#include "qtxlsxversion.h"
#include <QDateTime>
#include "ConfigInfo.h"
#pragma execution_character_set("utf-8")
CopyAllFileThread::CopyAllFileThread()
{

}

CopyAllFileThread::~CopyAllFileThread()
{

}

int CopyAllFileThread::StartCopyFile(QMap<QString, QString> selectItemFileTime, QString strTargetPath, QVariantList comparelist, QString strSql,QString table, bool checked)
{
	m_selectItemFileTime = selectItemFileTime;
	m_strTargetPath = strTargetPath;
	m_checkAll = checked;
	m_comparelist = comparelist;
	m_strSql = strSql;
	m_table = table;
	this->start();
	return 0;
}

void CopyAllFileThread::run()
{
	QString strSourcePath = "";
	QString strTargetPath = "";
	QVariantMap datainfo;
	QVariantList listSave;
	if (m_checkAll==true)//排除法
	{
		
		if (m_strSql!="")//查询sql语句获取所有值
		{
			QVariantMap replyData;
			QString strMsg="";
			if (LocalDb::instance()->ExcuateSql(m_strSql, replyData, strMsg)!=0)
			{
				LOG_ERROR("ExcuateSql to Get result failed %s", m_strSql.toStdString().c_str());
				return;
			}
			m_comparelist = replyData.value("data").toList();
		}
		for (int i = 0; i < m_comparelist.size(); i++)
		{
			datainfo = m_comparelist.at(i).toMap();
			strSourcePath = datainfo.value("targetpic").toString();
			if (strSourcePath == "")
			{
				continue;
			}
			if (m_selectItemFileTime.find(strSourcePath) != m_selectItemFileTime.end())
			{
				continue;
			}
			listSave.push_back(datainfo);
			//QFileInfo file(strSourcePath);
			//if (!file.exists())
			//{
			//	continue;
			//}
			//strTargetPath = m_strTargetPath + "/" + file.fileName();
			//QFile::copy(strSourcePath, strTargetPath);//从源路径将文件复制到目标路径
		}

	}
	else//根据查找信息
	{
		QStringList listFiles = m_selectItemFileTime.keys();
		if (listFiles.size()==0)
		{
			return;
		}
		QString strFileCondition = LocalDb::instance()->GetStringCondition(listFiles);
		QString strUrl = QString("select * from %1 where targetpic in (%2)").arg(m_table).arg(strFileCondition);
		QString strMsg = "";
		QVariantMap retValue;
		if (0 != LocalDb::instance()->ExcuateSql(strUrl, retValue, strMsg))
		{
			return;
		}
		listSave = retValue.value("data").toList();
	}
	SaveToExcelFile(listSave);
	
}

int CopyAllFileThread::SaveToExcelFile(QVariantList& lists)
{
	int iRet = -1;
	QString  strBackDir = m_strTargetPath + "/背景大图/";
	QString strTargetDir= m_strTargetPath + "/目标小图/";
	QString strFilePath = m_strTargetPath + "/" + m_table+"_"+QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz")+".xlsx";
	QDir dirBack(strBackDir);
	if (!dirBack.exists())
	{
		if (!dirBack.mkpath(strBackDir))
		{
			LOG_ERROR("make path error :%s", strBackDir.toStdString().c_str());
			return iRet;
		}
	}
	QDir dirTarget(strTargetDir);
	if (!dirTarget.exists())
	{
		if (!dirTarget.mkpath(strTargetDir))
		{
			LOG_ERROR("make path error :%s", strTargetDir.toStdString().c_str());
			return iRet;
		}
	}
	QStringList title;
	QStringList dataname;
	QList<int> columnwidth;
	if (m_table == "human")
	{
		title << "时间" << "来源视频" << "目标小图" << "背景大图" << "性别" << "年龄段" << "戴眼镜" << "戴口罩" << "戴帽子" << "上衣颜色" << "下衣颜色" << "背包" << "拎东西";
		dataname << "time" << "location" << "targetpic" << "relatepic" << "sex" << "agestate" << "glass" << "mask" << "hat" << "upclothecolor" << "downclothecolor" << "bag" << "takething";
		columnwidth << 25 <<    30    <<    30       <<    30      << 10    <<    10    <<    10    <<     10    <<    10   <<     12    <<       12    <<     10 <<     10;
	}
	else if (m_table == "vehicle")
	{
		title << "时间" << "来源视频" << "目标小图" << "背景大图" << "车牌号" << "车身颜色" << "车辆类型" <<"车辆品牌"<< "车牌颜色" <<"驾驶安全带"<< "副驾驶" << "挂件" << "遮阳板" << "危险品";
		dataname << "time" << "location" << "targetpic" << "relatepic" << "vehiclenum"<<"vehiclecolor" << "vehicletype"<<"brand" << "platecolor" << "pilotSafebelt" << "visepilot" << "hangthing" << "hidesunplate" << "danger";
		columnwidth << 25   << 30      <<      30   <<    30    <<      13   <<     12      <<     15     <<       15  <<     12   <<     13    <<     10    <<    10    << 10     <<10;
	}
	else if (m_table == "unvehicle")
	{
		title << "时间" << "来源视频" << "目标小图" << "背景大图" <<"车辆类型"<<"骑行人数"<< "性别" << "年龄段" << "戴眼镜" << "戴口罩" << "戴帽子" << "上衣颜色" << "下衣颜色" << "背包" << "拎东西";
		dataname << "time" << "location" << "targetpic" << "relatepic" << "ridetype"<<"ridenum"<<"sex" << "agestate" << "glass" << "mask" << "hat" << "upclothecolor" << "downclothecolor" << "bag" << "takething";
		columnwidth << 25   << 30      <<      30   <<    30    <<      12   <<     12      <<   10 <<    10  <<     10   <<     10    <<     10    <<    12      <<     12     <<   10      <<  10 ;
	}
	else if (m_table == "GaitAnaResult")
	{
		title << "时间" << "来源视频" << "目标小图" << "背景大图" ;
		dataname << "time" << "location" << "targetpic" << "relatepic";
		columnwidth << 25   << 30      <<      30   <<    30    ;
	}
	else
	{
		return iRet;
	}
	//填写表头
	QXlsx::Document xlsx;  //创建Excel文件
	QXlsx::Format Format1;  //设置该单元的样式
	QString QStrSheetName;  //定义表格名称字符串QStrSheetName
	int fontSize = 12;//字体大小
	Format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);/*横向居中*/
	Format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);/*边框样式*/
	Format1.setVerticalAlignment(QXlsx::Format::AlignVCenter);//横向居中
															  // Format1.setPatternBackgroundColor(QColor(218,238,243));
	Format1.setFontSize(fontSize);

	QXlsx::Format headFormat;  //设置表头样式
	headFormat.setPatternBackgroundColor(QColor(17, 93, 157));      //设置背景颜色
	headFormat.setFontSize(fontSize);  //设置字体大小
	headFormat.setFontBold(true);/*设置加粗*/
	headFormat.setHorizontalAlignment(QXlsx::Format::AlignHCenter);  //横向居中
	headFormat.setVerticalAlignment(QXlsx::Format::AlignVCenter);
	headFormat.setBorderStyle(QXlsx::Format::BorderThin);/*边框样式*/

	QXlsx::Format Format2;  //设置内容样式
	Format2.setPatternBackgroundColor(QColor(218, 238, 243));      //设置背景颜色
	Format2.setFontSize(fontSize);  //设置字体大小
	Format2.setHorizontalAlignment(QXlsx::Format::AlignHCenter);  //横向居中
	Format2.setVerticalAlignment(QXlsx::Format::AlignVCenter);
	Format2.setBorderStyle(QXlsx::Format::BorderThin);/*边框样式*/
	
													  //建立Sheet
	QStrSheetName = m_table;  //工作簿的名称
	xlsx.addSheet(QStrSheetName);  //添加该命名的工作簿
	
	//写表头
	int titleline = 1;
	for (int i = 0; i <title.size(); i++)
	{
		xlsx.write(titleline, i+1, title[i], headFormat);
		xlsx.setColumnWidth(i+1, columnwidth[i]);//设置列宽
	}
	//写了内容
	int dataline = 2;
	int extra = dataname.size()+1;
	QString strBaseDir = CConfig::instance()->GetResaultPath();
	for (int i = 0; i < lists.size(); i++)
	{
		QVariantMap tempInfo = lists.at(i).toMap();
		for (int s = 0; s < dataname.size(); s++)
		{//行列
			if (dataname[s] == "targetpic")
			{
				QString sorucepic = tempInfo.value(dataname.at(s)).toString();
				QFileInfo file(sorucepic);
				if (file.exists())
				{
					QFile::copy(sorucepic, strTargetDir+ file.fileName());//从源路径将文件复制到目标路径
				}
				xlsx.write(i + dataline, s + 1, file.fileName(), Format2); 
				QString strPath = file.absolutePath();
				if (strPath.contains(strBaseDir))
				{
					strPath.remove(strBaseDir);
				}
				if (strPath.right(1)=="/")
				{
					strPath = strPath.left(strPath.size() - 1);
				}
				//C:/Program Files (x86)/OVIT/components/AnalysisCenter/AnalysisPic/渣土车/观沙岭/NVR_ch7_main_20240307010003_20240307020003.mp4/StructPic/_6_45064_1gPWb5S3f10.JPG
				QStringList listDir = strPath.split("/");
				if (listDir.size()>2)
				{
					listDir.pop_back();
					listDir.pop_back();
					for (int m=0;m<listDir.size();m++)
					{
						xlsx.write(i + dataline, extra + m, listDir[m], Format2);
					}
				}

			}
			else if (dataname[s] == "relatepic")
			{
				QString backpic = tempInfo.value(dataname.at(s)).toString();
				QFileInfo file(backpic);
				if (file.exists())
				{
					QFile::copy(backpic, strBackDir+ file.fileName());//从源路径将文件复制到目标路径
				}
				xlsx.write(i + dataline, s + 1, file.fileName(), Format2);
			}
			else
			{
				xlsx.write(i + dataline, s + 1, tempInfo.value(dataname.at(s)).toString(), Format2);
			}
		}
	}

	bool b = xlsx.saveAs(strFilePath);
	if (!b)
	{
		LOG_ERROR("Save to Excel failed %s", strFilePath.toStdString().c_str());
		return iRet;
	}
	return 0;
}

  

标签:插件,file,自定义,CopyAllFileThread,Excel,QString,strTargetPath,include,strPath
From: https://www.cnblogs.com/bclshuai/p/18103126

相关文章

  • Windows10 安装Docker及自定义安装路径
    本文主要介绍了Windows平台下Docker的安装过程,包括安装包下载、自定义安装路径、安装过程、安装后配置和运行验证。一、前提条件,windows10内部版本要升级到19044以上。可以在官网下载升级工具:下载Windows10二、打开控制面板,参照下图打开“启动或关闭windows功能”,Hyper-V......
  • 自定义的基于System.Net.Http.HttpClient的WebClient,可以作为微信支付宝的发起请求时
    个人编写的,自己用于自己的微信api的请求的实现当中,源码公开,大家可以查看反编译源码。以下是使用方法:第一步搜索和安装zmjtool第二步发起请求1/**引入命名空间*/2usingZmjTool;34/**发起Get请求*/5using(varcl=newZmjTool.WebClient())6{7cl.......
  • 自定义微信红包封面小程序,附带后端源码,快速制作个性化红包封面,附带系统搭建教程
    采用云开发,无需服务器,无需域名。小程序里插入banner广告,插屏广告,视频广告,激励式广告。邀请好友获取抽奖机会,或者观看激励式广告获取抽奖机会。自己其他需要引流的小程序推广。下面是小程序的流量主广告代码修改位置:源码中直接替换/pages/下.vue文件中的流量主id即可首页最......
  • Finereport11 类Excel筛选
    微信公众号:次世代数据技术关注可了解更多的教程。问题或建议,请公众号留言或联系本人;微信号:weibw162本教程视频讲解可以关注本人B站账号进行观看:weibw162一、需求描述在使用FIneReport软件开发时,我们希望前台报表展示时可以类似Excel表格筛选那样,在表头进行多选筛选过滤显......
  • 关于《完全手册Excel VBA典型实例大全——通过368个例子掌握》随书样例的下载
    按照早先下载的电子教程查看和编写vba,有些例子使用运行错误,想着看看原始文件。容易看到,网上有提供的doc或者pdf文档,都不完整,可能是{完全手册Excel_VBA典型实例大全:通过368个例子掌握}.{doc,pdf}这样命名的,139页或者134页的样子。搜索发现很多一些链接要么失效,要么是需要相应的积......
  • Vue 自定义组件库通过配置调整样式?
      在Vue自定义组件库中,通常可以通过配置来调整样式。为了实现这一点,你可以定义一组样式相关的配置项,并在组件内部使用这些配置项来动态地设置样式。以下是一个简单的示例,演示了如何通过配置调整组件的样式。自定义组件(CustomComponent.vue)<template><div:style......
  • 前端base64转换成excel
    baseToExcel(baseStr,fileName){varraw=window.atob(baseStr);varuInt8Array=newUint8Array(raw.length);for(vari=0;i<raw.length;i++){uInt8Array[i]=raw.charCodeAt(i);}constlink=......
  • LibreOffice 将word,excel,PowerPoint文件转换PDF
    安装LibreOffice并将Word和Excel文件转换为PDF文件,并设置文件存放路径的步骤如下:1.安装LibreOffice如果尚未安装LibreOffice,可以通过以下命令在Ubuntu上安装:sudoaptupdatesudoaptinstalllibreoffice2.使用LibreOffice将Word和Excel文件转换为PDF要将Word和Excel......
  • 魔兽世界LUA插件开发
    魔兽世界LUA插件开发1.创建插件1.1创建插件文件夹打开WorldofWarcraft\Interface\AddOns文件下,在该文件夹下创建一个插件名文件夹用来存放插件,如Makubex1.2创建插件文件在该文件夹下创建俩个文件,一个是用来给魔兽世界引入的toc头文件,一个是你自己的lua脚本文件......
  • 若依RuoYi-Vue创建菜单并添加自定义页
    全文见:若依管理系统RuoYi-Vue(一):项目启动和菜单创建添加菜单和页面是若依管理系统最核心的功能,也是使用者最关心的事情,这里涉及到核心中的核心功能:权限,先不考虑那么多,直接新增页面,看看能否生效。下面演示新闻列表页添加的过程,这里如果想要新增新闻列表菜单,需要先新增“新闻”父菜......