原材料:
1.EPPlus.dll、Excel.dll、ICSharpCode.SharpZipLib.dll。
2.Unity2020.3.2f1
3.rider
步骤
1.unity工程Plugins文件夹内添加Dll。
2.添加代码。
public class LoadExcelData : MonoBehaviour
{
private static LoadExcelData instance;
public static LoadExcelData Instance => instance;
public string filePath;
private Awake()
{
Instance = this;
}
public void Start()
{
fliePath=Application.streamingAssetsPath + "/CellData.xls";
}
public void ReadExcel(string filePath)
{
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
//Debug.Log(stream.Length);
IExcelDataReader excelReader = null;
//读取两种不同格式的excel,用的api不同
if (filePath.Contains(".xls"))
{
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else
{
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
DataSet result = excelReader.AsDataSet();
for (int i = 0; i < result.Tables.Count; i++)
{
//Tables[i]为sheet
var count = result.Tables[i].Rows.Count;
for (int j = 0; j < count; j++)
{
for (int k = 0; k < count; k++)
{
//检查数据如果有不是定义的字符打印log
if (str != "s" && str != "e")
{
Debug.Log($"检查数据:{result.Tables[i].TableName}\"str\" pos: {j}{k}");
}
}
}
}
stream.Close();
}
问题记录
1.策划写数据的时候注意表格内空格里不能有全角或者半角的空格,代码读取的时候会把空格的数据页读作一行或者一列。不能有未规定的字符出现。