Read by Unity3D -- read Excel file content

Keywords: Programming Excel Android Java

In the process of Unity3D development, reading excel table is a very frequent thing. It is mainly used to record various data. Various platforms may have many methods, such as Android, you can plug-in, or you can use third-party java to develop. dll package for Plugin, and then C ා and Java interact to realize Excel reading (it's troublesome, not recommended, unless you develop Android Also very well known, can use)
Today, I'll talk about the FlexReader plug-in, which specializes in reading Excel files.
Direct code:
Plug in: you can search FlexReader directly on the Asset Store (used by tuhao)
Download address: link: https://pan.baidu.com/s/1mLTu2ymQY0Wmvpzb0DQ5iw Password: j9m6
//Load files asynchronously

 IEnumerator LoadGuideAync(string path, DownloadHandler handler)
    {
        var url = Path.Combine(Application.streamingAssetsPath, path);
        using (var request = UnityWebRequest.Get(url))
        {
            yield return request.SendWebRequest();
            var bytes = request.downloadHandler.data;
            handler(bytes);
        }
    }

//Load to content callback

  void LoadGuideData(byte[] bytes)
    {
        //bytes is to load the file content stream in Excel
        if (bytes.Length == 0)
            return;
        //Through the WorkBook class transformation of the plug-in, a list is obtained. The size of the list represents the number of tables in Excel.
        var book = new WorkBook(bytes);
        Debug.Log(book.Count);
        if (book.Count < 2)
            return;
        InitNoviceGuideRectTransform(book[0]);
        InitNoviceGuideRectTransform(book[1]);
    }

Analyze each table and read the data of each cell

         void InitNoviceGuideRectTransform(IEnumerable<Row> rows)
    {
        int index = -1;
        int count = rows.Count(r => !r.IsEmpty());
        if (count == 0)
            return;
                //Store the two-dimensional numbers in the list and read them through the row and column 
        List<Row> rowData = new List<Row>(rows);
                 for (int j = 1; j < rowData .Count; j++)//That's ok
        {
            for (int i = 0; i < rowData[j].Count; i++)//column
            {
                Debug.Log(rowData[j][i].Text);
            }
        }  
  }

It's that simple. Read the data you want and do what you want.

Posted by jbille on Mon, 11 May 2020 09:08:14 -0700