目录
一.DLL插件读取
1.1.Excel存放位置
1.2.使用示例
1.3.Excel格式
1.4.输出显示
1.5.所需插件
二.Excel转成Asset文件,再进行读取
2.1Excel文件存放位置
2.2 编辑模式生成Asset文件,并保存到指定位置
2.3创建ExcelRead脚本,读取Excel内容
2.4 创建数据存储脚本
2.5 编辑器生成Asset 与属性面板详情
2.6 Asset数据的使用方式
三:Excel文件转成json 再以json的方式读取
一.DLL插件读取
1.1.Excel存放位置
如图 将命名为cook的excel文件存放在Assets根目录下
1.2.使用示例
using System.Data;
using System.IO;
using Excel;
using UnityEngine;
public class ExcelTool : MonoBehaviour
{
void Start()
{
ReadExcel("/cook.xlsx");
}
public void ReadExcel(string xmlName)
{
FileStream stream = File.Open(Application.dataPath + xmlName, FileMode.Open, FileAccess.Read, FileShare.Read);
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
if (stream != null)
{
stream.Close();
}
int[] counts = GetCount(result.Tables[0]);
int rows = counts[0];
int columns = counts[1];
Debug.LogError("row:" + rows + "...col:" + columns);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Debug.LogError(result.Tables[0].Rows[i][j].ToString());
}
}
}
private int[] GetCount(DataTable dt)
{
int i = dt.Rows.Count;
for (int m = 0; m < dt.Rows.Count; m++)
{
if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
{
i = m;
break;
}
}
int j = dt.Columns.Count;
for (int n = 0; n < dt.Columns.Count; n++)
{
if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
{
j = n;
break;
}
}
return new int[] { i, j };
}
}
1.3.Excel格式
1.4.输出显示
1.5.所需插件
需导入3个dll文件到Plugins文件夹下。
分别是:Excel.dll EPPlus.dll 和 ICSharpCode.SharpZipLib.dl
插件下载https://download.csdn.net/download/lalate/87401837
⚠️注意:上述示例在安卓真机上运行是读取不到的,有解决了的同学欢迎评论补充,或者私信我
二.Excel转成Asset文件,再进行读取
2.1Excel文件存放位置
2.2 编辑模式生成Asset文件,并保存到指定位置
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class ExcelReadEditor : Editor
{
[MenuItem("Excel/批量读表")]
public static void ReadAllExcel()
{
Type typeInfo = typeof(ExcelReadEditor);
MethodInfo[] methodInfo = typeInfo.GetMethods();
foreach (MethodInfo mInfo in methodInfo)
{
if (mInfo.ToString().StartsWith("Void Create"))
{
mInfo.Invoke(null, null);
}
}
Debug.Log("读取全部表成功");
}
[MenuItem("Excel/读取Decoration数据")]
public static void CreateChapterData()
{
GetLocalData<LocalSheBei, Entity_SheBei>("SheBei");
Debug.Log("读取Decoration数据成功");
}
//泛型创建 保存本地数据
public static void GetLocalData<T, W>(string fileName) where T : LocalDataBase<W>
{
T t = CreateInstance<T>();
string filePath = Application.dataPath + "/Excel/" + "cook-en" + ".xlsx";
//string filePath = Path.GetFullPath(".") + "\\Excel\\" + "cook-en" + ".xlsx";
t.data = ExcelRead.GetExcelData<W>(filePath, fileName);
if (t.data == null) Debug.LogError("数据读取错误");
string savePath = "Assets/Excel/Data/EN/" + fileName + ".asset";
AssetDatabase.CreateAsset(t, savePath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
GetLocalDataCH<T, W>(fileName);
}
public static void GetLocalDataCH<T, W>(string fileName) where T : LocalDataBase<W>
{
T t = CreateInstance<T>();
string filePath = Application.dataPath + "/Excel/" + "cook-ch" + ".xlsx";
//string filePath = Path.GetFullPath(".") + "\\Excel\\" + "cook-ch" + ".xlsx";
t.data = ExcelRead.GetExcelData<W>(filePath, fileName);
if (t.data == null) Debug.LogError("数据读取错误");
string savePath = "Assets/Excel/Data/CH/" + fileName + ".asset";
AssetDatabase.CreateAsset(t, savePath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
⚠️注意:要先在指定位置创建好父文件夹,此脚步要放在Editor文件夹下
2.3创建ExcelRead脚本,读取Excel内容
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using System.IO;
using Excel;
public class ExcelRead
{
public static T[] GetExcelData<T>(string filePath, string name)
{
switch (name)
{
case "Entity_SheBei":
return GetEntity_SheBei(filePath) as T[];
//。。。
}
return null;
}
//设备
private static Entity_SheBei[] GetEntity_SheBei(string filePath)
{
int columnNum = 0, rowNum = 0;
DataRowCollection collect = ReadExcel(filePath, ref columnNum, ref rowNum, 1);
List<Entity_SheBei> list = new List<Entity_SheBei>();
for (int i = 1; i < rowNum; i++)
{
if (string.IsNullOrEmpty(collect[i][0].ToString()) || string.IsNullOrEmpty(collect[0][i].ToString())) break;
Entity_SheBei data = new Entity_SheBei();
data.name = collect[i][0].ToString();
for (int j = 1; j < columnNum; j++)
{
//Debug.LogError(collect[i][j].ToString());
data.parts.Add(collect[i][j].ToString());
}
list.Add(data);
}
return list.ToArray();
}
/// <summary>
/// 读取excel文件内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="columnNum">行数</param>
/// <param name="rowNum">列数</param>
/// <param name="table">第几张表</param>
/// <returns></returns>
static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum, int table = 0)
{
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
//Tables[0] 下标0表示excel文件中第一张表的数据
int[] counts = GetRowColumns(result.Tables[table]);
//columnNum = result.Tables[table].Columns.Count;
//rowNum = result.Tables[table].Rows.Count;
rowNum = counts[0];
columnNum = counts[1];
Debug.LogError("行:" + rowNum + "...列:" + columnNum);
return result.Tables[table].Rows;
}
private static int[] GetRowColumns(DataTable dt)
{
int i = dt.Rows.Count;
for (int m = 0; m < dt.Rows.Count; m++)
{
if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
{
i = m;
break;
}
}
int j = dt.Columns.Count;
for (int n = 0; n < dt.Columns.Count; n++)
{
if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
{
j = n;
break;
}
}
return new int[] { i, j };
}
}
2.4 创建数据存储脚本
基类:
using UnityEngine;
public class LocalDataBase<T> : ScriptableObject
{
public T[] data;
}
子类:
public class LocalSheBei : LocalDataBase<Entity_SheBei>
{
}
子类对应的实体类:
using System.Collections.Generic;
[System.Serializable]
public class Entity_SheBei
{
public string name;
public List<string> parts = new List<string>();
}
2.5 编辑器生成Asset 与属性面板详情
2.6 Asset数据的使用方式
Resources.Load方式
也可以定义一个单例类,直接拖拽:
文章来源:https://www.toymoban.com/news/detail-807899.html
public LocalSheBei Data_SheBei;
//数据读取
public void GetLocalData()
{
Debug.LogError(Data_SheBei.name + "..." + Data_SheBei.data.Length);
}
三:Excel文件转成json 再以json的方式读取
unity--json读取与解析文章来源地址https://www.toymoban.com/news/detail-807899.html
到了这里,关于UNITY--读取Excel的几种方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!