Unity Android reads Excel files (convert xslx files to csv files)

There are 4 types of paths:

 Application.dataPath (read-only): This path points to the Asset folder of our Unity editor

Application.persistentDataPath (readable and writable): This path points to the sandbox path for iOS and Android

 Application.streamingAssetsPath (read-only): The path to the streamingAsset folder. The contents of the folder can be read through this path on any platform.

 Application.temporaryCachePath (read-only): Temporary data file path

Prefabs are generally placed under Resources, and binary files (csv, bin, txt, xml, json, AB packages, etc.) are placed under StreamingAssets. Android cannot read this path through the File class and FileStream, and can only obtain it through the WWW class.

The code to read csv is as follows:

 public Dictionary<int, LingZhi> _InfoData = new Dictionary<int, LingZhi>();
    void Start()
    {
        StartCoroutine(Lingzhi());
        
        //Data infodata = CSV._Instance._InfoData[1];
        //Debug.Log(infodata.id);
        //Debug.Log(infodata.name);
        //Debug.Log(infodata.intro);
    }
    void Update()
    {
        
    }
    IEnumerator Lingzhi()
    {
        string fullFileName = Application.streamingAssetsPath + "/TianDao.csv";
        UnityWebRequest www = UnityWebRequest.Get(fullFileName);
        Debug.Log(www);
        yield return www.SendWebRequest();
        if (www.result == UnityWebRequest.Result.Success)
        {
            var fileData = www.downloadHandler.text; //文件内的数据信息
            fileData = fileData.Replace("\r", "");
            fileData = fileData.Replace("\"", "");
            string[] infoDatas = fileData.Split('\n');
            for (int i = 2; i < infoDatas.Length; i++)
            {
                if (infoDatas[i] != "")
                {
                    string[] infos = infoDatas[i].Split(',');
                    LingZhi data = new LingZhi(int.Parse(infos[0]), infos[1], int.Parse(infos[2]));
                    //{
                    //    id = int.Parse(infos[0]),
                    //    name = infos[1],
                    //    intro = int.Parse(infos[2]),
                    //};
                    _InfoData.Add(data.id, data);
                }
            }
            LingZhi infodata = _InfoData[1];
            Debug.Log(infodata.id);
            Debug.Log(infodata.name);
            Debug.Log(infodata.sum);
        }
        else
        {
          
        }
    }
}
[System.Serializable]
public class LingZhi
{
    public int id;
    public string name;
    public int sum;

    public LingZhi(int id,string name,int sum)
    {
        this.id = id;
        this.name = name;
        this.sum = sum;
    }
}

Guess you like

Origin blog.csdn.net/m0_71624363/article/details/132012709