1. 程式人生 > 其它 >C#實現EXCEL轉Lua

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所需要的庫檔案

這樣就可以使用以上程式碼了