Unity解析Excel存储到DataTable中

csharp 复制代码
public class ExcelParse : MonoBehaviour
{

    public Dictionary<string, DataTable> GetExcelData(string filePath)
    {
       
        Dictionary<string, DataTable> worksheets = new Dictionary<string, DataTable>();

        FileInfo fileInfo = new FileInfo(filePath);

        using (ExcelPackage package = new ExcelPackage(fileInfo))
        {
            ExcelWorkbook workbook = package.Workbook;
            if (workbook != null)
            {
                foreach (ExcelWorksheet worksheet in workbook.Worksheets)
                {
                    // 创建一个新的DataTable对象
                    DataTable dataTable = ExcelFileRead(worksheet,false);
                    
                        // 将 DataTable 存储到 Dictionary 中
                    worksheets.Add(worksheet.Name, dataTable);
                    
                }
            }
        }
        return worksheets;



    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="workBook"></param>
    /// <param name="index"></param>
    /// <param name="sheetName"></param>
    /// <param name="isHadColumnName">是否包含列名,默认有</param>
    /// <returns></returns>
    private DataTable ExcelFileRead(ExcelWorksheet sheet,bool isHadColumnName = true)
    {
        DataTable dt = new DataTable();
        int cellCount = sheet.Dimension.End.Column; //总列数
        int rowCount = sheet.Dimension.End.Row; //总行数
        
       
        if (sheet != null && (rowCount + 1) != 0)
        {
            int firstRow = sheet.Dimension.Start.Row;
            int firsColumn=sheet.Dimension.Start.Column;
            if (isHadColumnName)
            {
                for (int i =firsColumn; i < cellCount; i++)
                {
                    string columnName = Convert.ToString(sheet.Cells[firstRow, i].Value);
                    
                    dt.Columns.Add(columnName);
                }
                firstRow = 2;
            }
            else
            {
                for (int i = 0; i < cellCount; i++)
                {
                    DataColumn dataColumn = new DataColumn("Column" + i);
                    dt.Columns.Add(dataColumn);
                }
            }
            for (int i = firstRow; i < rowCount+1; i++)
            {
                DataRow dataRow = dt.NewRow();
                for (int j = firsColumn; j < cellCount+1; j++)
                {
                    if (sheet.Cells[i, j].Value != null)
                    {
                        dataRow[j-1] = sheet.Cells[i, j].Value.ToString();
                        //UnityEngine.Debug.Log(dataRow[j - 1]);
                    }
                    else
                    {
                        dataRow[j-1] = "";
                    }
                }
                dt.Rows.Add(dataRow);

                
            }
        }
        
        return dt;
    }

经过测试只能在编辑器和打包exe能解析成功,打包到安卓apk就无法解析

相关推荐
魔士于安12 小时前
Unity资源Toon City Pack 发电厂 工地 公园 地铁站口 银行 车 直升飞机 可动 URP
游戏·unity·游戏引擎·贴图·模型
SmartRadio14 小时前
NRF52833 + MPU6050 室内定位跟随无人机
游戏引擎·无人机·cocos2d
心前阳光15 小时前
Unity之运行时标准材质半透明无效果
unity·游戏引擎·材质
鱼樱16 小时前
腾讯版小龙虾WorkBuddy试用体验——Excel处理效率翻倍
excel
LucaJu16 小时前
Java + EasyExcel 实现单个接口导出多个Excel
java·excel
张老师带你学17 小时前
Unity buildin 石头围墙 树木 树墩子 卡通风格 栅栏 小桥 低多边形
科技·游戏·unity·游戏引擎·模型
CN.LG18 小时前
Excel 中把 Sheet页所有 “xxx” 字体标红
excel
weixin_7042660519 小时前
读取Excel 和 显示预约人数
spring boot·mybatis·excel
呆呆敲代码的小Y21 小时前
【Unity工具篇】| 使用YooAsset接入自己的游戏项目,实现完整热更新流程
游戏·unity·游戏引擎·热更新·yooasset·资源热更新
张老师带你学21 小时前
Unity 低多边形 赛博朋克城市 拼装 模型 道路 建筑 buildin
科技·游戏·unity·游戏引擎·模型