Excel表格和Unity
1.配置
下載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 = https://www.cnblogs.com/Leslielei/archive/2023/05/25/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,打包才不會出錯,


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/553444.html
標籤:其他
上一篇:Kubernetes 證書詳解
下一篇:返回列表
