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就无法解析

相关推荐
快乐觉主吖1 小时前
Unity的日志管理类
android·unity·游戏引擎
WarPigs9 小时前
Unity性能优化笔记
笔记·unity·游戏引擎
沉到海底去吧Go11 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
T.D.C15 小时前
【业务框架】3C-相机-Cinemachine
unity
一线灵1 天前
跨平台游戏引擎 Axmol-2.6.1 发布
游戏引擎
Clank的游戏栈1 天前
Unity基于GraphView的可视化关卡编辑器开发指南
unity·编辑器·游戏引擎
Abigail_chow1 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
海尔辛1 天前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化
三巧2 天前
Godot 敌人生成半径和围墙不匹配,导致敌人错误的生成在围墙外的解决代码
游戏引擎·godot
技术小甜甜2 天前
【Godot引擎】如何使用内置的全局搜索功能提升开发效率
游戏引擎·godot