在项目开发过程当中,常常会用到大量的可编辑的数据,而这些数据使用Json,XML等形式存储又比较麻烦 PS:对于不懂电脑的客户来讲彻底就是看天书,后期编辑也比较费事。因此就有了使用Excel表格进行数据的存储和读取。好比:人员名单(姓名,班级,学号等信息)。因此本篇文章就分享一下如何使用Unity读写Excel表格。markdown
此篇文章中使用的是Unity2019.4.17版本,VS2017。须要引入EPPlus.dll,Excel.dll 和ICSharpCode.SharpZipLib库文件。测试
下图是咱们要读取的数据this
读取Excel须要用到Excel.dll 和ICSharpCode.SharpZipLib库文件。将其放到Plugins文件夹下。 首先须要引入命名空间spa
使用以下脚本,首先加载Excel文件excel
FileStream fileStream = File.Open(Application.streamingAssetsPath + "/"+ 表格名, FileMode.Open, FileAccess.Read);
code
上一句代码为Unity的StreamingAssets目录下的.xlsx文件的路径:Application.streamingAssetsPath + "/表格名.xlsx"orm
读取文件以后要对文件进行相似实例化解析操做,ip
IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
开发
将全部数据所有读取出来字符串
DataSet result = excelDataReader.AsDataSet();
接下来获取表格的行数和列数
// 获取表格有多少列
int columns = result.Tables[0].Columns.Count;
// 获取表格有多少行
int rows = result.Tables[0].Rows.Count;
复制代码
接下来咱们要把读取到的每一行每一列数据进行整合,经过for循环,将每一行的数据进行记录,首先读取每一行,而后在读取这一行中的每一列,从而得到整行数据信息。具体代码信息以下
// 根据行列依次打印表格中的每一个数据
List<string> excelDta = new List<string>();
//第一行为表头,不读取。没有表头从0开始
for (int i = 1; i < rows; i++)
{
value = null;
all = null;
for (int j = 0; j < columns; j++)
{
// 获取表格中指定行指定列的数据
value = result.Tables[0].Rows[i][j].ToString();
if (value == "")
{
continue;
}
all = all + value + "|";
}
if (all != null)
{
//print(all);
excelDta.Add(all);
}
}
复制代码
在这里,每一行中每一列数据是使用“|进行分割的,接下来要作的是将读取到的每一行数据存到定义好的类中。方便之后好调用。 首先定义数据类型,要有ID(序号),姓名,班级,学号,电话。 代码以下:
public class PlayerInfo
{
// 序号
public string ID { get; set; }
// 姓名
public string Name { get; set; }
// 学号
public string Number { get; set; }
// 班级
public string Class { get; set; }
/// 手机号
public string Mobile { get; set; }
public PlayerInfo(string id, string name,string class,string number,string mobile)
{
this.ID = id;
this.Name = name;
this.Number = number;
this.Class = class;
this.Mobile = mobile;
}
}
复制代码
接下来遍历读取到的数据进行处理而后存储到List中,首先使用Split("|"),读数据进行截取,组成一个是字符串数据,而后在new一个PlayerInfo,传入参数,最后将new的PlayerInfo存入List中。
string[] item = data_1[i].Split('|');
复制代码
PlayerInfo playerinfo = new PlayerInfo(item[0], item[1], item[2], mobile_Enc);
复制代码
List<PlayerInfo> Players = new List<PlayerInfo>();
Players.Add(playerinfo);
复制代码
到此,全部数据也就所有读取出来了,能够再控制台进行一下打印,输入结果以下,证实数据读取成功
最终结果是要在上述表格中添加一列,以下所示
首先,须要引入EPPlus.dll库文件,其次,须要引用命名空间:using OfficeOpenXml;以便对Excel表格进行写入 一样,首先须要打开文件,确认文件是否存在,不存在须要自动建立一个文件
//文件路径
string path = Application.streamingAssetsPath + "/表格名.xlsx";
FileInfo newFile = new FileInfo(path);
//判断文件是否中存在
if (!newFile.Exists)
{
//建立一个新的excel文件
newFile = new FileInfo(path);
}
复制代码
接下来进行文件的写入,在这以前,咱们须要要写入的文件存入一个list中,一样使用"|"对每一列数据进行分割,呀添加的数据为
"3|王五|20210103|三年一班|11111111111"
经过ExcelPackage打开文件
using (ExcelPackage package = new ExcelPackage(newFile))
而后进行数据分割,存储,这里之因此使用一整串字符串是为了之后存储数据较多的时候,能够直接进行遍历,省的一点点进行添加
string[] messages = newList[i].Split('|');
string itemId = messages[0];
string itemName = messages[1];
string itemNumber = messages[2];
string imageClass = messages[3];
string imageMobile = messages[4];
//添加第四行数据
worksheet.Cells["A4"].Value = itemId;
worksheet.Cells["B4"].Value = itemName;
worksheet.Cells["C4"].Value = itemNumber;
worksheet.Cells["D4"].Value = imageClass;
worksheet.Cells["E4"].Value = imageMobile;
复制代码
最后,对表格进行保存
package.Save();
经测试发现以下几个问题: 一、在打包以后或者没打包的时候就会出现读取不到excel数据,须将
Unity\Editor\Data\Mono\lib\mono\unity目录下的一系列i18n相关dll导入项目Plugins文件夹中。 二、若是xlsx文件的后缀为.xlsx,读取的代码应该为
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
若使用CreateBinaryReader读取,则在excelReader.AsDataSet();会报错NullReferenceException: Object reference not set to an instance of an object
三、若是xlsx文件的后缀为.xls,读取的代码应该为
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
若使用CreateOpenXmlReader读取,则在CreateOpenXmlReader处会报错ArgumentNullException: Value cannot be null.
总体项目案例会在后期整理好以后分享给你们,若有错误之处还请多多指出。