市面上能搜到的Excel读写插件大多封装成dll了,有些要么是仅windows端能用,有些甚至仅编辑器内才能用,对于Mac党简直苦不堪言。我曾经也找了很久Mac端的Excel读写解决方案,能搜到的竟然仅仅是雨凇15年的文章,写得挺复杂,试了也没成功。
这里找到一个Unity端的Excel读写方案,解析excel部分都是通过C#实现的,因此不存在平台问题,也就是说从各平台的编辑器到运行时都可以用。
使用方法也很简单,直接读取一个路径的excel就可以输出、修改、保存,在编写UnityEditor脚本的时候使用这些配置非常方便,用法如下:
using Excel;
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string excelPath = Application.streamingAssetsPath + "/demo.xlsx";
Cells sheet = new Cells(excelPath);
string value1 = sheet[0, 0];
string value2 = sheet[1, 1];
string value3 = sheet[1, 2];
Debug.Log("value1:" + value1);
Debug.Log("value2:" + value2);
Debug.Log("value3:" + value3);
Debug.Log(sheet.GetRows() + "行");
Debug.Log(sheet.GetCollumns() + "列");
sheet[0, 0] = "test1";
sheet[0, 1] = "1";
sheet[1, 1] = "2";
////自动扩充表格到10x10
//sheet.Cells[10, 10] = "1";
sheet.Save();
Debug.Log("sheet[0, 0]:" + sheet[0, 0]);
Debug.Log("sheet[0, 1]:" + sheet[0, 1]);
Debug.Log("sheet[1, 1]:" + sheet[1, 1]);
}
// Update is called once per frame
void Update()
{
}
}
链接: https://pan.baidu.com/s/1NRgC9N7qt_3p_KszAAoQxw 密码: im10
--来自百度网盘超级会员V3的分享
不过excel的读写还是偏慢的,建议还是使用json的数据格式
另外,用外部的excel直接导入有可能会出现行列读取不正确的问题,尽可能复制本项目的excel操作(因为excel版本太多了)。
网友评论