Excel表格和Unity1.配置

下载EPPlus.dll

链接:https://pan.baidu.com/s/1l0FYTf8nATrPdEt6fXJ6Kg?pwd=1111
提取码:1111

将dll文件拖拽到Assets/Plugins

Assets下新建文件夹Editor,右键Editor点击Show in Explorer,新建Excel表格文件(后缀.xlsx),表格文件放在Assete/Editor中。

2.读取表格

引入命名空间 :using OfficeOpenXml ; using System.IO;

using System.Collections;using System.Collections.Generic;using UnityEngine;using OfficeOpenXml;using System.IO;public class NewRead : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        string filepath = Application.dataPath + "/Editor/内容.xlsx";        //获取Excel文件的信息        FileInfo fileInfo = new FileInfo(filepath);        //通过Excel表格的文件信息,打开Excel表格        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))        {            //对文件操作            //取得第一张表            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];            //            for (int i = worksheet.Dimension.Start.Row; i <= worksheet.Dimension.End.Row; i++)            {                for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)                {                    string s = worksheet.Cells[i, j].Value.ToString();                    Debug.Log(s);                }            }        }//关闭文件    }}

ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];读取第一个表格

worksheet为如图所示,且从1开始,不是从0开始。


3.写入表格

using System.Collections;using System.Collections.Generic;using UnityEngine;using OfficeOpenXml;using System.IO;public class NewRead : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        string filepath = Application.dataPath + "/Editor/内容.xlsx";        //获取Excel文件的信息        FileInfo fileInfo = new FileInfo(filepath);        //通过Excel表格的文件信息,打开Excel表格        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))        {        //对文件操作 worksheet.Cells[1, 1].Value = 20;            //保存            excelPackage.Save();        }//关闭文件    }}

4.创建Excel表格

using System.Collections;using System.Collections.Generic;using UnityEngine;using OfficeOpenXml;using System.IO;public class NewRead : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        string filepath = Application.dataPath + "/Editor/内容.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");            //删除表            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Delete("sheet1");            //保存            excelPackage.Save();        }//关闭文件    }}

5.打包

如果dll文件再Editor下,unity打包不会打包Editor内容。不会报错。

如果不在,则需要将unity的.net2.0子集改为.net2.0,打包才不会出错。