需要增加动态链接库EPPLUS、Excel、ICSharpCode.SharpZipLib、I18N、I18N.CJK、I18N.MidEast、I18N.Other、I18N.Rare、I18N、West,Excel相关动态链接库需要下载,I18相关在Unity安装路径下可以找到

[code]csharpcode:

    DataSet ds;
    /// <summary>
    /// 人员数据xls、xlsx选择
    /// </summary>
    public void SelectExcel()
    {
        OpenFileName openFileName = new OpenFileName();
        openFileName.structSize = System.Runtime.InteropServices.Marshal.SizeOf(openFileName);
        //openFileName.filter = "人脸照片(*.png*.jpg*.bmp)\0*.png;*.jpg;*.bmp";
        openFileName.filter = "选择信息(*.xls*.xlsx)\0*.xls;*.xlsx";
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        openFileName.title = "窗口标题";
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

        if (LocalDialog.GetSaveFileName(openFileName))
        {
            //path_T.text = openFileName.file;
            ds = ReadExcel(openFileName.file);
            //Debug.Log("Excel长度:"+ds.Tables[0].Rows.Count);
            //预览按钮
            //previewBtn.gameObject.SetActive(true);
        }
    }
    /// <summary>
    /// 读取excel
    /// </summary>
    private DataSet ReadExcel(string path)
    {
        try
        {
            File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        }
        catch (System.Exception e)
        {
            Logic_MainControl._instance.mainResult.text = "读取失败:" + e.ToString();
        }
        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();
        stream.Close();
        stream.Dispose();
        return result;
    }
    /// <summary>
    /// 处理数据
    /// </summary>
    private void Dispose() {
        for (int i = 1; i < ds.Tables[0].Rows.Count; i++)//表格遍历
        {
            if (string.IsNullOrEmpty(ds.Tables[0].Rows[i][0].ToString())) break;
            GameObject go=Instantiate(userItem);
            
            Text[] t = go.GetComponentsInChildren<Text>();
            t[0].text = ds.Tables[0].Rows[i][0].ToString();//第i行第0列
            t[1].text = ds.Tables[0].Rows[i][1].ToString();
            t[2].text = ds.Tables[0].Rows[i][2].ToString();
            t[3].text = ds.Tables[0].Rows[i][3].ToString();
            t[4].text = ds.Tables[0].Rows[i][4].ToString();
            t[5].text = ds.Tables[0].Rows[i][5].ToString();
            t[6].text = ds.Tables[0].Rows[i][6].ToString();
           
        }
    }

  

posted on 2021-10-09 19:09  陌洛  阅读(6)  评论(0编辑  收藏  举报