Unity读取Excel文件需要引入 dll 链接库:
同时添加 Unity 安装目录下的以下 dll 库文件:
坑1:I18N系列的 dll 库文件的添加主要是解决 编辑器下可以读取,打包.exe程序后,无法读取的情况
读取示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using System.IO;
using Excel;
public class DoExcel {
public static DataSet ReadExcel(string path)
{
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
// CreateOpenXmlReader用于读取Excel2007版本及以上的文件
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
excelReader.Close();
return result;
}
public static List Load(string path)
{
List _data = new List();
DataSet resultds = ReadExcel(path);
int column = resultds.Tables[0].Columns.Count;
int row = resultds.Tables[0].Rows.Count;
Debug.LogWarning(column + " " + row);
for(int i=1;i|
坑2:这里还有一个小问题需要注意下,就是关于Excel文件,有的时候Excel文件内的数据有效行可能很少,比如不到10行,但是Excel文件中右侧导航条会很长,拉到底会有一个巨大的行数,这里建议是将无效行的清除掉,否则Unity在读取时,会将这些无效行全部读取进去,这样会造成程序直接卡死