Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(3)

本文仅作笔记学习和分享,不用做任何商业用途

本文包括但不限于unity官方手册,unity唐老狮等教程知识,如有不足还请斧正​​

Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(2) (*****生成数据结构类的方式特别有趣****)-CSDN博客

做完了数据结构类,该做一个存储类了,也就是生成一个字典类(只是声明)

实现和上一节的数据结构类的方式大同小异,所以不再赘述,其中标有y的是今天这一节的代码,仅作参考

cs 复制代码
using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ExcelTools 
{
    // 表格文件夹的路径
    private static string Excel_Path = Application.dataPath + "/Excel/";

    //数据结构类脚本存储
    private static string Data_Class_Path = Application.dataPath + "/Scripts/ExcelData/DataClass/";

    //容器类脚存储
    private static string Data_Container_Path = Application.dataPath + "/Scripts/ExcelData/Container/";

    // 生成和读取表格的方法
    [MenuItem("Tool/GenerateExcel")]
    private static void GenerateExcelInfo()
    {
        // 创建或返回表格文件夹的路径
        DirectoryInfo directoryInfo = Directory.CreateDirectory(Excel_Path);
        // 获取文件夹中的所有文件
        FileInfo[] fileInfo = directoryInfo.GetFiles();
        // 数据表集合
        DataTableCollection dataTableCollection;

        for (int i = 0; i < fileInfo.Length; i++)
        {
            // 筛选出扩展名为 .xlsx 或 .xls 的文件
            if (fileInfo[i].Extension != ".xlsx" && fileInfo[i].Extension != ".xls")
                continue;

            // 打开文件流读取表格
            using (FileStream fs = fileInfo[i].Open(FileMode.Open, FileAccess.Read))
            {
                // 使用 IExcelDataReader 读取表格数据
                IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                dataTableCollection = excelDataReader.AsDataSet().Tables; // 转换为数据表集合
                fs.Close();
            }

            // 输出每个工作表的名称
            foreach (DataTable temp in dataTableCollection)
            {
                Debug.Log(temp.TableName);

                //生成数据结构类
                GenerateExcelDataClass(temp);
                //生成数据容器类
                GenerateExcelContainer(temp);
            }
           

        }
    }

    //生成数据结构类的方法-x
    private static void GenerateExcelDataClass(DataTable dataTable)
    {
       DataRow dataRowName= GetVariableNameRow(dataTable);
       DataRow dataRowType= GetVariableDataTypeRow(dataTable);
        //判断存储该脚本的路径是否为空
        if(!Directory.Exists(Data_Class_Path))
            Directory.CreateDirectory(Data_Class_Path);

        string str = "public class " + dataTable.TableName.ToString()+"\n{\n";

        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            str += "  public " + dataRowType[0].ToString() + " " + dataRowName[i].ToString() + ";" + "\n";
        }

        str += "}";

        ///dataTable.TableName.ToString() =类名,".cs"=后缀名,str=内容
        File.WriteAllText(Data_Class_Path+dataTable.TableName.ToString()+".cs", str);

    }
    //生成容器类的方法-y
    private static void GenerateExcelContainer(DataTable dataTable)
    {
        int keyIndex = GetKey(dataTable);
        //得到该key所代表的字段类型
        DataRow keyForType = GetVariableDataTypeRow(dataTable);
        //检查路径
        if (!Directory.Exists(Data_Container_Path))
            Directory.CreateDirectory(Data_Container_Path);

        //拼接字符串
        string str = "using System.Collections.Generic;\n";

        str += "public class " + dataTable.TableName.ToString() + "Container"+"\n{\n";
        
        str += "    ";
        str += "  " + "public " + "Dictionary<" + keyForType[keyIndex] + "," + dataTable.TableName.ToString() + "> ";
        str += "dataDic = new Dictionary" + "<" + keyForType[keyIndex] + "," + dataTable.TableName.ToString() + " >();\n";

        str += "}";

        //写入文件
        ///dataTable.TableName.ToString() =类名,".cs"=后缀名,str=内容
        File.WriteAllText(Data_Container_Path + dataTable.TableName.ToString() + "Container.cs", str);
    }

    //获取表中行字段名字的方法-x
    private static DataRow GetVariableNameRow(DataTable dataTable)
    { 
    return dataTable.Rows[0];   //索引可修改
    }
    //获取表中行字段数据类型的方法-x
    private static DataRow GetVariableDataTypeRow(DataTable dataTable)
    {
        return dataTable.Rows[1];//索引可修改
    }

    //获取key的索引-y
    private static int GetKey(DataTable dataTable)
    { 
     DataRow keyRow =dataTable.Rows[2];//key所在的行,此处索引可改
        //然后找到key所在的列,也就是具体单元格的位置
        for (int i = 0; i<dataTable.Columns.Count ; i++)
        {
            if (keyRow[i].ToString() == "Key")//通过keyRow[i]得到第三行每一列的值
                return i;    
        }
        return 0;
    }
}

结果:

相关推荐
唐青枫1 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20107 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
SmalBox15 小时前
【光照】[自发光Emission]以UnityURP为例
unity·渲染
葡萄城技术团队20 小时前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
mudtools20 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
SmalBox2 天前
【光照】Unity中的[经验模型]
unity·渲染
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
萘柰奈2 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity