UnityでExcelを読み込む【ExcelDataReader】

# 環境
- Unity 2019.2.13
- NuGetForUnity 2.0.0

# 導入方法
## 1. NuGetをインストールする

https://github.com/GlitchEnzo/NuGetForUnity/releases

<img width="926" alt="Screen Shot 2019-12-20 at 17.03.34.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132627/45f78666-5c73-9b48-4141-671d1304d3fc.png">

<img width="725" alt="Screen Shot 2019-12-20 at 17.04.50.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132627/35055a3b-dceb-0d4d-9b3a-8fcb75da8d5e.png">

## 2. ExcelDataReaderをインストールする

<img width="720" alt="Screen Shot 2019-12-20 at 17.06.09.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132627/7276819e-b177-3b2e-c5da-4ccb9558fee5.png">

<img width="697" alt="Screen Shot 2019-12-20 at 17.08.22.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132627/2eac7bac-7b34-85b1-b0d3-5b0f4de3be25.png">

Excelのデータを`System.Data.DataSet`として受け取るのなら、
`ExcelDataReader.DataSet`も一緒にインストールする

<img width="834" alt="Screen Shot 2019-12-20 at 17.14.59.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132627/69f5e101-50eb-8509-9b9b-db85e1263e40.png">

# サンプルコード

https://github.com/tani-shi/unity-excel

```
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using ExcelDataReader;
using UnityEngine;

public class Excel {
    public struct Sheet {
        public string name;
        public Cell[] cells;

        public Cell GetCell (int row, int column) {
            return cells.FirstOrDefault (c => c.row == row && c.column == column);
        }

        public Cell[] GetRowCells (int row) {
            return cells.Where (c => c.row == row).ToArray ();
        }

        public Cell[] GetColumnCells (int column) {
            return cells.Where (c => c.column == column).ToArray ();
        }
    }

    public struct Cell {
        public int row;
        public int column;
        public string value;
    }

    public string error {
        get {
            return _error;
        }
    }

    public Sheet[] Sheets {
        get {
            return _sheets;
        }
    }

    private Sheet[] _sheets = null;
    private string _error = string.Empty;
    private string _name = string.Empty;

    public static bool TryRead (string path, out Excel excel) {
        excel = Read (path);
        return string.IsNullOrEmpty (excel._error);
    }

    public static Excel Read (string path) {
        var excel = new Excel ();
        try {
            using (var stream = File.Open (path, FileMode.Open, FileAccess.Read)) {
                var reader = ExcelReaderFactory.CreateOpenXmlReader (stream);
                if (reader != null) {
                    excel.ParseDataSet (reader.AsDataSet ());
                }
            }
        } catch (Exception e) {
            Debug.LogError (e);
            excel._sheets = new Sheet[] { };
            excel._error = e.ToString ();
        }
        return excel;
    }

    public Sheet GetSheet (string name) {
        return _sheets.FirstOrDefault (s => s.name == name);
    }

    private void ParseDataSet (DataSet dataSet) {
        var sheetList = new List<Sheet> ();
        foreach (DataTable table in dataSet.Tables) {
            var sheet = new Sheet ();
            sheet.name = table.TableName;
            var cellList = new List<Cell> ();
            for (int row = 0; row < table.Rows.Count; row++) {
                for (int column = 0; column < table.Columns.Count; column++) {
                    var cell = new Cell ();
                    cell.row = row;
                    cell.column = column;
                    cell.value = table.Rows[row][column].ToString ();
                    cellList.Add (cell);
                }
            }
            sheet.cells = cellList.ToArray ();
            sheetList.Add (sheet);
        }
        _sheets = sheetList.ToArray ();
    }
}
```

```
using UnityEngine;
using UnityEditor;

public static class ExcelDemo {
    private const string kSampleExcelPath = "Xlsx/Sample.xlsx";

    [MenuItem("ExcelDemo/Read Sample Excel")]
    private static void ReadSampleExcel () {
        Excel excel;
        Debug.Log("EXCEL READING START");
        Debug.Log("==================");
        if (Excel.TryRead(kSampleExcelPath, out excel)) {
            foreach (var sheet in excel.Sheets) {
                foreach (var cell in sheet.cells) {
                    Debug.Log(string.Format("{0}:{1}:{2} value={3}", sheet.name, cell.row, cell.column, cell.value));
                }
            }
        }
        Debug.Log("==================");
        Debug.Log("EXCEL READING END");
    }
}
```