C#实现EXCEL转Lua
在开发过程中,我们往往需要把资源配在excel表里进行管理,如果我们的逻辑层是使用lua实现的,那我们就需要一个工具来实现把excel直接转换为lua可以读取的table结构
代码如下
//-----------------------------------excel转lua----------------------------------------------- [MenuItem("Assets/工具/ExcelToLua", false, 10)] public static void XlsTolua() { Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered); if (selection != null && selection.Length > 0) { for (int i = 0; i < selection.Length; i++) { string path = AssetDatabase.GetAssetPath(selection[i]); MyReadTo(path, selection[i].name); } } } public static void MyReadTo(string path, string fileName) { Debug.Log("ReadExcel path ==> " + path); FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); if (stream == null) { Debug.Log("stream is null!!"); } IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); DataSet result = excelReader.AsDataSet(); if (result == null) { Debug.Log("读取失败!!"); return; } int rows = result.Tables[0].Rows.Count; int cols = result.Tables[0].Columns.Count; string[] porpertyNames = new string[cols]; string[] porpertyType = new string[cols]; List<string[]> data = new List<string[]>(); for (int i = 0; i < cols; i++) { porpertyNames[i] = result.Tables[0].Rows[1][i].ToString(); porpertyType[i] = result.Tables[0].Rows[2][i].ToString(); } for (int i = 3; i < rows; i++) { if (string.IsNullOrEmpty(result.Tables[0].Rows[i][0].ToString())) { continue; } string[] colsData = new string[cols]; for (int j = 0; j < cols; j++) { colsData[j] = result.Tables[0].Rows[i][j].ToString(); } data.Add(colsData); } DirectoryInfo cc = Directory.GetParent(path); string direPath = cc.ToString(); direPath = direPath.Replace("Excel", "Lua"); if (!Directory.Exists(direPath)) { Directory.CreateDirectory(direPath); } string filePath = direPath + "/" + fileName.Replace(".xlsx", "") + ".lua.bytes"; WriteToLua(filePath, data, porpertyNames, porpertyType); excelReader.Close(); } private static void WriteToLua(string path, List<string[]> data, string[] porpertyNames, string[] porpertyType) { string LuaContent = "local data = { "; for (int i = 0; i < data.Count; i++) { LuaContent += "\n {"; for (int j = 0; j < data[i].Length; j++) { if (porpertyType[j] == null || porpertyType[j] == "") continue; LuaContent += "\n " + porpertyNames[j] + " = "; switch (porpertyType[j]) { case "int": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "nil,"; } else { LuaContent += data[i][j] + ","; } break; case "string": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "nil,"; } else { LuaContent += "\"" + data[i][j] + "\","; } break; case "Array": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "{},"; } else { LuaContent += "{\"" + data[i][j].Replace(";", "\",\"") + "\"},"; } break; case "bool": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "false,"; } else { LuaContent += data[i][j].ToLower() + ","; } break; case "float": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "nil,"; } else { LuaContent += data[i][j] + ","; } break; case "ArrayInt": if (string.IsNullOrEmpty(data[i][j])) { LuaContent += "{},"; } else { LuaContent += "{" + data[i][j].Replace(";", ",") + "},"; } break; } } LuaContent += "\n },"; } LuaContent += "\n}"; LuaContent += "\nreturn data"; File.WriteAllText(path, LuaContent ); AssetDatabase.Refresh(); }
在unity中使用excel转lua功能还需要导入c#可以使用的读取excel所需要的库文件

这样就可以使用以上代码了

浙公网安备 33010602011771号