Unity读取和写入Excel表格的示例 需要使用到的dll为 EPPlus.dll Excel.dll ICSharpCode.SharpZipLib.dll I18N.West.dll I18N.Rare.dll I18N.Other.dll I18N.MidEast.dll I18N.dll I18N.CJK.dll 示例代买为如下
读取脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using System.IO;
using Excel;
public class DoExcel
{
public static DataSet ReadExcel(string path)
{
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //读取2007以后版本
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); //读取2003以后版本
DataSet result = excelReader.AsDataSet();
excelReader.Close();
int columns = result.Tables[0].Columns.Count;
int rows = result.Tables[0].Rows.Count;
for (int i = 0; i < rows; i++)
{
string values = "I = " + (i + 1);
for (int j = 0; j < columns; j++)
{
values +=" " + result.Tables[0].Rows[i][j].ToString();
}
Debug.Log(values);
}
return result;
}
}
写入脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using OfficeOpenXml;
using System.IO;
public class PrintExcel : MonoBehaviour
{
void Start()
{
//写
WriteExcel(Application.streamingAssetsPath + "/Test2007.xlsx");
DoExcel.ReadExcel(Application.streamingAssetsPath + "/Test2007.xlsx");
}
public static void WriteExcel(string outputDir)
{
FileInfo newFile = new FileInfo(outputDir);
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "姓名";
worksheet.Cells[1, 3].Value = "年龄";
worksheet.Cells[1, 4].Value = "性别";
worksheet.Cells[1, 5].Value = "出身日期";
worksheet.Cells["A2"].Value = 12001;
worksheet.Cells["B2"].Value = "张三";
worksheet.Cells["C2"].Value = 22;
worksheet.Cells["D2"].Value = "男";
worksheet.Cells["E2"].Value = "1995-12-11";
worksheet.Cells["A3"].Value = 12002;
worksheet.Cells["B3"].Value = "李四";
worksheet.Cells["C3"].Value = 20;
worksheet.Cells["D3"].Value = "女";
worksheet.Cells["E3"].Value = "1996-01-12";
worksheet.Cells["A4"].Value = 12003;
worksheet.Cells["B4"].Value = "王五";
worksheet.Cells["C4"].Value = 24;
worksheet.Cells["D4"].Value = "男";
worksheet.Cells["E4"].Value = "1997-02-13";
package.Save();
}
}
}
使用的dll的地址链接:https://pan.baidu.com/s/1-JhGvicHbDKGcYuyPbNJ4A
提取码:3hug