您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x CSV文件读取 (Excel生成csv文件)

2013-11-09 14:38 495 查看

一、准备素材

      1.EXCEL表:内容如下



      2.EXCEL表转换为csv文件:方法很多,网上搜索就有。

[b] [/b]但要注意:文件保存为UTF-8格式的(否则中文显示乱码)

二、实现代码:

1.操作csv方法类为CCSVParse

CCSVParse.h

#ifndef __cocos2d_x_Excel__CCSVParse__
#define __cocos2d_x_Excel__CCSVParse__

#include <iostream>
#include "cocos2d.h"
#include <vector>
using namespace std;
USING_NS_CC;

class CCSVParse
{
public:
//    CCSVParse(void);
~CCSVParse(void);
CCSVParse(istream& fin=cin, string sep=","):
fieldsep(sep),
cols(0)
{

}

//用以存储数据
std::vector<std::vector<std::string> > data;
bool openFile(const char* fileName);
const char* getData(unsigned int rows, unsigned int cols);
int findColsData(int cols, const char* value);

inline int getCols(){return cols;}
inline int getRows(){return data.size();}

private:
string        fieldsep;
int            cols;

void StringSplit(const string& str, vector<string>& tokens, const char& delimiters);
void split(vector<string>& field, string line);
int advplain(const string& line, string& fld, int);
int advquoted(const string& line, string& fld, int);

};

#endif /* defined(__cocos2d_x_Excel__CCSVParse__) */


CCSVParse.cpp

#include "CCSVParse.h"

//CCSVParse::CCSVParse(void)
//{
//
//}

CCSVParse::~CCSVParse(void)
{

}

void CCSVParse::StringSplit( const string& str, vector<string>& tokens, const char& delimiters )
{
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos-lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
}

void CCSVParse::split( vector<string>& field, string line )
{
string fld;
unsigned int i,j=0;

if( line.length() ==0 )
return;
i=0;

do
{
if(j<line.length() && line[i]=='"')
j = advquoted(line, fld, ++i);
else
j = advplain(line, fld, i);

field.push_back(fld);
i = j+1;
} while (j<line.length());
}

int CCSVParse::advplain( const string& s, string& fld, int i)
{
unsigned int j;
j = s.find_first_of(fieldsep, i);
if(j>s.length())
j=s.length();
fld = string(s,i,j-i);
return j;
}

int CCSVParse::advquoted( const string& s, string& fld, int i)
{
unsigned int j;
fld = "";
for (j=i; j<s.length(); ++j)
{
if(s[j]=='"' && s[++j]!='"')
{
unsigned int k = s.find_first_of(fieldsep, j);
if(k>s.length())
k = s.length();
for(k-=j; k-->0;)
fld += s[j++];
break;
}
fld += s[j];
}
return j;
}

//解析 CVS 文件
bool CCSVParse::openFile( const char* fileName )
{
string pathKey = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName);
unsigned char* pBuffer = NULL;
unsigned long bufferSize = 0;
pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize);

string s = (char*)pBuffer;
string str = s.substr(0,bufferSize);

vector<string> line;
StringSplit(str, line, '\n');
for(unsigned int i=0; i<line.size(); ++i)
{
vector<string> field;
split(field, line[i]);
data.push_back(field);
cols = max(cols, (int)field.size());
}

return true;
}

//获取指定行列的数据
const char* CCSVParse::getData(unsigned int rows, unsigned int cols )
{
if (rows<0 || rows>=data.size() || cols<0 || cols>=data[rows].size())
{
return "";
}
return data[rows][cols].c_str();
}

//获取指定数据的列下标
int CCSVParse::findColsData( int cols, const char* value )
{
for (unsigned int i=0; i<data.size(); ++i)
{
if(strcmp(getData(i,cols),value)==0)
return i;
}
return -1;
}


      在init内添加如下代码:

csvFile->openFile("testExcel.csv");
for (int i=0; i<csvFile->getCols(); ++i)
{
string strLine = "";
for(int j=0; j<csvFile->getRows(); ++j)
{
strLine += csvFile->getData(i,j);
strLine += ",";
}
CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",20);
pLab->setColor(ccc3(255, 0, 0));
pLab->setPosition(ccp(size.width/2,size.height-150-i*30));
this->addChild(pLab,2);
}
delete csvFile;


      运行效果如下:



引用博文:http://www.cnblogs.com/MrGreen/archive/2013/09/03/3295662.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x c++