本文仅作笔记学习和分享,不用做任何商业用途
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;
}
}
结果: