这个小工具是我对 Excel 插件 API 中已有的内容进行的二次封装。
如有错误,可以在本贴中反馈错误信息。
若有其它 Excel 读取需求,也可以在本贴中留言。
0. 导入Excel插件
1. 准备一张Excel表,用于读取Excel信息
2. 写一个Inspector编辑器功能,用于在编辑器中调用API
Inspector功能参考文档:https://blog.csdn.net/qq_41225779/article/details/127031940
(1)首先要有一个特性类
[AttributeUsage(AttributeTargets.Method)]
public class InspectorButtonAttribute : PropertyAttribute
{
public readonly string Name;
public InspectorButtonAttribute()
{
}
public InspectorButtonAttribute(string name)
{
Name = name;
}
}
(2)其次为特性赋予编辑器功能
[CustomEditor(typeof(MonoBehaviour), true)]
[CanEditMultipleObjects]
public class InspectorButton : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
//得到Mono脚本
var mono = target as MonoBehaviour;
if (mono == null)
return;
//使用反射获取所有特性上的方法
var methods = mono.GetType()
.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static
).Where(method =>
Attribute.IsDefined(method, typeof(InspectorButtonAttribute))
).ToArray();
//为特性上的方法画按钮
foreach (var method in methods)
{
var attr = method.GetCustomAttribute<InspectorButtonAttribute>();
DrawButton(method, attr.Name);
}
}
private void DrawButton(MethodInfo methodInfo, string methodName)
{
//名称为空,则按钮名为方法名
if (string.IsNullOrEmpty(methodName))
methodName = methodInfo.Name;
EditorGUILayout.BeginHorizontal();
{
if (GUILayout.Button(methodName, GUILayout.ExpandWidth(true)))
{
foreach (var targetObj in targets)
{
var mono = targetObj as MonoBehaviour;
if (mono == null)
continue;
//普通方法和协程方法的执行方式
var val = methodInfo.Invoke(mono, new object[] { });
if (val is IEnumerator coroutine)
mono.StartCoroutine(coroutine);
else if (val != null)
Debug.Log($"{methodName}调用结果: {val}");
}
}
}
EditorGUILayout.EndHorizontal();
}
}
(3)特性的使用
public class InspectorTest : MonoBehaviour
{
[InspectorButton]
public void TestFunction()
{
print("执行按钮名为函数名的方法");
}
[InspectorButton("测试方法")]
public void TestFunction2()
{
print("执行按钮名为参数名的方法");
}
}
【2】在Hierarchy创建一个空物体,并挂载测试用的脚本
API | 作用 |
DataSet LoadExcelFile(string path, string fileName) | 获取指定路径下Excel文件 |
DataTable LoadTable(string path, string fileName, string tableName) | 获取Excel文件中指定Excel表 |
DataTable[] LoadAllTables(string path, string fileName) | 获取Excel文件中所有Excel表 |
string[] GetRowInfos(DataTable table, int index) | 获取指定行的数据 |
string[] GetColumnInfos(DataTable table, int index) | 获取指定列的数据 |
string[,] GetInfos2(DataTable table) | 以二维数组形式,获取所有数据 |
string[] GetInfos1(DataTable table) | 以一维数组形式,获取所有数据 |
static string[] GetRowLastInfo(DataTable table, int index) | 获取Excel表中指定行之后的所有数据 |
一、获取Excel信息
1. 获取Excel文件信息
[InspectorButton("获取Excel文件信息")]
private void GetExcelFileInfo()
{
//通过DataSet可以获取Excel文件中的所有的表信息
DataSet data = ExcelTool.LoadExcelFile(Application.streamingAssetsPath, "StringTable");
for (int i = 0; i < data.Tables.Count; i++)
{
print("表名:" + data.Tables[i].TableName);
print("行数:" + data.Tables[i].Rows.Count);
print("列数:" + data.Tables[i].Columns.Count);
}
}
2.获取Excel表信息
[InspectorButton("获取Excel表信息")]
private void GetExcelTableInfo()
{
// 通过DataTable可以获取一张表中的信息
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
print("行数:" + table.Rows.Count);
print("列数:" + table.Columns.Count);
}
二、按行列获取数据
1. 获取某行数据
使用注意
Excel表中的第2行的索引,为数组中的第1个下标
即:想要获取某行数据,则下标索引为"行数-1″
使用时要理清行数与下标的关系
[InspectorButton("获取第二行Excel表数据")]
private void GetRowInfo()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[] rowInfos = ExcelTool.GetRowInfos(table, 1);
foreach (var info in rowInfos)
{
print(info);
}
}
2. 获取某列数据
[InspectorButton("获取第一列Excel表数据")]
private void GetColumnInfo()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[] columnInfos = ExcelTool.GetColumnInfos(table, 0);
foreach (var info in columnInfos)
{
print(info);
}
}
三、遍历Excel表
1. 以一维数组的形式遍历Excel表
(1)获取表中的所有数据
[InspectorButton("以一维数组形式 遍历Excel表")]
private void ForeachTable()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[] infos = ExcelTool.GetInfos1(table);
foreach (var info in infos)
{
print(info);
}
}
(2)获取某行之后的所有数据
[InspectorButton("获取第二行之后的所有数据")]
private void GetRowLastInfo()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[] rowLastInfos = ExcelTool.GetRowLastInfo(table, 1);
foreach (var info in rowLastInfos)
{
print(info);
}
}
2. 以二维数组的形式遍历Excel表
(1)横向遍历
[InspectorButton("以二维数组形式 横向遍历Excel表")]
private void ForeachHorizontal()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[,] infos = ExcelTool.GetInfos2(table);
for (int i = 0; i < infos.GetLength(0); i++)
{
for (int j = 0; j < infos.GetLength(1); j++)
{
print(infos[i, j]);
}
}
}
(2)纵向遍历
[InspectorButton("以二维数组形式 纵向遍历Excel表")]
private void ForeachVertical()
{
DataTable table = ExcelTool.LoadTable(Application.streamingAssetsPath, "StringTable", "RoleName");
string[,] infos = ExcelTool.GetInfos2(table);
for (int i = 0; i < infos.GetLength(1); i++)
{
for (int j = 0; j < infos.GetLength(0); j++)
{
print(infos[j, i]);
}
}
}
这个小工具是我对Excel插件API中已有的内容进行的二次封装
如有错误,可以在本贴中反馈错误信息
若有其它Excel读取需求,也可以在本贴中留言
分类 | 代码 |
特性代码 | |
编辑器代码 | |
Excel代码 | |
Excel测试代码 | |
Excel样例 | |