Unity技术沉淀--Excel表格导出json、lua

在开发过程中,Excel数据表格是我们很常用的数据存储工具,但是我们如何在程序中更方便快捷的去使用Excel中的数据呢?
首先说明下json格式是我们在开发中经常使用到的一种数据存储结构。JSON,实际上就是一种规范的存储数据的结构,它具有结构简明、实现方便、多变万用、跨平台等特点,深受开发者们喜爱。
我们需要引入三个dll文件,Newtonsoft.Json.dll、Excel.dll、ICSharpCode.SharpZipLib、文章下面附有demo工程。

Excel数据结构定义

我们看一个简单的Excel数据结构
第一行是每个变量名的说明,大家也可以在这一行做标签处理,说明更具体些。


image.png

第二行第一列定义为类名与unity代码类名一一对应,这样方便在代码中查找相应类以及对其引用。


image.png

第二行其他列定义为类的变量名与代码中变量名一一对应,再导出json后,我们能快速通过这些变量名绑定到相应的类中。


image.png

代码逻辑

1.首先我们定义editor工具类,去导出Excel数据。
给定指定的Excel文件夹路径,整体导出json数据


image.png

2.讲解下ExcelUtility类
构造函数打开相应的Excel文件


image.png

导出json,一行一行的去读取Excel数据
/// <summary>
    /// 转换为Json
    /// </summary>
    /// <param name="JsonPath">Json文件路径</param>
    /// <param name="Header">表头行数</param>
    public void ConvertToJson(string JsonPath, Encoding encoding, string name)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;

        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[0];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;

        //准备一个列表存储整个表的数据
        //Dictionary<string, List<Dictionary<string, object>>> dic = new Dictionary<string, List<Dictionary<string, object>>>();
        List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

        //string excelName = mSheet.Rows[1][0].ToString();
        //dic[excelName] = table;
        //读取数据
        for (int i = 2; i < rowCount; i++)
        {
            //准备一个字典存储每一行的数据
            Dictionary<string, object> row = new Dictionary<string, object>();
            for (int j = 0; j < colCount; j++)
            {
                //读取第2行数据作为表头字段
                string field = mSheet.Rows[1][j].ToString();
                if (String.IsNullOrEmpty(field)) continue;
                //Key-Value对应
                row[field] = mSheet.Rows[i][j];
            }

            //添加到表数据中
            table.Add(row);
        }

        //生成Json字符串
        string json = JsonConvert.SerializeObject(table, Newtonsoft.Json.Formatting.Indented);
        //json = StringEncryption.EncryptDES(json);   //加密
        //写入文件
        //string newJsonPath = Application.streamingAssetsPath + "/" + GetAssetPath(excelName + ".json");

        using (FileStream fileStream = new FileStream(JsonPath, FileMode.Create, FileAccess.Write))
        {
            using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
            {
                textWriter.Write(json);
            }
        }
    }

导出lua代码结构如导出json类似,自己去拼接lua每一行代码。

/// <summary>
    /// 转换为lua
    /// </summary>
    /// <param name="luaPath">lua文件路径</param>
    public void ConvertToLua(string luaPath, Encoding encoding ,ref List<string> fileNames)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;
        
        

        //读取数据表
        foreach (DataTable mSheet in mResultSet.Tables)
        {
            //判断数据表内是否存在数据
            if (mSheet.Rows.Count < 1)
                continue;
            if (!mSheet.Rows[0][0].ToString().Equals("类名"))
                continue;
            string fileName = "test";
            fileName = mSheet.Rows[1][0].ToString();
            if (String.IsNullOrEmpty(fileName)) continue;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("local "+fileName+" = {");
            stringBuilder.Append("\r\n");
            
            //读取数据表行数和列数
            int rowCount = mSheet.Rows.Count;
            int colCount = mSheet.Columns.Count;

            //准备一个列表存储整个表的数据
            List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

            //读取数据
            for (int i = 2; i < rowCount; i++)
            {
                bool isNext = false;
                for (int m = 0; m < colCount; m++)
                {
                    string objStr = mSheet.Rows[i][m].ToString();
                    if (!string.IsNullOrEmpty(objStr) && objStr != "null")
                    {
                        isNext = true;
                        break;
                    }
                }
                if (!isNext) continue;
                //准备一个字典存储每一行的数据
                Dictionary<string, object> row = new Dictionary<string, object>();
                for (int j = 1; j < colCount; j++)
                {
                    //读取第1行数据作为表头字段
                    string field = mSheet.Rows[1][j].ToString();
                    //Key-Value对应
                    row[field] = mSheet.Rows[i][j];
                }
                //添加到表数据中
                table.Add(row);
            }
            //stringBuilder.Append(string.Format("\t\"{0}\" = ", mSheet.TableName));
            //stringBuilder.Append("{\r\n");
            foreach (Dictionary<string, object> dic in table)
            {
                stringBuilder.Append("\t\t{\r\n");
                foreach (string key in dic.Keys)
                {
                    if (string.IsNullOrEmpty(key)) continue;
                    object valueObj = dic[key];
                    Type type = valueObj.GetType();
                    if (valueObj.GetType().Name == "String")
                    {
                        string valueStr = valueObj as string;
                        string str = valueStr.Replace("\"", "\\\"");
                        if (!string.IsNullOrEmpty(valueStr))
                            stringBuilder.Append(string.Format("\t\t\t{0} = \"{1}\",\r\n", key, str));
                        else
                            stringBuilder.Append(string.Format("\t\t\t{0} = nil,\r\n", key));
                    }  
                    else if (valueObj == null || valueObj.GetType().Name == "DBNull")
                        stringBuilder.Append(string.Format("\t\t\t{0} = nil,\r\n", key));
                    else
                        stringBuilder.Append(string.Format("\t\t\t{0} = {1},\r\n", key, valueObj));
                }
                stringBuilder.Append("\t\t},\r\n");
            }
            stringBuilder.Append("}\r\n");
            stringBuilder.Append("_G."+fileName+" = "+ fileName);
            stringBuilder.Append("\r\n");
            stringBuilder.Append("setmetatable(_G," + fileName + ")");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("return " + fileName);
            fileNames.Add(fileName);
            fileName += ".lua";
            string writePath = Path.Combine(luaPath, fileName);
            //写入文件
            using (FileStream fileStream = new FileStream(writePath, FileMode.Create, FileAccess.Write))
            {
                using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
                {
                    textWriter.Write(stringBuilder.ToString());
                }
            }
        }
    }

ExcelUtility完整代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Excel;
using System.Data;
using System.IO;
using Newtonsoft.Json;  
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System;   

public class ExcelUtility
{

    /// <summary>
    /// 表格数据集合
    /// </summary>
    private DataSet mResultSet;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="excelFile">Excel file.</param>
    public ExcelUtility(string excelFile)
    {
        if (File.Exists(excelFile))
        {
            FileStream mStream = File.Open(excelFile, FileMode.Open, FileAccess.Read);
            IExcelDataReader mExcelReader = ExcelReaderFactory.CreateOpenXmlReader(mStream);
            mResultSet = mExcelReader.AsDataSet();
        }
        
    }

    /// <summary>
    /// 转换为实体类列表
    /// </summary>
    public List<T> ConvertToList<T>()
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return null;
        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[0];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return null;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;

        //准备一个列表以保存全部数据
        List<T> list = new List<T>();

        //读取数据
        for (int i = 1; i < rowCount; i++)
        {
            //创建实例
            Type t = typeof(T);
            ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
            T target = (T)ct.Invoke(null);
            for (int j = 0; j < colCount; j++)
            {
                //读取第1行数据作为表头字段
                string field = mSheet.Rows[0][j].ToString();
                object value = mSheet.Rows[i][j];
                //设置属性值
                SetTargetProperty(target, field, value);
            }

            //添加至列表
            list.Add(target);
        }

        return list;
    }
    public string GetAssetPath(string name)
    {
        string Platform = "";
#if UNITY_IOS
            Platform="iOS";
#elif UNITY_ANDROID
        Platform = "Android";
#elif UNITY_WEBPLAYER
            Platform ="WebPlayer";
#elif UNITY_WP8
            Platform="WP8Player";
#elif UNITY_METRO
            Platform = "MetroPlayer";
#elif UNITY_OSX || UNITY_STANDALONE_OSX
        Platform = "StandaloneOSXIntel";
#else
        Platform = "StandaloneWindows";
#endif
        string path = Path.Combine(Platform, name);  //System.String.Format("{0}/{1}", Platform, name);
        //string path = Platform + "/" + name;  
        return path;
    }
    /// <summary>
    /// 转换为Json
    /// </summary>
    /// <param name="JsonPath">Json文件路径</param>
    /// <param name="Header">表头行数</param>
    public void ConvertToJson(string JsonPath, Encoding encoding, string name)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;

        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[0];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;

        //准备一个列表存储整个表的数据
        //Dictionary<string, List<Dictionary<string, object>>> dic = new Dictionary<string, List<Dictionary<string, object>>>();
        List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

        //string excelName = mSheet.Rows[1][0].ToString();
        //dic[excelName] = table;
        //读取数据
        for (int i = 2; i < rowCount; i++)
        {
            //准备一个字典存储每一行的数据
            Dictionary<string, object> row = new Dictionary<string, object>();
            for (int j = 0; j < colCount; j++)
            {
                //读取第2行数据作为表头字段
                string field = mSheet.Rows[1][j].ToString();
                if (String.IsNullOrEmpty(field)) continue;
                //Key-Value对应
                row[field] = mSheet.Rows[i][j];
            }

            //添加到表数据中
            table.Add(row);
        }

        //生成Json字符串
        string json = JsonConvert.SerializeObject(table, Newtonsoft.Json.Formatting.Indented);
        //json = StringEncryption.EncryptDES(json);   //加密
        //写入文件
        //string newJsonPath = Application.streamingAssetsPath + "/" + GetAssetPath(excelName + ".json");

        using (FileStream fileStream = new FileStream(JsonPath, FileMode.Create, FileAccess.Write))
        {
            using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
            {
                textWriter.Write(json);
            }
        }
    }

    public void ConvertToJson(string AssetPath, Encoding encoding)
    {
        string fileName = "test";
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;
        for(int ln = 0; ln < mResultSet.Tables.Count;ln++ )
        {
            //默认读取第一个数据表
            DataTable mSheet = mResultSet.Tables[ln];

            //判断数据表内是否存在数据
            if (mSheet.Rows.Count < 1)
                continue;
            if (!mSheet.Rows[0][0].ToString().Equals("类名"))
                continue;
            fileName = mSheet.Rows[1][0].ToString();
            if (String.IsNullOrEmpty(fileName)) continue;
            fileName += ".json";
            //读取数据表行数和列数
            int rowCount = mSheet.Rows.Count;
            int colCount = mSheet.Columns.Count;

            //准备一个列表存储整个表的数据
            //Dictionary<string, List<Dictionary<string, object>>> dic = new Dictionary<string, List<Dictionary<string, object>>>();
            List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

            //string excelName = mSheet.Rows[1][0].ToString();
            //dic[excelName] = table;
            //读取数据
            for (int i = 2; i < rowCount; i++)
            {
                bool isNext = false;
                for (int m = 0; m < colCount; m++)
                {
                    string objStr = mSheet.Rows[i][m].ToString();
                    if (!string.IsNullOrEmpty(objStr) && objStr != "null")
                    {
                        isNext = true;
                        break;
                    }
                }
                if (!isNext) continue;
                //准备一个字典存储每一行的数据
                Dictionary<string, object> row = new Dictionary<string, object>();
                for (int j = 1; j < colCount; j++)
                {
                    //读取第2行数据作为表头字段
                    string field = mSheet.Rows[1][j].ToString();
                    if (String.IsNullOrEmpty(field)) continue;
                    //Key-Value对应
                    row[field] = mSheet.Rows[i][j];
                }

                //添加到表数据中
                    table.Add(row);
            }

            //生成Json字符串
            string json = JsonConvert.SerializeObject(table, Newtonsoft.Json.Formatting.Indented);
            //json = StringEncryption.EncryptDES(json);   //加密
            //写入文件
            //string newJsonPath = Application.streamingAssetsPath + "/" + GetAssetPath(excelName + ".json");
            string JsonPath = Path.Combine(AssetPath, fileName);
            using (FileStream fileStream = new FileStream(JsonPath, FileMode.Create, FileAccess.Write))
            {
                using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
                {
                    textWriter.Write(json);
                }
            }
        }


        
    }

    /// <summary>
    /// 转换为lua
    /// </summary>
    /// <param name="luaPath">lua文件路径</param>
    public void ConvertToLua(string luaPath, Encoding encoding ,ref List<string> fileNames)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;
        
        

        //读取数据表
        foreach (DataTable mSheet in mResultSet.Tables)
        {
            //判断数据表内是否存在数据
            if (mSheet.Rows.Count < 1)
                continue;
            if (!mSheet.Rows[0][0].ToString().Equals("类名"))
                continue;
            string fileName = "test";
            fileName = mSheet.Rows[1][0].ToString();
            if (String.IsNullOrEmpty(fileName)) continue;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("local "+fileName+" = {");
            stringBuilder.Append("\r\n");
            
            //读取数据表行数和列数
            int rowCount = mSheet.Rows.Count;
            int colCount = mSheet.Columns.Count;

            //准备一个列表存储整个表的数据
            List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

            //读取数据
            for (int i = 2; i < rowCount; i++)
            {
                bool isNext = false;
                for (int m = 0; m < colCount; m++)
                {
                    string objStr = mSheet.Rows[i][m].ToString();
                    if (!string.IsNullOrEmpty(objStr) && objStr != "null")
                    {
                        isNext = true;
                        break;
                    }
                }
                if (!isNext) continue;
                //准备一个字典存储每一行的数据
                Dictionary<string, object> row = new Dictionary<string, object>();
                for (int j = 1; j < colCount; j++)
                {
                    //读取第1行数据作为表头字段
                    string field = mSheet.Rows[1][j].ToString();
                    //Key-Value对应
                    row[field] = mSheet.Rows[i][j];
                }
                //添加到表数据中
                table.Add(row);
            }
            //stringBuilder.Append(string.Format("\t\"{0}\" = ", mSheet.TableName));
            //stringBuilder.Append("{\r\n");
            foreach (Dictionary<string, object> dic in table)
            {
                stringBuilder.Append("\t\t{\r\n");
                foreach (string key in dic.Keys)
                {
                    if (string.IsNullOrEmpty(key)) continue;
                    object valueObj = dic[key];
                    Type type = valueObj.GetType();
                    if (valueObj.GetType().Name == "String")
                    {
                        string valueStr = valueObj as string;
                        string str = valueStr.Replace("\"", "\\\"");
                        if (!string.IsNullOrEmpty(valueStr))
                            stringBuilder.Append(string.Format("\t\t\t{0} = \"{1}\",\r\n", key, str));
                        else
                            stringBuilder.Append(string.Format("\t\t\t{0} = nil,\r\n", key));
                    }  
                    else if (valueObj == null || valueObj.GetType().Name == "DBNull")
                        stringBuilder.Append(string.Format("\t\t\t{0} = nil,\r\n", key));
                    else
                        stringBuilder.Append(string.Format("\t\t\t{0} = {1},\r\n", key, valueObj));
                }
                stringBuilder.Append("\t\t},\r\n");
            }
            stringBuilder.Append("}\r\n");
            stringBuilder.Append("_G."+fileName+" = "+ fileName);
            stringBuilder.Append("\r\n");
            stringBuilder.Append("setmetatable(_G," + fileName + ")");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("return " + fileName);
            fileNames.Add(fileName);
            fileName += ".lua";
            string writePath = Path.Combine(luaPath, fileName);
            //写入文件
            using (FileStream fileStream = new FileStream(writePath, FileMode.Create, FileAccess.Write))
            {
                using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
                {
                    textWriter.Write(stringBuilder.ToString());
                }
            }
        }

        
    }


    /// <summary>
    /// 转换为CSV
    /// </summary>
    public void ConvertToCSV(string CSVPath, Encoding encoding)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;

        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[0];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;

        //创建一个StringBuilder存储数据
        StringBuilder stringBuilder = new StringBuilder();

        //读取数据
        for (int i = 0; i < rowCount; i++)
        {
            for (int j = 0; j < colCount; j++)
            {
                //使用","分割每一个数值
                stringBuilder.Append(mSheet.Rows[i][j] + ",");
            }
            //使用换行符分割每一行
            stringBuilder.Append("\r\n");
        }
        
        //写入文件
        using (FileStream fileStream = new FileStream(CSVPath, FileMode.Create, FileAccess.Write))
        {
            using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
            {
                textWriter.Write(stringBuilder.ToString());
            }
        }
    }

    /// <summary>
    /// 导出为Xml
    /// </summary>
    public void ConvertToXml(string XmlFile)
    {
        //判断Excel文件中是否存在数据表
        if (mResultSet.Tables.Count < 1)
            return;

        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[0];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;

        //创建一个StringBuilder存储数据
        StringBuilder stringBuilder = new StringBuilder();
        //创建Xml文件头
        stringBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        stringBuilder.Append("\r\n");
        //创建根节点
        stringBuilder.Append("<Table>");
        stringBuilder.Append("\r\n");
        //读取数据
        for (int i = 1; i < rowCount; i++)
        {
            //创建子节点
            stringBuilder.Append("  <Row>");
            stringBuilder.Append("\r\n");
            for (int j = 0; j < colCount; j++)
            {
                stringBuilder.Append("   <" + mSheet.Rows[0][j].ToString() + ">");
                stringBuilder.Append(mSheet.Rows[i][j].ToString());
                stringBuilder.Append("</" + mSheet.Rows[0][j].ToString() + ">");
                stringBuilder.Append("\r\n");
            }
            //使用换行符分割每一行
            stringBuilder.Append("  </Row>");
            stringBuilder.Append("\r\n");
        }
        //闭合标签
        stringBuilder.Append("</Table>");
        //写入文件
        using (FileStream fileStream = new FileStream(XmlFile, FileMode.Create, FileAccess.Write))
        {
            using (TextWriter textWriter = new StreamWriter(fileStream, Encoding.GetEncoding("utf-8")))
            {
                textWriter.Write(stringBuilder.ToString());
            }
        }
    }

    /// <summary>
    /// 设置目标实例的属性
    /// </summary>
    private void SetTargetProperty(object target, string propertyName, object propertyValue)
    {
        //获取类型
        Type mType = target.GetType();
        //获取属性集合
        PropertyInfo[] mPropertys = mType.GetProperties();
        foreach (PropertyInfo property in mPropertys)
        {
            if (property.Name == propertyName)
            {
                property.SetValue(target, Convert.ChangeType(propertyValue, property.PropertyType), null);
            }
        }
    }
}


导出工具类ExcelTool代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class ExcelTool
{
    [MenuItem("Tools/读取配置文件")]
    public static void CreateAndRead()
    {
        string assetPath = Path.Combine(Application.dataPath, "Res/JsonData");
        Debug.Log($"Application.dataPath==={Application.dataPath}");
        if (Directory.Exists(assetPath))
            Directory.Delete(assetPath, true);
        Directory.CreateDirectory(assetPath);  
        string path = Application.dataPath;
        path = path.Replace("Assets", "GameData/");
        List<string> excels = new List<string>();
        var dirInfo = new DirectoryInfo(path);
        foreach (FileInfo preaInfoFile in dirInfo.GetFiles("*.*", SearchOption.AllDirectories))
        {                                
            if (preaInfoFile.Name.EndsWith(".xlsx"))
            {
                //构造Excel工具类
                ExcelUtility excel = new ExcelUtility(preaInfoFile.ToString());
                //Encoding encoding = Encoding.GetEncoding("gb2312");
                Encoding encoding = new UTF8Encoding();
                excel.ConvertToJson(assetPath, encoding);
                excels.Add(preaInfoFile.Name.Split('.')[0]);
            }
                
        }
        Debug.Log("读取配置文件成功");
        AssetDatabase.Refresh();
    }
    [MenuItem("Tools/读取配置文件Lua")]
    public static void CreateLuaAndRead()
    {
        string assetPath = Path.Combine(Application.dataPath, "Lua/LuaData");
        Debug.Log($"Application.dataPath==={Application.dataPath}");
        if (Directory.Exists(assetPath))
            Directory.Delete(assetPath, true);
        Directory.CreateDirectory(assetPath);
        string path = Application.dataPath;
        path = path.Replace("Assets", "GameData/");
        List<string> excels = new List<string>();
        var dirInfo = new DirectoryInfo(path);
        List<string> luafileNames = new List<string>();
        foreach (FileInfo preaInfoFile in dirInfo.GetFiles("*.*", SearchOption.AllDirectories))
        {
            if (preaInfoFile.Name.EndsWith(".xlsx"))
            {
                //构造Excel工具类
                ExcelUtility excel = new ExcelUtility(preaInfoFile.ToString());
                //Encoding encoding = Encoding.GetEncoding("gb2312");
                Encoding encoding = new UTF8Encoding();
                excel.ConvertToLua(assetPath, encoding,ref luafileNames);
                excels.Add(preaInfoFile.Name.Split('.')[0]);
            }

        }
        CreateLuaData(assetPath, luafileNames);
        Debug.Log("读取配置文件成功");
        AssetDatabase.Refresh();
    }
    private static void CreateLuaData(string luaPath, List<string> luafileNames)
    {
        string luaData = "LuaData.lua";
        string writePath = Path.Combine(luaPath, luaData);
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < luafileNames.Count; i++)
        {
            string fileName = luafileNames[i];
            stringBuilder.Append("require(\"LuaData."+ fileName+"\")");
            stringBuilder.Append("\r\n");
        }
        
        //写入文件
        using (FileStream fileStream = new FileStream(writePath, FileMode.Create, FileAccess.Write))
        {
            using (TextWriter textWriter = new StreamWriter(fileStream, new UTF8Encoding()))
            {
                textWriter.Write(stringBuilder.ToString());
            }
        }
    }
}

导出的json如下:

[
  {
    "Lv": 1.0,
    "price": "100000,0",
    "speed": 3.0,
    "volume": 1.0,
    "icon": null
  },
  {
    "Lv": 2.0,
    "price": "1000000,0",
    "speed": 3.0,
    "volume": 2.0,
    "icon": null
  },
  {
    "Lv": 3.0,
    "price": "10000000,0",
    "speed": 3.0,
    "volume": 3.0,
    "icon": null
  },
  {
    "Lv": 4.0,
    "price": "50000000,0",
    "speed": 3.0,
    "volume": 4.0,
    "icon": null
  },
  {
    "Lv": 5.0,
    "price": "50000000,1",
    "speed": 3.0,
    "volume": 5.0,
    "icon": null
  },
  {
    "Lv": 6.0,
    "price": "50000000,2",
    "speed": 3.0,
    "volume": 6.0,
    "icon": null
  },
  {
    "Lv": 7.0,
    "price": "50000000,3",
    "speed": 3.0,
    "volume": 7.0,
    "icon": null
  },
  {
    "Lv": 8.0,
    "price": "50000000,4",
    "speed": 3.0,
    "volume": 8.0,
    "icon": null
  },
  {
    "Lv": 9.0,
    "price": "50000000,5",
    "speed": 3.0,
    "volume": 9.0,
    "icon": null
  },
  {
    "Lv": 10.0,
    "price": "50000000,6",
    "speed": 3.0,
    "volume": 10.0,
    "icon": null
  }
]

lua代码如下

local IdleTruckLv = {
        {
            Lv = 1,
            price = "100000,0",
            speed = 3,
            volume = 1,
            icon = nil,
        },
        {
            Lv = 2,
            price = "1000000,0",
            speed = 3,
            volume = 2,
            icon = nil,
        },
        {
            Lv = 3,
            price = "10000000,0",
            speed = 3,
            volume = 3,
            icon = nil,
        },
        {
            Lv = 4,
            price = "50000000,0",
            speed = 3,
            volume = 4,
            icon = nil,
        },
        {
            Lv = 5,
            price = "50000000,1",
            speed = 3,
            volume = 5,
            icon = nil,
        },
        {
            Lv = 6,
            price = "50000000,2",
            speed = 3,
            volume = 6,
            icon = nil,
        },
        {
            Lv = 7,
            price = "50000000,3",
            speed = 3,
            volume = 7,
            icon = nil,
        },
        {
            Lv = 8,
            price = "50000000,4",
            speed = 3,
            volume = 8,
            icon = nil,
        },
        {
            Lv = 9,
            price = "50000000,5",
            speed = 3,
            volume = 9,
            icon = nil,
        },
        {
            Lv = 10,
            price = "50000000,6",
            speed = 3,
            volume = 10,
            icon = nil,
        },
}
_G.IdleTruckLv = IdleTruckLv
setmetatable(_G,IdleTruckLv)
return IdleTruckLv

Demo下载地址:https://github.com/h1014179971/ExcelDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350

推荐阅读更多精彩内容