需要用到Excel.dll和ICSharpCode.SharpZipLib.dll
表格示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using Excel;
using System.Reflection;
using System.Reflection.Emit;
using System;
public class ExcelTool : Editor
{
public class ExcelClassData
{
public string className; //类名
public string[] infos; //注释
public string[] types; //类型
public string[] propertyName; //属性名
public List<string[]> datas; //数据
}
//excel文件夹
const string EXCEL_PATH = "ExcelData/Excel";
[MenuItem("ExcelTool/生成数据")]
static void ExcelToData()
{
//所有xlsx表格
string[] files = Directory.GetFiles(Application.dataPath + "/" + EXCEL_PATH, "*.xlsx");
//Debug.Log(files.Length);
//所有数据信息
List<ExcelClassData> allClassDatas = new List<ExcelClassData>();
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
if (!excelReader.IsValid)
{
Debug.Log("读取excel失败" + file);
continue;
}
//构建数据
ExcelClassData exdata = new ExcelClassData();
exdata.className = excelReader.Name;
exdata.datas = new List<string[]>();
int line = 1;
while (excelReader.Read())
{
//一行数据
string[] strLineDatas = new string[excelReader.FieldCount];
for (int j = 0; j < strLineDatas.Length; j++)
{
strLineDatas[j] = excelReader.GetString(j);
}
////空行处理
//if (string.IsNullOrEmpty(strLineDatas[0]))
//{
// continue;
//}
//注释行
if (line == 2)
{
exdata.infos = strLineDatas;
}
//类型行
if (line == 3)
{
exdata.types = strLineDatas;
}
//属性名行
if (line == 4)
{
exdata.propertyName = strLineDatas;
}
//数据行
if(line > 4)
{
exdata.datas.Add(strLineDatas);
}
line++;
}
allClassDatas.Add(exdata);
}
//写出数据和脚本操作
Writer(allClassDatas);
Debug.Log("----->excel datas trans finish!");
AssetDatabase.Refresh();
}
static void Writer(List<ExcelClassData> exDataList)
{
#region//---json---
//AssemblyName assemblyName = new AssemblyName("dynamicAssembly");
//AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
//ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
//for (int i = 0; i < exDataList.Count; i++)
//{
// ExcelClassData exData = exDataList[i];
// //定义类型
// TypeBuilder typeBuilder = moduleBuilder.DefineType(exData.className, TypeAttributes.Public);
// //定义属性
// for (int j = 0; j < exData.types.Length; j++)
// {
// typeBuilder.DefineField(exData.propertyName[j], GetType(exData.types[j]), FieldAttributes.Public);
// }
// //t
// Type t = typeBuilder.CreateType();
// List<object> allObjList = new List<object>();
// for (int j = 0; j < exData.datas.Count; j++)
// {
// //一行数据
// string[] strDatas = exData.datas[j];
// //反射实例
// object obj = Activator.CreateInstance(t);
// for (int k = 0; k < exData.types.Length; k++)
// {
// //设置属性值
// FieldInfo fieldInfo = t.GetField(exData.propertyName[k]);
// object value = GetValue(exData.types[k], strDatas[k]);
// fieldInfo.SetValue(obj, value);
// }
// allObjList.Add(obj);
// }
// string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(allObjList, Newtonsoft.Json.F

本文介绍了一款用于Unity项目的Excel数据导入工具,该工具能够将Excel文件转换为Unity可读的JSON或字节数据,并自动生成对应的C#脚本来解析这些数据。此工具通过使用Excel.dll和ICSharpCode.SharpZipLib.dll库实现,适用于游戏开发和数据管理。

948

被折叠的 条评论
为什么被折叠?



