Unity解析Excel存储到DataTable中,在Android中也能解析

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

public Dictionary<string, DataTable> GetExcelDataByBytes(MemoryStream ms)
{

    Dictionary<string, DataTable> worksheets = new Dictionary<string, DataTable>();

  

    using (ExcelPackage package = new ExcelPackage(ms))
    {
        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;



}

blog.csdnimg.cn/direct/995013a3eca0436a96f4388220beb209.png)
经过测试只能在编辑器和打包exe能解析成功

使用下面方法加载经过测试在Android中也能解析Excel

 IEnumerator ExcelLoad()
 {
     string excelPath = Path.Combine(Application.streamingAssetsPath, "IOEcl.xlsx");
     using (UnityWebRequest www = UnityWebRequest.Get(excelPath))
     {
         yield return www.SendWebRequest();

         if (www.isNetworkError || www.isHttpError)
         {
         }
         else
         {
             byte[] excelBytes = www.downloadHandler.data;
             using (MemoryStream memoryStream = new MemoryStream(excelBytes))
             {
                 ExcelParse excel = new ExcelParse();
                 ExcelDic = excel.GetExcelDataByBytes(memoryStream);
                 isLoaded = true;
             }
         }
     }
 }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值