Unity解析Excel存储到DataTable中

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;
    }

blog.csdnimg.cn/direct/995013a3eca0436a96f4388220beb209.png)
经过测试只能在编辑器和打包exe能解析成功,打包到安卓apk就无法解析

最近更新

  1. Android 混淆模板

    2024-04-03 08:44:02       0 阅读
  2. redis常用5大数据类型

    2024-04-03 08:44:02       0 阅读
  3. 深度学习——Transformer的理解整理

    2024-04-03 08:44:02       0 阅读
  4. XiaodiSec day014 Learn Note 小迪渗透学习笔记

    2024-04-03 08:44:02       0 阅读
  5. 微信小程序

    2024-04-03 08:44:02       0 阅读

热门阅读

  1. Vue-前端应用开发平时作业第二章作业2

    2024-04-03 08:44:02       4 阅读
  2. Android 11 init进程对Selinux的处理

    2024-04-03 08:44:02       6 阅读
  3. 读《Spring实战》第二章:装配bean

    2024-04-03 08:44:02       3 阅读
  4. oracle杀外部连接会话

    2024-04-03 08:44:02       5 阅读
  5. 高级排序算法-快速排序

    2024-04-03 08:44:02       6 阅读
  6. Jenkins 自动化更新 Stable Diffusion 介绍

    2024-04-03 08:44:02       3 阅读
  7. maya移除节点 修改节点

    2024-04-03 08:44:02       5 阅读
  8. 上传.PDF文件转为.DOCX文件

    2024-04-03 08:44:02       4 阅读
  9. 音响或耳机音频处理常用的芯片

    2024-04-03 08:44:02       6 阅读