在这篇博文里面
【Unity】读取Excel表格(.xlsx或者.xls)_DAGUNIANGZHOU的博客-CSDN博客
用了Excel的库解析了Excel表格,但是由于打包exe需要将编辑器的I18n的库导入工程,win打包出来才不会解析不出来。
但是由于我的打包方式是IL2Cpp,打包会有冲突,Mono 不会有冲突。
如果你是Mono的模式,上面的博文是没有问题的。
所以我换了一种读取Excel的库。
包的链接:
https://download.csdn.net/download/weixin_46472622/87257047
解析示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using FlexFramework.Excel;
using UnityEngine.Networking;
using System.Linq;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
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("ExaminationQuestions.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);
}
}
}