unity读取excel数据并绘制曲线

一、读取数据

1.导入EPPlus类库:EPPlus.dll

2.创建script脚本

3.创建空物体,挂载脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;  //excel命名空间
using System.IO;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string filePath = "F:/Unity Project/Curve_draw/Assets/scripts/data.xlsx";
        //获取excel文件信息
        FileInfo fileInfo = new FileInfo(filePath);
        //通过excel文件信息,打开excel表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)) 
        {
            //Excel表格的操作,花括号结束完成一系列操作
            ExcelWorksheet worksheet= excelPackage.Workbook.Worksheets["Sheet1"]; //取Excel中的第一张表
            string s = worksheet.Cells[2, 1].Value.ToString();//第一行第一列信息

           // Debug.Log(s);
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j <3; j++)
                {
                    Debug.Log(worksheet.Cells[i, j].Value.ToString());
                }
                
            }
      
        }//关闭Excel表格
    }
}

二、写入数据

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


public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string filePath = "F:/Unity Project/Curve_draw/Assets/scripts/data.xlsx";

        //获取excel文件信息
        FileInfo fileInfo = new FileInfo(filePath);

        //通过excel文件信息,打开excel表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)) 
        {
            //Excel表格的操作,花括号结束完成一系列操作
            ExcelWorksheet worksheet= excelPackage.Workbook.Worksheets["Sheet1"]; //取Excel中的第一张表
           
            string s = worksheet.Cells[2, 1].Value.ToString();//获取第二行第一列信息

            worksheet.Cells[5, 1].Value = "4";//往第四行,第一列写入数据
            excelPackage.Save();//保存表格

        }//关闭Excel表格
    }
}

三、创建Excel表格

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


public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string filePath = "F:/Unity Project/Curve_draw/Assets/scripts/data_new.xlsx";

        //获取excel文件信息,此时文件不存在
        FileInfo fileInfo = new FileInfo(filePath);

        //通过excel文件信息,打开excel表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)) 
        {
            ExcelWorksheet worksheet= excelPackage.Workbook.Worksheets.Add("Sheet1");//创建表
            excelPackage.Workbook.Worksheets.Add("Sheet2");//创建表
            excelPackage.Workbook.Worksheets.Add("Sheet3");//创建表
            excelPackage.Workbook.Worksheets.Delete("Sheet3");//删除表
            excelPackage.Save();

        }//关闭Excel表格
    }
}

四、绘制曲线

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;  
using System.IO;
using System.Threading;
using System;


public class Test : MonoBehaviour
{
    public LineRenderer lr;
    public float x = 0.1f;
    public float y = 0.1f;

    // Start is called before the first frame update
    void Start()
    {
        lr = GetComponent();
        lr.startWidth = 0.2f;
        lr.endWidth = 0.2f;
        lr.startColor = Color.red;
        lr.endColor = Color.red;
        string filePath = "F:/Unity Project/Curve_draw/Assets/scripts/data_xy.xlsx";

        //获取excel文件信息
        FileInfo fileInfo = new FileInfo(filePath);
        //通过excel文件信息,打开excel表格
        Vector3[] positions = new Vector3[1000]; 
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
        {
            //Excel表格的操作,花括号结束完成一系列操作
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"]; //取Excel中的第一张表
            
            for (int i = 2; i < 1000; i++)
            {
                positions[i] = new Vector3(x, y,0);
                object a = worksheet.Cells[i,1].Value;
                object b = worksheet.Cells[i, 2].Value;
                x = Convert.ToInt64(a);
                y = Convert.ToInt64(b);
            }
        }//关闭Excel表格
        lr.positionCount = positions.Length;
        lr.SetPositions(positions);

    }

}

五、在UI界面绘图

未完待续

你可能感兴趣的:(unity,unity,excel,游戏引擎)