在unity2017中使用EXCEL读写数据

1.导入ExcelSDK:链接:https://pan.baidu.com/s/1LiU6Kaxmc9j6kbFoE9QMFA?pwd=dr78

提取码:dr78

2.挂载脚本,并运行unity

using Excel;
using OfficeOpenXml;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEngine;

public class RWExcel : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string path = Application.dataPath + "/入学信息.xlsx";
        WriteExcel(path);
        ReadExcel(path);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    //向excel中写数据
    public void WriteExcel(string path)
    {
        FileInfo fileInfo = new FileInfo(path);//创建
        if (fileInfo.Exists)
        {
            fileInfo.Delete();
            fileInfo = new FileInfo(path);
        }
        using (ExcelPackage ep = new ExcelPackage(fileInfo))//内置关闭流,打开Excel
        {
            ExcelWorksheet sheet = ep.Workbook.Worksheets.Add("学生表");
            sheet.Cells["A1"].Value = "学号";//给指定表格赋值
            sheet.Cells["B1"].Value = "姓名";
            sheet.Cells["C1"].Value = "年龄";
            sheet.Cells["A2"].Value = "001";//给指定表格赋值
            sheet.Cells["B2"].Value = "张三";
            sheet.Cells["C2"].Value = "17";
            sheet.Cells["A3"].Value = "002";//给指定表格赋值
            sheet.Cells["B3"].Value = "李四";
            sheet.Cells["C3"].Value = "18";
            ep.Save();
        }
    }
    //读excel中的数据
    public void ReadExcel(string path)
    {
        List<SkillInfo> skills = DoExcel.LoadRead(path);
        for (int i = 0; i < skills.Count; i++)
        {
            print(skills[i].id + "  " + skills[i].name + " " + skills[i].skill);
        }
    }
    public DataSet DoRead(string path)
    {
        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
        IExcelDataReader readData = ExcelReaderFactory.CreateOpenXmlReader(fs);//Excel流解析
        DataSet data = readData.AsDataSet();//数据读取完毕 data
        readData.Close();//关闭读取任务
        return data;
    }
    public List<Student> LoadRead(string path)
    {
        DataSet data = DoRead(path);
        List<Student> list = new List<Student>();
        int com = data.Tables[0].Columns.Count;//拿到列
        int row = data.Tables[0].Rows.Count;//拿到行
        for (int i = 0; i < row; i++)
        {
            Student info;
            info.id = data.Tables[0].Rows[i][0].ToString();
            info.name = data.Tables[0].Rows[i][1].ToString();
            info.age = int.Parse( data.Tables[0].Rows[i][2].ToString());
            list.Add(info);
        }
        return list;
    }
}
public struct Student
{
    public string id;
    public string name;
    public int age;
}

3.refresh刷新后可以看到excel和写入的数据

4.console控制台输出从excel表中读取的数据

注:以上方法适合unity2017版本

结语:合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒冷的晚风

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值