cocos2d-x CSV文件读取 (Excel生成csv文件)
来源:互联网 发布:帝国cms技巧 编辑:程序博客网 时间:2024/05/17 08:23
一、准备素材
1.EXCEL表:内容如下
2.EXCEL表转换为csv文件:方法很多,网上搜索就有。
但要注意:文件保存为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 CSV文件读取 (Excel生成csv文件)
- cocos2d-x CSV文件读取 (Excel生成csv文件)
- Cocos2d-x读取Csv文件工具类
- cocos2d-x(quick-lua)读取csv文件
- cocos2d-x 读取CSV文件,读取本地Excel配置表的方法
- C# 读取EXCEL(CSV文件方式)
- TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。
- cocos2d-x学习笔记——Csv文件读取工具
- Cocos2d-js读取csv文件工具类
- 读取Excel和CSV文件数据
- 读取excel为CSV文件件
- lua读取excel导出的csv文件
- Java读取Excel和CSV文件
- DataGridView生成CSV,XML 和 EXCEL文件
- DataGridView生成CSV,XML 和 EXCEL文件
- PHP 动态导出生成 excel,csv文件
- 生成csv文件 可以使用excell读取
- java读取csv文件生成 sql
- MMS 收发
- java中使用sax解析xml的解决方法
- hangzhou 02
- 输出GBK所有编码及其代表的字符
- 黑马程序员_银行业务调度系统
- cocos2d-x CSV文件读取 (Excel生成csv文件)
- 用谷歌浏览器来当手机模拟器
- mapreduce调试
- 第九章 内存模型和名称空间
- 畅通工程
- 原生 Android 系统 的震动设计
- CodeForces 351B Jeff and Furik(YY题)
- Linux 网络编程之ioctl函数 .
- BitmapDrawable()不正确使用