在项目开发制作中经常回读取excel表格数据,其实有多种实现方式,FlexReader是比较方便快捷读取Excel文件内容,支持CSV,XLSX等格式。支持所以平台上运行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using FlexFramework.Excel;
using UnityEngine.Networking;
using System.Linq;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//开始读取表格数据
// LoadExcel();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
LoadExcel();
print("load");
}
}
public delegate void DownloadHandler(byte[] bytes);
//加载excel
public void LoadExcel()
{
// StreamingAssets
StartCoroutine(LoadFileAsync("checkdata.xlsx", bytes =>
{
var book = new WorkBook(bytes);
//Populate(book[0]);
SetDataRow(book[0]);
//this.modal.Show("Loaded XLSX!");
}));
}
//读取列表和列表
void SetDataRow(IEnumerable<Row> rows)
{
int index = -1;
int count = rows.Count(r => !r.IsEmpty());
if (count == 0)
return;
//将二维数字存到列表 ,通过行列读取
List<Row> rowData = new List<Row>(rows);
for (int j = 1; j < rowData.Count; j++)//行
{
for (int i = 0; i < rowData[j].Count; i++)//列
{
Debug.Log(rowData[j][i].Text);
}
}
}
//异步加载
private IEnumerator LoadFileAsync(string path, DownloadHandler handler)
{
// streaming assets should be loaded via web request
// on WebGL/Android platforms, this folder is in a compressed directory
var url = Path.Combine(Application.streamingAssetsPath, path);
using (var req = UnityWebRequest.Get(url))
{
yield return req.SendWebRequest();
var bytes = req.downloadHandler.data;
handler(bytes);
}
}
}
插件:可以上Asset Store上直接搜FlexReader
下载地址:链接:https://pan.baidu/s/1mLTu2ymQY0Wmvpzb0DQ5iw 密码:j9m6
更多推荐
Unity开发——读取Excel表格数据
发布评论