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

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

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

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

本节内容

实现目标

通过已经得到的Excel表格数据,生成对应类对象(不赋值),一张表就是一个对象,其中包含了如下的字段

就像这样子

实现思路

上节已经获取了一个个单元格,那直接利用其中的字段进行字符串拼接

然后把这个字符串通过文件处理存到一个脚本文件之中就行了,自然就识别成了类对象了

实现过程

生成数据结构类的脚本的路径

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

获得表中的字段名和字段类型

cs 复制代码
   //生成数据结构类的方法
   private static void GenerateExcelDataClass(DataTable dataTable)
   {
       GetVariableNameRow(dataTable);
       GetVariableDataTypeRow(dataTable);


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

确认或创建存储数据结构脚本的路径

cs 复制代码
  DataRow dataRowName= GetVariableNameRow(dataTable);
  DataRow dataRowType= GetVariableDataTypeRow(dataTable);
   //判断存储该脚本的路径是否为空
   if(!Directory.Exists(Data_Class_Path))
       Directory.CreateDirectory(Data_Class_Path);

狠狠拼接字符串

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

  str += "}";

存入先前的路径中,生成脚本

Windows.File-WriteAllBytes - Unity 脚本 API

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

最终结果

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/";

    // 生成和读取表格的方法
    [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);
            }
           

        }
    }

    //生成数据结构类的方法
    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);

    }
    //获取表中行字段名字的方法
    private static DataRow GetVariableNameRow(DataTable dataTable)
    { 
    return dataTable.Rows[0];   //索引可修改
    }
    //获取表中行字段数据类型的方法
    private static DataRow GetVariableDataTypeRow(DataTable dataTable)
    {
        return dataTable.Rows[1];//索引可修改
    }
}
相关推荐
张晓~183399481211 天前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
lrh30251 天前
Custom SRP - Point and Spot Lights
unity·srp·render pipeline
almighty271 天前
C# DataGridView表头自定义设置全攻略
数据库·c#·winform·datagridview·自定义表头
绀目澄清1 天前
unity UGUI 鼠标画线
unity·计算机外设·游戏引擎
星空的资源小屋1 天前
Digital Clock 4,一款免费的个性化桌面数字时钟
stm32·单片机·嵌入式硬件·电脑·excel
Magnum Lehar1 天前
3d wpf游戏引擎的导入文件功能c++的.h实现
3d·游戏引擎·wpf
作孽就得先起床1 天前
unity pcd 二进制版 简单显示文件对象(单色)
unity·游戏引擎
arbboter1 天前
【自动化】深入浅出UIAutomationClient:C#桌面自动化实战指南
运维·c#·自动化·inspect·uiautomation·uia·桌面自动化
文弱书生6561 天前
5.后台运行设置和包设计与实现
服务器·开发语言·c#
大飞pkz1 天前
【设计模式】题目小练2
开发语言·设计模式·c#·题目小练