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

相关推荐
CodeKwang15 小时前
Qt实战:简易Excel表格 | 附完整源码
qt·excel·qtabwidget·qt控件
脸大是真的好~17 小时前
EasyExcel的使用
java·excel
我的offer在哪里17 小时前
示例 Unity 项目结构(Playable Game Template)
unity·游戏引擎
骆驼爱记录20 小时前
Word样式检查器使用指南
自动化·word·excel·wps·新人首发
淡海水20 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
在路上看风景20 小时前
4.6 显存和缓存
unity
热爱生活的五柒21 小时前
wps office/word 表格左对齐后 文字前仍有空白,如何解决
excel
Zik----1 天前
简单的Unity漫游场景搭建
unity·游戏引擎
程序员敲代码吗1 天前
在Excel中快速进行精确数据查找的方法
excel
在路上看风景1 天前
4.5 顶点和片元
unity