当前位置:   article > 正文

UNITY--读取Excel的几种方式_unity 读取excel

unity 读取excel

目录

一.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.使用示例

  1. using System.Data;
  2. using System.IO;
  3. using Excel;
  4. using UnityEngine;
  5. public class ExcelTool : MonoBehaviour
  6. {
  7. void Start()
  8. {
  9. ReadExcel("/cook.xlsx");
  10. }
  11. public void ReadExcel(string xmlName)
  12. {
  13. FileStream stream = File.Open(Application.dataPath + xmlName, FileMode.Open, FileAccess.Read, FileShare.Read);
  14. //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
  15. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
  16. DataSet result = excelReader.AsDataSet();
  17. if (stream != null)
  18. {
  19. stream.Close();
  20. }
  21. int[] counts = GetCount(result.Tables[0]);
  22. int rows = counts[0];
  23. int columns = counts[1];
  24. Debug.LogError("row:" + rows + "...col:" + columns);
  25. for (int i = 0; i < rows; i++)
  26. {
  27. for (int j = 0; j < columns; j++)
  28. {
  29. Debug.LogError(result.Tables[0].Rows[i][j].ToString());
  30. }
  31. }
  32. }
  33. private int[] GetCount(DataTable dt)
  34. {
  35. int i = dt.Rows.Count;
  36. for (int m = 0; m < dt.Rows.Count; m++)
  37. {
  38. if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
  39. {
  40. i = m;
  41. break;
  42. }
  43. }
  44. int j = dt.Columns.Count;
  45. for (int n = 0; n < dt.Columns.Count; n++)
  46. {
  47. if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
  48. {
  49. j = n;
  50. break;
  51. }
  52. }
  53. return new int[] { i, j };
  54. }
  55. }

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文件,并保存到指定位置 

  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class ExcelReadEditor : Editor
  6. {
  7. [MenuItem("Excel/批量读表")]
  8. public static void ReadAllExcel()
  9. {
  10. Type typeInfo = typeof(ExcelReadEditor);
  11. MethodInfo[] methodInfo = typeInfo.GetMethods();
  12. foreach (MethodInfo mInfo in methodInfo)
  13. {
  14. if (mInfo.ToString().StartsWith("Void Create"))
  15. {
  16. mInfo.Invoke(null, null);
  17. }
  18. }
  19. Debug.Log("读取全部表成功");
  20. }
  21. [MenuItem("Excel/读取Decoration数据")]
  22. public static void CreateChapterData()
  23. {
  24. GetLocalData<LocalSheBei, Entity_SheBei>("SheBei");
  25. Debug.Log("读取Decoration数据成功");
  26. }
  27. //泛型创建 保存本地数据
  28. public static void GetLocalData<T, W>(string fileName) where T : LocalDataBase<W>
  29. {
  30. T t = CreateInstance<T>();
  31. string filePath = Application.dataPath + "/Excel/" + "cook-en" + ".xlsx";
  32. //string filePath = Path.GetFullPath(".") + "\\Excel\\" + "cook-en" + ".xlsx";
  33. t.data = ExcelRead.GetExcelData<W>(filePath, fileName);
  34. if (t.data == null) Debug.LogError("数据读取错误");
  35. string savePath = "Assets/Excel/Data/EN/" + fileName + ".asset";
  36. AssetDatabase.CreateAsset(t, savePath);
  37. AssetDatabase.SaveAssets();
  38. AssetDatabase.Refresh();
  39. GetLocalDataCH<T, W>(fileName);
  40. }
  41. public static void GetLocalDataCH<T, W>(string fileName) where T : LocalDataBase<W>
  42. {
  43. T t = CreateInstance<T>();
  44. string filePath = Application.dataPath + "/Excel/" + "cook-ch" + ".xlsx";
  45. //string filePath = Path.GetFullPath(".") + "\\Excel\\" + "cook-ch" + ".xlsx";
  46. t.data = ExcelRead.GetExcelData<W>(filePath, fileName);
  47. if (t.data == null) Debug.LogError("数据读取错误");
  48. string savePath = "Assets/Excel/Data/CH/" + fileName + ".asset";
  49. AssetDatabase.CreateAsset(t, savePath);
  50. AssetDatabase.SaveAssets();
  51. AssetDatabase.Refresh();
  52. }
  53. }

⚠️注意:要先在指定位置创建好父文件夹,此脚步要放在Editor文件夹下

2.3创建ExcelRead脚本,读取Excel内容

  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.Data;
  4. using System.IO;
  5. using Excel;
  6. public class ExcelRead
  7. {
  8. public static T[] GetExcelData<T>(string filePath, string name)
  9. {
  10. switch (name)
  11. {
  12. case "Entity_SheBei":
  13. return GetEntity_SheBei(filePath) as T[];
  14. //。。。
  15. }
  16. return null;
  17. }
  18. //设备
  19. private static Entity_SheBei[] GetEntity_SheBei(string filePath)
  20. {
  21. int columnNum = 0, rowNum = 0;
  22. DataRowCollection collect = ReadExcel(filePath, ref columnNum, ref rowNum, 1);
  23. List<Entity_SheBei> list = new List<Entity_SheBei>();
  24. for (int i = 1; i < rowNum; i++)
  25. {
  26. if (string.IsNullOrEmpty(collect[i][0].ToString()) || string.IsNullOrEmpty(collect[0][i].ToString())) break;
  27. Entity_SheBei data = new Entity_SheBei();
  28. data.name = collect[i][0].ToString();
  29. for (int j = 1; j < columnNum; j++)
  30. {
  31. //Debug.LogError(collect[i][j].ToString());
  32. data.parts.Add(collect[i][j].ToString());
  33. }
  34. list.Add(data);
  35. }
  36. return list.ToArray();
  37. }
  38. /// <summary>
  39. /// 读取excel文件内容
  40. /// </summary>
  41. /// <param name="filePath">文件路径</param>
  42. /// <param name="columnNum">行数</param>
  43. /// <param name="rowNum">列数</param>
  44. /// <param name="table">第几张表</param>
  45. /// <returns></returns>
  46. static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum, int table = 0)
  47. {
  48. FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  49. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  50. DataSet result = excelReader.AsDataSet();
  51. //Tables[0] 下标0表示excel文件中第一张表的数据
  52. int[] counts = GetRowColumns(result.Tables[table]);
  53. //columnNum = result.Tables[table].Columns.Count;
  54. //rowNum = result.Tables[table].Rows.Count;
  55. rowNum = counts[0];
  56. columnNum = counts[1];
  57. Debug.LogError("行:" + rowNum + "...列:" + columnNum);
  58. return result.Tables[table].Rows;
  59. }
  60. private static int[] GetRowColumns(DataTable dt)
  61. {
  62. int i = dt.Rows.Count;
  63. for (int m = 0; m < dt.Rows.Count; m++)
  64. {
  65. if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
  66. {
  67. i = m;
  68. break;
  69. }
  70. }
  71. int j = dt.Columns.Count;
  72. for (int n = 0; n < dt.Columns.Count; n++)
  73. {
  74. if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
  75. {
  76. j = n;
  77. break;
  78. }
  79. }
  80. return new int[] { i, j };
  81. }
  82. }

2.4 创建数据存储脚本

基类:

  1. using UnityEngine;
  2. public class LocalDataBase<T> : ScriptableObject
  3. {
  4. public T[] data;
  5. }

 子类:

  1. public class LocalSheBei : LocalDataBase<Entity_SheBei>
  2. {
  3. }

 子类对应的实体类:

  1. using System.Collections.Generic;
  2. [System.Serializable]
  3. public class Entity_SheBei
  4. {
  5. public string name;
  6. public List<string> parts = new List<string>();
  7. }

2.5  编辑器生成Asset 与属性面板详情

 

 

 

 2.6 Asset数据的使用方式

  Resources.Load方式

 也可以定义一个单例类,直接拖拽:

  1. public LocalSheBei Data_SheBei;
  2. //数据读取
  3. public void GetLocalData()
  4. {
  5. Debug.LogError(Data_SheBei.name + "..." + Data_SheBei.data.Length);
  6. }

 三:Excel文件转成json 再以json的方式读取

unity--json读取与解析

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/82021
推荐阅读
相关标签
  

闽ICP备14008679号