Unity Excel的最简单读取方式实现
2020-02-02 11:54
1891 查看
Unity版本:Unity 2018.3.4f1
准备
1、获取第三方开发包,GitHub地址:https://github.com/ExcelDataReader/ExcelDataReader
2、对下载的解决方案ExcelDataReader进行编译生成,生成相应的 dll 文件,并根据Unity的Scripting Runtime Version选择最低的版本,并将这三个库文件复制到
Assets/Plugins下
3、准备用来读取的Excel表,我准备了两个两个Excel文件,分别是对应97-2003的UserLevel.xls 和 对应2007的UserLevel.xlsx,内容自己随意定义
读取Excel表信息
直接上代码,都很简单,所以就没有添加注释
using ExcelDataReader; using System.Collections.Generic; using System.Data; using System.IO; using UnityEngine; using UnityEngine.UI; namespace ExcelData.DirectReading { public class ReadExcel : MonoBehaviour { public Text m_Message; private string excelFilePath; private Dictionary<int, List<string>> excelDataDic; // Start is called before the first frame update void Start() { excelDataDic = new Dictionary<int, List<string>>(); ReadExcelStream(0); m_Message.text += new string('-', 70) + "\r\n"; ReadExcelStream(1); } // Update is called once per frame void Update() { } private void ReadExcelStream(int _type) { excelDataDic.Clear(); if (_type == 0) { excelFilePath = Application.dataPath + "/Resources/ExcelDataReader/UserLevel.xls"; } else { excelFilePath = Application.dataPath + "/Resources/ExcelDataReader/UserLevel.xlsx"; } FileStream fileStream = File.Open(excelFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); IExcelDataReader dataReader; if (_type == 0) { #region 读取 *.xls 文件 //ExcelReaderFactory.CreateBinaryReader:读取的excel的类型为(97 - 2003 format, *.xls) dataReader = ExcelReaderFactory.CreateBinaryReader(fileStream); #endregion } else { #region 读取 *.xlsx 文件 //ExcelReaderFactory.CreateOpenXmlReader:读取的excel的类型为(2007 format, *.xlsx) dataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream); #endregion } DataSet result = dataReader.AsDataSet(); dataReader.Close(); //列 int columns = result.Tables[0].Columns.Count; //行 int rows = result.Tables[0].Rows.Count; for (int i = 0; i < rows; i++) { List<string> temp = new List<string>(); for (int j = 0; j < columns; j++) { string value = result.Tables[0].Rows[i][j].ToString(); temp.Add(value); } excelDataDic.Add(i, temp); } Dictionary<int, List<string>>.Enumerator enumerator = excelDataDic.GetEnumerator(); while (enumerator.MoveNext()) { List<string> list = enumerator.Current.Value; string info = enumerator.Current.Key.ToString() + " :"; for (int i = 0; i < list.Count; i++) { info += " " + list[i]; } m_Message.text += info + "\r\n"; } } } }
直接读取两个文件格式中的数据,运行之后,效果如下:
最后
选择调试模式发布,发布完成之后运行时发现报错
报错的原因是打包出来的程序下
Resources文件中不包含相应的Excel文件,需要人为的手动将对应的文件信息拷贝到
Resources中,再次运行时发现出现另外一个错误,错误信息如下:
通过各种百度和谷歌,在Unity官方论坛上找到一篇很早以前的文章,其中有相关的讨论,该篇文章的地址:https://answers.unity.com/questions/42955/codepage-1252-not-supported-works-in-editor-but-no.html
找到Unity的安装路径,如我的安装路径:
C:\Program Files\Unity\Editor,然后查找与
I18N相关的
dll文件,并将这些文件拷贝到
Assets/Plugins下,具体路径如下,根据自己的Unity安装路径做出对应修改即可
再次发布,并将相应的文件拷贝到相应的位置并运行,运行结果与在编辑器中的运行结果相同
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- SpringMVC+POI 实现Excel按模板方式简单导出功能
- 在 unity 中使用三种简单的方式实现实时时钟动画
- java poi 读取excel文档最简单方式
- 【Unity随笔】Unity之ugui简单背包实现与Json读取物品信息
- java利用注解实现简单的excel数据读取
- poi 读取 excel ->简单实现
- 从Excel 电子表格中读取数据并插入到数据库的简单方式
- spring.net、castle windsor、unity实现aop、ioc的方式和简单区别
- C#实现OleDb方式读取excel并在datagridview中显示
- Java实现文件读取的简单方式
- Java注解--实现简单读取excel
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- jxl操作实现导出读取excel
- Android最简单的方式实现实体类的创建(不需要Getter和Setter)
- cell高度自适应实现的最简单方式
- Unity实现大转盘的简单笔记
- 二进制数据流方式实现 个人头像的读取、上传、修改------善良公社项目
- unity 实现另一个unity软件的安装工具(4)——unity调用外部程序,三种路径方式
- java 用单态方式实现数据库配置文件读取
- Unity 读取 Excel 表格 配置 游戏 参数