赞
踩
目录
如图 将命名为cook的excel文件存放在Assets根目录下
- 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 };
- }
- }
需导入3个dll文件到Plugins文件夹下。
分别是:Excel.dll EPPlus.dll 和 ICSharpCode.SharpZipLib.dl
插件下载https://download.csdn.net/download/lalate/87401837
⚠️注意:上述示例在安卓真机上运行是读取不到的,有解决了的同学欢迎评论补充,或者私信我
- 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文件夹下
- 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 };
- }
- }
基类:
- 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>();
- }
也可以定义一个单例类,直接拖拽:
- public LocalSheBei Data_SheBei;
- //数据读取
- public void GetLocalData()
- {
- Debug.LogError(Data_SheBei.name + "..." + Data_SheBei.data.Length);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。