public static string WriteTo(Sheet sheet, string targetDir) { LuaTable luaTable = new LuaTable(); for (int i = 0; i < sheet.LineCount; ++i) { Line line = sheet.GetLineByIndex(i); int id = int.Parse(sheet.GetLineIDByIndex(i)); LuaTable lineTable = new LuaTable(); luaTable.AddItem(id, lineTable); for (int j = 0; j < sheet.FieldCount; ++j) { Field field = sheet.GetFieldByIndex(j); object value = field.GetValue(line.GetCellByIndex(j)); if (value != null) { lineTable.AddItem(field.Name, value); } } } string luaStr = luaTable.GetString(1); if (!string.IsNullOrEmpty(targetDir)) { string filePath = $"{targetDir}/{sheet.Name}.txt"; luaStr = $"local {sheet.Name} = {luaStr}\r\nreturn {sheet.Name}"; File.WriteAllText(filePath, luaStr); } return(luaStr); }
static LuaTable JsonArray2LuaTable(JArray json_arr) { var curLuaTable = new LuaTable(); //往luaTable里面扔有序数组 foreach (var item in json_arr) { //检查子项类型 switch (item.Type) { case JTokenType.Boolean: curLuaTable.AddItem((bool)item); break; case JTokenType.Array: curLuaTable.AddItem(JsonArray2LuaTable(item.ToObject <JArray>())); break; case JTokenType.String: curLuaTable.AddItem((string)item); break; case JTokenType.Object: curLuaTable.AddItem(JsonObject2LuaTable(item.ToObject <JObject>())); break; case JTokenType.Date: curLuaTable.AddItem((string)item); break; case JTokenType.Float: curLuaTable.AddItem((float)item); break; case JTokenType.Integer: curLuaTable.AddItem((int)item); break; case JTokenType.None: curLuaTable.AddItemNil(); break; case JTokenType.Null: curLuaTable.AddItemNil(); break; case JTokenType.TimeSpan: curLuaTable.AddItem((float)item); break; } } return(curLuaTable); }
/// <summary> /// 递归方法 /// </summary> static LuaTable JsonObject2LuaTable(JObject jsonObj) { var curLuaTable = new LuaTable(); if (jsonObj.Type == JTokenType.Array) { return(JsonArray2LuaTable(jsonObj.ToObject <JArray>())); } else if (jsonObj.Type == JTokenType.Object) { //无序 foreach (var item in jsonObj) { switch (item.Value.Type) { case JTokenType.Boolean: curLuaTable.AddItem(item.Key, (bool)item.Value); break; case JTokenType.Array: curLuaTable.AddItem(item.Key, JsonArray2LuaTable(item.Value.ToObject <JArray>())); break; case JTokenType.String: curLuaTable.AddItem(item.Key, (string)item.Value); break; case JTokenType.Date: //转成string curLuaTable.AddItem(item.Key, (string)item.Value); break; case JTokenType.Float: curLuaTable.AddItem(item.Key, (float)item.Value); break; case JTokenType.Integer: curLuaTable.AddItem(item.Key, (int)item.Value); break; case JTokenType.None: curLuaTable.AddItemNil(item.Key); break; case JTokenType.Null: curLuaTable.AddItemNil(item.Key); break; case JTokenType.Object: curLuaTable.AddItem(item.Key, JsonObject2LuaTable(item.Value.ToObject <JObject>())); break; case JTokenType.TimeSpan: curLuaTable.AddItem(item.Key, (float)item.Value); break; } } } return(curLuaTable); }