欢迎加入Unity业内qq交流群:956187480
制表转存csv或txt
public static Dictionary<string, Dictionary<string, string>> LoadExcelFormTxt(string cfgName)
{
TextAsset tw = Resources.Load("Cfg/" + cfgName) as TextAsset;
string strLine = tw.text;
//开始解析字符串
return ExplainString(strLine);
}
public static Dictionary<string, Dictionary<string, string>> ExplainString(string strLine)
{
//创建一个字典,用于存放数据表的内容(从配置表第四行读取)//<字段名,字段值(string数组)>
Dictionary<string, string[]> content = new Dictionary<string, string[]>();
string[] lineArray = strLine.Replace("\r\n", "*").Split(new char[] { '*' });
//获取行数
int rows = lineArray.Length - 1;
//获取列数
int Columns = lineArray[0].Split(new char[] { '\t' }).Length;
//定义一个数组用于存放字段名
string[] ColumnName = new string[Columns];
for (int i = 0; i < rows; i++)
{
string[] Array = lineArray[i].Split(new char[] { '\t' });
for (int j = 0; j < Columns; j++)
{
//获取Array的列的值
string nvalue = Array[j].Trim();
if (i == 0)
{
//存储字段名
ColumnName[j] = nvalue;
content[ColumnName[j]] = new string[rows - 1];
}
else
{
//存储对应字段的默认值//<字段名,默认值>
content[ColumnName[j]][i - 1] = nvalue;
}
}
}
//开始解析
return ExplainDictionary(content);
}
public static Dictionary<string, Dictionary<string, string>> ExplainDictionary(Dictionary<string, string[]> content)
{
Dictionary<string, Dictionary<string, string>> DicContent = new Dictionary<string, Dictionary<string, string>>();
//获取字典中所有的键(字段名)
Dictionary<string, string[]>.KeyCollection Allkeys = content.Keys;
//遍历所有的字段名
foreach (string key in Allkeys)
{
//实例化一个hasData的字典//<ID,字段值>
Dictionary<string, string> hasData = new Dictionary<string, string>();
string[] Data = content[key];
for (int i = 0; i < Data.Length; i++)
{
//<ID><字段值>
hasData[content["ID"][i]] = Data[i];
}
DicContent[key] = hasData;
}
return DicContent;
}
欢迎加入Unity业内qq交流群:956187480