说明
此处操作的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文件的速度相当。