77 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Data;
 | |
| using System.IO;
 | |
| using System.Collections.Generic;
 | |
| #if UNITY_EDITOR
 | |
| using Excel;
 | |
| #endif
 | |
| using UnityEngine;
 | |
| 
 | |
| public class ExcelParser
 | |
| {
 | |
|     /// <summary>
 | |
|     /// 读取表数据,生成对应的数组
 | |
|     /// </summary>
 | |
|     /// <param name="pFilePath">excel文件全路径</param>
 | |
|     /// <returns>Item数组</returns>
 | |
|     public static List<LevelData> CreateItemArrayWithExcel(string pFilePath)
 | |
|     {
 | |
|         int tColNum = 0, tRowNum = 0;
 | |
|         Debug.Log(pFilePath);
 | |
|         DataRowCollection collect = ReadExcelContext(pFilePath, ref tColNum, ref tRowNum);
 | |
|         Debug.Log("列数: " + tColNum + " ,行数: " + tRowNum);
 | |
|         // 第一行变量名,循环列
 | |
|         //for (int i = 0; i < tColNum; i++)
 | |
|         //{
 | |
|         //}
 | |
| 
 | |
|         //第二行开始才是数据,循环行
 | |
|         List<LevelData> tList = new List<LevelData>(tRowNum - 1);
 | |
|         for (int i = 1; i < tRowNum; i++)
 | |
|         {
 | |
|             LevelData tItem = new LevelData();
 | |
|             //解析每列的数据
 | |
|             tItem.LevelID = 0;
 | |
|             int.TryParse(collect[i][0].ToString(), out tItem.LevelID);
 | |
|             tItem.FirstNum = 0;
 | |
|             int.TryParse(collect[i][1].ToString(), out tItem.FirstNum);
 | |
|             tItem.Retry1Num = 0;
 | |
|             int.TryParse(collect[i][2].ToString(), out tItem.Retry1Num);
 | |
|             tItem.Retry2Num = 0;
 | |
|             int.TryParse(collect[i][3].ToString(), out tItem.Retry2Num);
 | |
|             tItem.TotalNum = 0;
 | |
|             int.TryParse(collect[i][4].ToString(), out tItem.TotalNum);
 | |
|             tItem.UnitNum = 0;
 | |
|             int.TryParse(collect[i][5].ToString(), out tItem.UnitNum);
 | |
|             tItem.MaxLevel = 0;
 | |
|             int.TryParse(collect[i][6].ToString(), out tItem.MaxLevel);
 | |
|             tItem.Formation = string.IsNullOrEmpty(collect[i][8].ToString()) ? "" : collect[i][8].ToString();
 | |
| 
 | |
|             tList.Add(tItem);
 | |
|         }
 | |
| 
 | |
|         return tList;
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 读Excel
 | |
|     /// </summary>
 | |
|     /// <param name="pFilePath">文件路径</param>
 | |
|     /// <param name="pColNum">行数</param>
 | |
|     /// <param name="pRowNum">列数</param>
 | |
|     /// <returns></returns>
 | |
|     static DataRowCollection ReadExcelContext(string pFilePath, ref int pColNum, ref int pRowNum)
 | |
|     {
 | |
| #if UNITY_EDITOR
 | |
|         FileStream stream = File.Open(pFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
 | |
|         IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 | |
| 
 | |
|         DataSet result = excelReader.AsDataSet();
 | |
|         // Tables[0] 下标0表示excel文件中第一张表的数据
 | |
|         pColNum = result.Tables[0].Columns.Count;
 | |
|         pRowNum = result.Tables[0].Rows.Count;
 | |
|         return result.Tables[0].Rows;
 | |
| #else
 | |
|         return null;
 | |
| #endif
 | |
|     }
 | |
| } |