Operation of Excel in Unity (using EPPlus)

Table of contents

1. Import EPPlus

1. First import EPPlus and Excel in Unity

2. Create a script and introduce a namespace

2. Read Excel

1. Get the Excel information file

2. Open the Excel file information and open the table

3. Which table of this Excel needs to be opened

4. Read data

5. Output the result

6. Complete code

3. Write to Excel

1. The first and second steps are the same as reading

2. Write data

3. After writing the form

4. Complete code

4. Create a table

1. The first and second steps are the same as reading

2. Create a table

3. Create a good form

5. Common APIs

1. Operation on the form

2. Processing data

3. Operation on Excel

6. Tips


1. Import EPPlus

1. First import EPPlus and Excel in Unity

2. Create a script and introduce a namespace

using UnityEngine;
using UnityEditor;
using OfficeOpenXml;
using System.IO;


2. Read Excel

 

- Form data

1. Get the Excel information file

        string filePath = Application.dataPath + "/目标名称.xlsx";//这里是文件路径
   
        //获取Excel文件的信息
        FileInfo fileInfo = new FileInfo(filePath);

2. Open the Excel file information and open the table

        //通过Excel文件信息,打开表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
        //using是用来强行资源释放(前括号是打开,后括号是关闭)
        {

        }

3. Which table of this Excel needs to be opened

        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
        {
            //取得Excel文件中的第N张表
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
        }

4. Read data


        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
        {
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
            for (int i = 2; i < worksheet.Dimension.End.Row; i++)
            //End.Row获得当前表格的最大行数
            {
                Debug.Log("目标名称:" + worksheet.Cells[i, 1].Value.ToString() + " // 设备名称:" + worksheet.Cells[i, 2].Value.ToString() + " // 地址:" + worksheet.Cells[i, 3].Value.ToString());
                //Cells是个二维数组,第一个参数是读取第几行,第二个参数是读取第几列需要ToString出数据
            }
        }

5. Output the result

6. Complete code

using UnityEngine;
using UnityEditor;
using OfficeOpenXml;
using System.IO;

/// <summary> Read文件 </summary>
public class ReadExcel
{

    [MenuItem("数据持久化/1.Excel/1.ReadExcel")]
    public static void InputExcel()
    {
        string filePath = Application.dataPath + "/00.Excel文件夹/Read.xlsx";

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

        ///通过Excel文件信息,打开表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
        {
            //取得Excel文件中的第N张表
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
            for (int i = 2; i < worksheet.Dimension.End.Row; i++)//End.Row获得当前表格的最大行数
            {
                Debug.Log("目标名称:" + worksheet.Cells[i, 1].Value.ToString() + 
                    " // 设备名称:" + worksheet.Cells[i, 2].Value.ToString() + 
                    " // 地址:" + worksheet.Cells[i, 3].Value.ToString());
                //Cells是个二维数组,第一个参数是读取第几行,第二个参数是读取第几列需要ToString出数据
            }
        }
    }
}


3. Write to Excel

- initial form

1. The first and second steps are the same as reading

2. Write data

        ///通过Excel文件信息,打开表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
        {
            //取得Excel文件中的第N张表
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
            for (int i = 2; i <= 12; i++)
            {
                //直接向每个表格赋值则是写入
                worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
                worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
                worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
            }
            excelPackage.Save();//写入后保存表格
        }

3. After writing the form

4. Complete code

using UnityEngine;
using UnityEditor;
using OfficeOpenXml;
using System.IO;

public class WriteExcel
{

    [MenuItem("数据持久化/1.Excel/2.WriteExcel")]
    public static void ChangeExcel()
    {
        string filePath = Application.dataPath + "/00.Excel文件夹/Write.xlsx";

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

        ///通过Excel文件信息,打开表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
        {
            //取得Excel文件中表格叫Sheet1的表格
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
            for (int i = 2; i <= 12; i++)
            {
                //直接向每个表格赋值则是写入
                worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
                worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
                worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
            }
            excelPackage.Save();//写入后保存表格
        }
    }

}


4. Create a table

1. The first and second steps are the same as reading

2. Create a table

using UnityEngine;
using UnityEditor;
using OfficeOpenXml;
using System.IO;

public class Creat
{
    [MenuItem("数据持久化/1.Excel/3.CreatExcel")]
    public static void ChangeExcel()
    {
        string filePath = Application.dataPath + "/00.Excel文件夹/Creat.xlsx";

        ///获取Excel文件的信息(文件中没有这个文件也不会报错)
        FileInfo fileInfo = new FileInfo(filePath);

        ///通过Excel文件信息,打开表格
        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//在这里也没有创建Excel文件
        {
            //在新Excel文件中创建叫Sheet1的表格
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1");
            worksheet.Cells[1, 1].Value = "目标名称";
            worksheet.Cells[1, 2].Value = "设备地址";
            worksheet.Cells[1, 3].Value = "地址";
            for (int i = 2; i <= 12; i++)
            {
                //直接向每个表格赋值则是写入
                worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
                worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
                worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
            }
            excelPackage.Save();//写入后保存表格
        }
        //完成文件创建
    }
}

3. Create a good form


5. Common APIs

1. Operation on the form

  1. Get the Nth table in the Excel file through the int index
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1]
  2. Get the Excel table
    ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["Sheet1"];
  3. Get the number of current sheets
    int i = excelPackage.Workbook.Worksheets.Count;
  4. Add a table
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet2");
  5. Add color to the table name (change the header color to green)
    worksheet.TabColor = System.Drawing.Color.ForestGreen;

2. Processing data

  1. Get the largest row and column in the table
    int row = worksheet.Dimension.End.Row;//row
    int column = worksheet.Dimension.End.Column;//column
  2. Get the content in the xth cell
    worksheet.Cells[1, 1].Value.ToString();//Get the content at [1,1], which can be written (write without ToString) or read 

3. Operation on Excel

  1. Get an Excel file
    ExcelPackage excelPackage = new ExcelPackage(fileInfo);
  2. Save the modified Excel file
    excelPackage.Save();//Save
    excelPackage.SaveAs();//Save as


6. Tips

  1. If you modify the form, remember to save the operation to save the form

GitHub of EPPlus below:

GitHub - JanKallman/EPPlus: Create advanced Excel spreadsheets using .NETCreate advanced Excel spreadsheets using .NET. Contribute to JanKallman/EPPlus development by creating an account on GitHub.https://github.com/JanKallman/EPPlus

Guess you like

Origin blog.csdn.net/SodasZ/article/details/125322835