JustinAB
JustinAB

Reputation: 33

Transferring Excel Data to Unity

I have a large list of data in an excel spreadsheet that I need to transfer a few arrays in my Unity project. I have been looking up a few methods and will likely try out a few of them, but I had another question. Is it possible to transfer Excel data to Unity arrays without an additional plug in or tool? Just with base Unity?

Upvotes: 1

Views: 2451

Answers (2)

Lotan
Lotan

Reputation: 4283

As @julienkay says, if the excel file is a .csv could be treated as a plain text file, BUT what if not?

If you are working with .xlsx I recommend to use Microsoft.Office.Interop.Excel;, it's pretty easy to use, but not so easy to get it working in Unity, that's why this link could help you!

And this is a simple example on how to use the Interop.Excel methods, this code will create a dictionary filled with the titles and links for the books in this excel:

private void Exel()
{
    Dictionary<string, string> linkTitleDict = new Dictionary<string, string>();

    Application app = new Application();
    Workbook excelWorkbook = app.Workbooks.Open(@"C:\Users\...\SpringerEbooks.xlsx");
    Sheets excelSheets = excelWorkbook.Worksheets;
    string currentSheet = "Table 1";
    Worksheet excelWorksheet = (Worksheet)excelSheets.get_Item(currentSheet);
    //
    for (int i = initialRow; i < finalRow + 1; i++)
    {
        string bookTitle = (string)(excelWorksheet.Cells[i, 2] as Range).Value;
        string bookLink = (string)(excelWorksheet.Cells[i, 5] as Range).Value;
        linkTitleDict.Add(bookLink, bookTitle);
    }
}

How the excel looks: enter image description here

Upvotes: 2

julienkay
julienkay

Reputation: 61

If you're willing to save your excel file in the .csv format to load that into Unity this is easily achievable without additional plugins, because .csv is essentially just a text file. Just one example on how to parse a .csv file can be found here: https://bravenewmethod.com/2014/09/13/lightweight-csv-reader-for-unity/

If you want to read native excel files (.xlsx) that would be more difficult.

Upvotes: 1

Related Questions