Unity 读取Excel

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Excel;
using System.Data;

public struct DepenceTableData

{

    public string word;

    public string instruct;

}

public class ReadExcel : MonoBehaviour
{
    public List<DepenceTableData> listdata;

    void Start()
    {
        listdata = Load(Application.dataPath + "\\AppAsset\\" + "OutboundPedsData.xlsx");

        foreach (var listing in listdata)

        {

            print(listing.instruct + "     " + listing.word);

            Debug.Log((listing.instruct + "     " + listing.word + "\n").ToString());
        }

    }



    public  DataSet ReadExcelFun(string path)

    {

        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

        // CreateOpenXmlReader用于读取Excel2007版本及以上的文件

        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        DataSet result = excelReader.AsDataSet();

        excelReader.Close();

        return result;

    }

    public List<DepenceTableData> Load(string path)

    {

        List<DepenceTableData> _data = new List<DepenceTableData>();

        DataSet resultds = ReadExcelFun(path);

        int column = resultds.Tables[0].Columns.Count;

        int row = resultds.Tables[0].Rows.Count;

        Debug.LogWarning(column + "  " + row);

        for (int i = 1; i < row; i++)

        {

            DepenceTableData temp_data;

            temp_data.instruct = resultds.Tables[0].Rows[i][0].ToString();

            temp_data.word = resultds.Tables[0].Rows[i][1].ToString();

            Debug.Log(temp_data.instruct + "  " + temp_data.word);

            _data.Add(temp_data);

        }

        return _data;

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值