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/內(nèi)容.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);
}
}
}//關(guān)閉文件
}
}
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/內(nèi)容.xlsx";
//獲取Excel文件的信息
FileInfo fileInfo = new FileInfo(filepath);
//通過Excel表格的文件信息,打開Excel表格
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
//對文件操作
worksheet.Cells[1, 1].Value = 20;
//保存
excelPackage.Save();
}//關(guān)閉文件
}
}
4.創(chuàng)建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/內(nèi)容.xlsx";
//獲取Excel文件的信息
FileInfo fileInfo = new FileInfo(filepath);
//通過Excel表格的文件信息,打開Excel表格
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
//對文件操作
//創(chuàng)建表
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();
}//關(guān)閉文件
}
}
5.打包
如果dll文件再Editor下,unity打包不會打包Editor內(nèi)容。不會報錯。
如果不在,則需要將unity的.net2.0子集改為.net2.0,打包才不會出錯。文章來源:http://www.zghlxwxcb.cn/news/detail-482203.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-482203.html
到了這里,關(guān)于Excel表格和Unity的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!