1.EXCEL表:内容如下
2.EXCEL表转换为csv文件:方法很多,网上搜索就有。
但要注意:文件保存为UTF-8格式的(否则中文显示乱码)
1.操作csv方法类为CCSVParse
CCSVParse.h
#ifndef __cocos2d_x_Excel__CCSVParse__
#define __cocos2d_x_Excel__CCSVParse__
#include
#include "cocos2d.h"
#include
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 > 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& tokens, const char& delimiters);
void split(vector& 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__) */
#include "CCSVParse.h"
//CCSVParse::CCSVParse(void)
//{
//
//}
CCSVParse::~CCSVParse(void)
{
}
void CCSVParse::StringSplit( const string& str, vector& 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& field, string line )
{
string fld;
unsigned int i,j=0;
if( line.length() ==0 )
return;
i=0;
do
{
if(js.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; js.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 line;
StringSplit(str, line, '\n');
for(unsigned int i=0; i 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
csvFile->openFile("testExcel.csv");
for (int i=0; igetCols(); ++i)
{
string strLine = "";
for(int j=0; jgetRows(); ++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