unity中必须要有Plugins文件夹以及dll文件
官方dll文件下载链接: https://archive.codeplex.com/?p=exceldatareader
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Excel;
using System.IO;
using System.Data;
using OfficeOpenXml; public class Exc_GenXin : MonoBehaviour {
private string DpanMain = "D:/anchor/main.xlsx";//D盘main表格
private string AsstPath;//streaamingAssets文件里面的文件
public static List<DepenceTableDataTwo> DepenceTableDataList = new List<DepenceTableDataTwo>();//创建列表
private void Awake()
{
AsstPath = Application.streamingAssetsPath + "/main.xlsx";
}
private void OnEnable()
{
if (File.Exists(DpanMain))
{
DepenceTableDataList = ReadLoad(DpanMain);
WriteExcel(AsstPath);
Debug.Log("更新成功");
}
} // Use this for initialization
void Start () { }
public void AANniu()
{ }
public struct DepenceTableDataTwo
{
public string itemNumber;//标号
public string topicType;//类型
public string topic;//题目
public string optionA;//A
public string optionB;//B
public string optionC;//C
public string optionD;//D
public string answer;//答案
}
/// <summary>
/// 读取表格
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public static DataSet ReadExcel(string path)
{
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet(); return result; }
public static List <DepenceTableDataTwo> ReadLoad(string path)
{
List<DepenceTableDataTwo> _data = new List<DepenceTableDataTwo>();
DataSet resultds = ReadExcel(path);
int rows = resultds.Tables[].Rows.Count;//行
int colums = resultds.Tables[].Columns.Count;//列
for (int i = ; i < rows; i++)
{
DepenceTableDataTwo dependData;
dependData.itemNumber = resultds.Tables[].Rows[i][].ToString();
dependData.topicType = resultds.Tables[].Rows[i][].ToString();
dependData.topic = resultds.Tables[].Rows[i][].ToString();
if (dependData.topicType =="选择")
{
dependData.optionA = resultds.Tables[].Rows[i][].ToString();
dependData.optionB = resultds.Tables[].Rows[i][].ToString();
dependData.optionC = resultds.Tables[].Rows[i][].ToString();
dependData.optionD = resultds.Tables[].Rows[i][].ToString();
dependData.answer = resultds.Tables[].Rows[i][].ToString();
}
else
{
dependData.optionA = string.Empty;
dependData.optionB = string.Empty;
dependData.optionC = string.Empty;
dependData.optionD = string.Empty;
dependData.answer = resultds.Tables[].Rows[i][].ToString();
}
_data.Add(dependData);
}
return _data;
}
/// <summary>
/// 写入表格
/// </summary>
/// <param name="path">路径</param>
public static void WriteExcel(string outpath)
{
FileInfo newFile = new FileInfo(outpath);
if (newFile .Exists )
{
newFile.Delete();
newFile = new FileInfo(outpath);
}
using (ExcelPackage package=new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
for (int i = ; i < DepenceTableDataList.Count; i++)
{
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].itemNumber.ToString ();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].topicType.ToString ();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].topic.ToString ();
if (DepenceTableDataList[i].topicType == "选择")
{
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].optionA .ToString();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].optionB .ToString();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].optionC .ToString();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].optionD .ToString();
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].answer .ToString();
}
else
{
worksheet.Cells[i + , ].Value = DepenceTableDataList[i].answer.ToString();
}
}
package.Save();
}
}
}
下面附上自己做的Demo地址,不是很完善,但里面也没有错误,大佬们不要介意,感谢!
这是链接:
链接:https://pan.baidu.com/s/1CPLIVsvMzWUknQAi0Ekgjg
提取码:bkod
复制这段内容后打开百度网盘手机App,操作更方便哦