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[0].Rows.Count;//行 int colums = resultds.Tables[0].Columns.Count;//列 for (int i = 0; i < rows; i++) { DepenceTableDataTwo dependData; dependData.itemNumber = resultds.Tables[0].Rows[i][0].ToString(); dependData.topicType = resultds.Tables[0].Rows[i][1].ToString(); dependData.topic = resultds.Tables[0].Rows[i][2].ToString(); if (dependData.topicType =="选择") { dependData.optionA = resultds.Tables[0].Rows[i][3].ToString(); dependData.optionB = resultds.Tables[0].Rows[i][4].ToString(); dependData.optionC = resultds.Tables[0].Rows[i][5].ToString(); dependData.optionD = resultds.Tables[0].Rows[i][6].ToString(); dependData.answer = resultds.Tables[0].Rows[i][7].ToString(); } else { dependData.optionA = string.Empty; dependData.optionB = string.Empty; dependData.optionC = string.Empty; dependData.optionD = string.Empty; dependData.answer = resultds.Tables[0].Rows[i][3].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 = 0; i < DepenceTableDataList.Count; i++) { worksheet.Cells[i + 1, 1].Value = DepenceTableDataList[i].itemNumber.ToString (); worksheet.Cells[i + 1, 2].Value = DepenceTableDataList[i].topicType.ToString (); worksheet.Cells[i + 1, 3].Value = DepenceTableDataList[i].topic.ToString (); if (DepenceTableDataList[i].topicType == "选择") { worksheet.Cells[i + 1, 4].Value = DepenceTableDataList[i].optionA .ToString(); worksheet.Cells[i + 1, 5].Value = DepenceTableDataList[i].optionB .ToString(); worksheet.Cells[i + 1, 6].Value = DepenceTableDataList[i].optionC .ToString(); worksheet.Cells[i + 1, 7].Value = DepenceTableDataList[i].optionD .ToString(); worksheet.Cells[i + 1, 8].Value = DepenceTableDataList[i].answer .ToString(); } else { worksheet.Cells[i + 1, 4].Value = DepenceTableDataList[i].answer.ToString(); } } package.Save(); } }
}
下面附上自己做的Demo地址,不是很完善,但里面也没有错误,大佬们不要介意,感谢!
这是链接:
链接:https://pan.baidu.com/s/1CPLIVsvMzWUknQAi0Ekgjg
提取码:bkod
复制这段内容后打开百度网盘手机App,操作更方便哦