unity 学习之读取excel 数据

最近导让用师兄算法的数据在本机上引用,模拟船舶的运动,所以学习了一下unity引用excel的方法,开始使用Excel的插件,直接读取后存入dataset中,但是在格式转换的时候始终报错,解决未果,又在网上找了好多代码例程,但是其中的结构都感觉过于复杂,记起之前用过NPOI,遂下载了NPOI2.2.1版的插件,

附上链接:https://pan.baidu.com/s/1ApaAC49X0__yQTW-VvlhGQ

将里面一系列dll文件导入unity中的plungins文件夹中,

但是unity不识别能够读取xlsx格式的XSSF,所以不能读取2007的excel格式,只能读取97—03格式的,

下面贴一下代码,防止之后再用这个功能,免得到处找。

这是读取的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NPOI;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;
public class NPOIRead : MonoBehaviour {

void Start ()   {

        ExcelRead();
}

    public void ExcelRead()
    {
        IWorkbook book = null;
        FileStream fs = File.OpenRead("C:/Users/zty07/Desktop/data.xls");
        book = new HSSFWorkbook(fs);
        fs.Close();
        ISheet sheet = book.GetSheetAt(1);
        IRow row = sheet.GetRow(0);
        for (int i = 0; i <= sheet.LastRowNum; i++)
        {
            row = sheet.GetRow(i);
            if (row != null)
            {
                string s = "";
                for (int j = 0; j < row.LastCellNum; j++)
                {
                    string value = row.GetCell(j).ToString();
                    s += value;
                }
                Debug.Log(s);
            }       
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zlb666/article/details/79826872