C++实现Excel文件存储数组

C++实现Excel文件存储数组

说明

此处操作的Excel文件为Excel文件中的csv文件,因为xlsx文件的数据写入速度真的让人着急,不适合用于快速存储数据,而csv文件也是Excel文件中的一员,打开后与xlsx文件相同,但在写入数据的过程中却和txt文件写入的方法相当,可以使用流函数实现快速读写操作。

格式

可参考博客C++实现Txt文件存储数组,格式方面只是将分隔用的空格改成了逗号,其他相同。

程序

//CExcel.h
#pragma once
#include "Cmyfile.h"
#include <vector>
#include <sstream>
#include "CDebug.h"
using namespace std;
class CExcel:public Cmyfile
{
public:CExcel();
	   CExcel(string str);
	   CExcel(CString str);
	   CExcel(char* str);
	   ~CExcel();

	   vector<vector<string>> readstring();
	   vector<vector<string>> readstring(string name) ;
	   vector<vector<string>> readstring(string name, int w, int h);
	   vector<vector<string>> readstring(CString name);
	   vector<vector<string>> readstring(CString name, int w, int h);

	   vector<vector<int>> readint();
	   vector<vector<int>> readint(string name);
	   vector<vector<int>> readint(string name, int w, int h);
	   vector<vector<int>> readint(CString name);
	   vector<vector<int>> readint(CString name, int w, int h);

	   vector<vector<float>> readfloat();
	   vector<vector<float>> readfloat(string name);
	   vector<vector<float>> readfloat(string name, int w, int h);
	   vector<vector<float>> readfloat(CString name);
	   vector<vector<float>> readfloat(CString name, int w, int h);

	   vector<vector<double>> readdouble();
	   vector<vector<double>> readdouble(string name);
	   vector<vector<double>> readdouble(string name, int w, int h);
	   vector<vector<double>> readdouble(CString name);
	   vector<vector<double>> readdouble(CString name, int w, int h);



	   template<class T>
	   bool writeT(vector<vector<T>> vec);
	   template<class T>
	   bool writeT(string name, vector<vector<T>> vec);
	   template<class T>
	   bool writeT(CString name, vector<vector<T>> vec);



	   template<class T>
	   friend CExcel& operator>>(CExcel & debug, vector<vector<T>>  vec);

private:
	string name;   //暂存的文件的名称
	   
};



template<class T>
inline bool CExcel::writeT(vector<vector<T>> vec)
{
	if (vec.empty())  //空的,不做任何处理
		return false;
	if (name.empty())
		return false;
	unsigned int w = vec.size();
	unsigned int h = vec[0].size();
	ofstream outfile(name.c_str());
	if (!outfile.is_open())
		return false;
	outfile << w << " " << h << Cendl;
	for (uint i = 0; i < w; i++)
	{
		for (uint j = 0; j < h; j++)
		{
			outfile << vec;
			if (j!<= h - 1)
				outfile << " ";
		}
		out << Cendl;
	}
	outfile.close();
	return true;
}

template<class T>
inline bool CExcel::writeT(string name, vector<vector<T>> vec)
{
	if (vec.empty())  //空的,不做任何处理
		return false;
	if (name.empty())
		return false;
	unsigned int w = vec.size();
	unsigned int h = vec[0].size();
	ofstream outfile(name.c_str());
	if (!outfile.is_open())
		return false;
	outfile.clear();
	outfile << w << "," << h << "\n";
	for (unsigned int i = 0; i < w; i++)
	{
		for (unsigned int j = 0; j < h; j++)
		{
			outfile << vec[i][j];
			if (j != h - 1)
				outfile << ",";
		}
		outfile << "\n";
	}
	outfile.close();
	return true;
}

template<class T>
inline bool CExcel::writeT(CString name, vector<vector<T>> vec)
{
	string name1 = CT2A(name.GetBuffer());
	return writeT(name1, vec);
}


template<class T>
inline CExcel & operator>>(CExcel & debug, vector<vector<T>> vec)
{
	// TODO: 在此处插入 return 语句
	debug.writeT(vec);
	return debug;
}


//CExcel.cpp
#include "pch.h"
#include "CExcel.h"
#include "CSprintf.h"
CExcel::CExcel()
{
	this->name = "data.csv";
}

CExcel::CExcel(string str)
{
	this->name = str;
}

CExcel::CExcel(CString str)
{
	string str1 = CT2A(str.GetBuffer());
	this->name = str1;
}

CExcel::CExcel(char * str)
{
	this->name = str;
}

CExcel::~CExcel()
{
}

vector<vector<string>> CExcel::readstring()
{
	ifstream myfile(name.c_str());
	if (!myfile.is_open())
	{
		OutputDebugString(_T("can not open this file"));
		return { };
	}
	string line;
	int w, h;
	getline(myfile, line);
	string filed;
	istringstream mystr(line);
	getline(mystr, filed, ',');
	w = atoi(filed.c_str());
	getline(mystr, filed, ',');
	h = atoi(filed.c_str());
	CDebug() << Cendl;
	CDebug() << w << " " << h;
	CDebug() << Cendl;
	vector<vector<string>> vec;
	//CSprintf sp;
	vec.resize(w);
	for (int i = 0; i < w; i++)
	{
		vec[i].resize(h);
	}
	for (int i = 0; i < w; i++)
	{
		string line;
		getline(myfile, line);
		for (int j = 0; j < h; j++)
		{
			string filed;
			istringstream mystr(line);
			getline(mystr, filed, ',');
			vec[i][j] = filed;
			//sp << vec[i][j] << " ";
		}
		//sp << "\n";
	}
	//CDebug() << sp;
	return vec;
}

//模板型的函数无法放入到cpp文件中,放入cpp会导致除CTxt.cpp以外的文件无法调用
vector<vector<string>> CExcel::readstring(string name)
{
	ifstream myfile(name.c_str());
	if (!myfile.is_open())
	{
		OutputDebugString(_T("can not open this file"));
		return { };
	}
	string line;
	int w, h;
	getline(myfile, line);
	string filed;
	istringstream mystr(line);
	getline(mystr, filed, ',');
	w = atoi(filed.c_str());
	getline(mystr, filed, ',');
	h = atoi(filed.c_str());
	CDebug() << Cendl;
	CDebug() << w << " " << h;
	CDebug() << Cendl;
	vector<vector<string>> vec;
	//CSprintf sp;
	vec.resize(w);
	for (int i = 0; i < w; i++)
	{
		vec[i].resize(h);
	}
	for (int i = 0; i < w; i++)
	{
		string line;
		getline(myfile, line);
		istringstream mystr(line);
		for (int j = 0; j < h; j++)
		{
			string filed;
			getline(mystr, filed, ',');
			vec[i][j] = filed;
			//sp << vec[i][j] << " ";
		}
		//sp << "\n";
	}
	//CDebug() << sp;
	return vec;
}
vector<vector<string>> CExcel::readstring(string name, int w, int h)
{
	ifstream myfile(name.c_str());
	if (!myfile.is_open())
	{
		OutputDebugString(_T("can not open this file"));
		return { };
	}
	string line;
	int w1, h1;
	getline(myfile, line);
	string filed;
	istringstream mystr(line);
	getline(mystr, filed, ',');
	w1 = atoi(filed.c_str());
	getline(mystr, filed, ',');
	h1 = atoi(filed.c_str());
	CDebug() << Cendl;
	CDebug() << w << " " << h;
	vector<vector<string>> vec;
	vec.resize(w);
	for (int i = 0; i < w; i++)
	{
		vec[i].resize(h);
	}
	for (int i = 0; i < w; i++)
	{
		string line;
		getline(myfile, line);
		for (int j = 0; j < h; j++)
		{
			string filed;
			istringstream mystr(line);
			getline(mystr, filed, ',');
			vec[i][j] = filed;
		}
	}
	return vec;
}

vector<vector<string>> CExcel::readstring(CString name)
{
	string name1 = CT2A(name.GetBuffer());
	return readstring(name1);
}

vector<vector<string>> CExcel::readstring(CString name, int w, int h)
{
	string name1 = CT2A(name.GetBuffer());
	return readstring(name1, w, h);
}

vector<vector<int>> CExcel::readint()
{
	vector<vector<string>> vec = readstring();
	vector<vector<int>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<int> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			int date = atoi(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}
			
	return vec1;
}

vector<vector<int>> CExcel::readint(string name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<int>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<int> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			int date = atoi(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<int>> CExcel::readint(string name, int w, int h)
{
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<int>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<int> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			int date = atoi(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
	return vector<vector<int>>();
}

vector<vector<int>> CExcel::readint(CString name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<int>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<int> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			int date = atoi(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<int>> CExcel::readint(CString name, int w, int h)
{
	
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<int>> vec1;
	unsigned int w1= vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<int> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			int date = atoi(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<float>> CExcel::readfloat()
{
	vector<vector<string>> vec = readstring();
	vector<vector<float>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<float> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			float date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<float>> CExcel::readfloat(string name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<float>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<float> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			float date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<float>> CExcel::readfloat(string name, int w, int h)
{
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<float>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<float> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			float date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<float>> CExcel::readfloat(CString name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<float>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<float> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			float date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<float>> CExcel::readfloat(CString name, int w, int h)
{
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<float>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<float> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			float date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<double>> CExcel::readdouble()
{
	vector<vector<string>> vec = readstring();
	vector<vector<double>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<double> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			double date = stod(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<double>> CExcel::readdouble(string name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<double>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<double> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			double date = stod(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}
vector<vector<double>> CExcel::readdouble(string name, int w, int h)
{
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<double>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<double> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			double date = stod(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<double>> CExcel::readdouble(CString name)
{
	vector<vector<string>> vec = readstring(name);
	vector<vector<double>> vec1;
	unsigned int w = vec.size(), h = vec[0].size();
	for (unsigned int i = 0; i < w; i++)
	{
		vector<double> vec2;
		for (unsigned int j = 0; j < h; j++)
		{
			double date = stod(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

vector<vector<double>> CExcel::readdouble(CString name, int w, int h)
{
	vector<vector<string>> vec = readstring(name,w,h);
	vector<vector<double>> vec1;
	unsigned int w1 = vec.size(), h1 = vec[0].size();
	for (unsigned int i = 0; i < w1; i++)
	{
		vector<double> vec2;
		for (unsigned int j = 0; j < h1; j++)
		{
			double date = atof(vec[i][j].c_str());
			vec2.push_back(date);
		}
		vec1.push_back(vec2);
	}

	return vec1;
}

最后

数据写入后的样式,写入速度与写入Txt文件的速度相当。
在这里插入图片描述

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[3\]中的代码展示了如何使用C++读取文件并将数据保存到结构体数组中。在这个例子中,使用了fstream库来进行文件的读写操作。首先,通过打开文件并设置文件模式为二进制写入模式,将结构体数组整体写入文件。然后,再次打开文件并设置文件模式为二进制读取模式,使用read函数将文件中的数据读取到结构体数组中。最后,通过遍历结构体数组,可以将数据打印出来。 引用\[2\]中的代码展示了另一种读取文件并保存至结构体数组的方法。在这个例子中,使用了stdio.h中的fopen和fprintf函数来进行文件的写入操作,使用了fscanf函数来进行文件读取操作。通过循环读取文件中的数据,并将数据保存到结构体数组中,最后可以将数据打印出来。 综上所述,无论是使用fstream库还是stdio.h库,都可以实现文件中的数据读取并保存至结构体数组中的功能。具体的实现方式可以根据实际需求选择。 #### 引用[.reference_title] - *1* *3* [c++文件读取结构体整体](https://blog.csdn.net/qq_16198739/article/details/127185502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C语言文件操作——输入数据存到文件,从文件读取到结构体](https://blog.csdn.net/weixin_44572229/article/details/120782682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值