using UnityEngine; using Excel; using System.Data; using System.IO; using System.Collections.Generic; using System.Linq; using OfficeOpenXml; using OfficeOpenXml.Table; using Unity.VisualScripting; namespace ZXL.Excel { public class ReadExcel { //public static string Excel = "Book"; public static List SelectPlayerTable(string name, string sheetName = "Sheet1") { string excelName = name + ".xlsx"; // DataRowCollection collect = ReadExcel.Read(excelName, sheetName); // // List array = new List(); // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // string id = collect[i][1].ToString(); // string playerName = collect[i][2].ToString(); // Player_DataInfo info = new Player_DataInfo( // id, // playerName // ); // array.Add(info); // } List array = new List(); var objs = ReadExcel.Read(excelName, sheetName); // var cellsValue = cells.Value; // object[,] objs = cells.Value as object[,]; var xLeng = objs.GetLength(1); var yLeng = objs.GetLength(0); for (var i = 2; i < yLeng; i++) //列 { string id = objs[i, 1].ToString(); string playerName = objs[i, 2].ToString(); Player_DataInfo info = new Player_DataInfo( id, playerName ); array.Add(info); // for (var j = 1; j < xLeng; j++) // 行 // { // if (objs[i, j] == null) continue; // // } } return array; } public static List SelectATable(string name, string sheetName = "Sheet1") { string excelName = name + ".xlsx"; // DataRowCollection collect = ReadExcel.Read(excelName, sheetName); // // List array = new List(); // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // string id = collect[i][1].ToString(); // string topic = collect[i][2].ToString(); // string[] options = collect[i][3].ToString().Split("\n"); // string[] answer = collect[i][4].ToString().Split(","); // SingleChoice_QuestionBank_A_DataInfo info = new SingleChoice_QuestionBank_A_DataInfo( // id, // topic, // options, // answer // ); // array.Add(info); // } List array = new List(); var objs = ReadExcel.Read(excelName, sheetName); // var cellsValue = cells.Value; // object[,] objs = cells.Value as object[,]; var xLeng = objs.GetLength(1); var yLeng = objs.GetLength(0); for (var i = 2; i < yLeng; i++) //列 { string id = objs[i, 1].ToString(); string topic = objs[i, 2].ToString(); string[] options = objs[i, 3].ToString().Split("\n"); string[] answer = objs[i, 4].ToString().Split(","); SingleChoice_QuestionBank_A_DataInfo info = new SingleChoice_QuestionBank_A_DataInfo( id, topic, options, answer ); array.Add(info); // for (var j = 1; j < xLeng; j++) // 行 // { // Debug.Log($"{nameof(SingleChoice_QuestionBank_A_DataInfo)} data : {i},{j}"); // if (objs[i, j] == null) continue; // // } } return array; } public static List SelectBTable(string name, string sheetName = "Sheet1") { string excelName = name + ".xlsx"; // DataRowCollection collect = ReadExcel.Read(excelName, sheetName); // // List array = new List(); // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // string id = collect[i][1].ToString(); // string topic = collect[i][2].ToString(); // string[] options = collect[i][3].ToString().Split(";"); // string[] answer = collect[i][4].ToString().Split(","); // SingleChoice_QuestionBank_B_DataInfo info = new SingleChoice_QuestionBank_B_DataInfo( // id, // topic, // options, // answer // ); // array.Add(info); // } List array = new List(); var objs = ReadExcel.Read(excelName, sheetName); // var cellsValue = cells.Value; // object[,] objs = cells.Value as object[,]; var xLeng = objs.GetLength(1); var yLeng = objs.GetLength(0); for (var i = 2; i < yLeng; i++) //列 { string id = objs[i, 1].ToString(); string topic = objs[i, 2].ToString(); string[] options = objs[i, 3].ToString().Split(";"); string[] answer = objs[i, 4].ToString().Split(","); SingleChoice_QuestionBank_B_DataInfo info = new SingleChoice_QuestionBank_B_DataInfo( id, topic, options, answer ); array.Add(info); // for (var j = 1; j < xLeng; j++) // 行 // { // if (objs[i, j] == null) continue; // // } } return array; } public static List SelectCTable(string name, string sheetName = "Sheet1") { string excelName = name + ".xlsx"; // DataRowCollection collect = ReadExcel.Read(excelName, sheetName); // // List array = new List(); // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // string id = collect[i][1].ToString(); // string topic = collect[i][2].ToString(); // string[] options = collect[i][3].ToString().Split(";"); // string[] answer = collect[i][4].ToString().Split(","); // TureOrFalse_QuestionBank_C_DataInfo info = new TureOrFalse_QuestionBank_C_DataInfo( // id, // topic, // options, // answer // ); // array.Add(info); // } List array = new List(); var objs = ReadExcel.Read(excelName, sheetName); // var cellsValue = cells.Value; // object[,] objs = cells.Value as object[,]; var xLeng = objs.GetLength(1); var yLeng = objs.GetLength(0); for (var i = 2; i < yLeng; i++) //列 { string id = objs[i, 1].ToString(); string topic = objs[i, 2].ToString(); string[] options = objs[i, 3].ToString().Split(";"); string[] answer = objs[i, 4].ToString().Split(","); TureOrFalse_QuestionBank_C_DataInfo info = new TureOrFalse_QuestionBank_C_DataInfo( id, topic, options, answer ); array.Add(info); // for (var j = 1; j < xLeng; j++) // 行 // { // if (objs[i, j] == null) continue; // // } } return array; } public static List SelectDTable(string name, string sheetName = "Sheet1") { string excelName = name + ".xlsx"; List array = new List(); var objs = ReadExcel.Read(excelName, sheetName); // var cellsValue = cells.Value; // object[,] objs = cells.Value as object[,]; var xLeng = objs.GetLength(1); var yLeng = objs.GetLength(0); for (var i = 2; i < yLeng; i++) //列 { string id = objs[i, 1].ToString(); string topic = objs[i, 2].ToString(); string[] options = objs[i, 3].ToString().Split(";"); string[] answer = objs[i, 4].ToString().Split(","); MultipleChoice_QuestionBank_D_DataInfo info = new MultipleChoice_QuestionBank_D_DataInfo( id, topic, options, answer ); array.Add(info); Debug.Log(id); // for (var j = 1; j < xLeng; j++) // 行 // { // if (objs[i, j] == null) continue; // // } } return array; // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // string id = collect[i][1].ToString(); // string topic = collect[i][2].ToString(); // string[] options = collect[i][3].ToString().Split(";"); // string[] answer = collect[i][4].ToString().Split(","); // MultipleChoice_QuestionBank_D_DataInfo info = new MultipleChoice_QuestionBank_D_DataInfo( // id, // topic, // options, // answer // ); // array.Add(info); // } // return array; } //查询menu表 // public static List SelectMenuTable(string name, string sheetName) // { // string excelName = name + ".xlsx"; // // DataRowCollection collect = ReadExcel.Read(excelName, sheetName); // // List menuArray = new List(); // for (int i = 2; i < collect.Count; i++) // { // if (collect[i][1].ToString() == "") continue; // // Menu menu = new Menu // { // m_Id = collect[i][0].ToString(), // m_level = collect[i][1].ToString(), // m_parentId = collect[i][2].ToString(), // m_name = collect[i][3].ToString() // }; // menuArray.Add(menu); // } // // return menuArray; // } /// /// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll; /// /// excel文件名 /// sheet名称 /// DataRow的集合 static object[,] Read(string excelName, string sheetName) { object[,] objs = new object[,] { }; using FileStream fs = new FileStream(Application.streamingAssetsPath + "/Excel/" + excelName, FileMode.Open, FileAccess.Read, FileShare.Read); using (var pck = new ExcelPackage(fs)) { var sheet = pck.Workbook.Worksheets[sheetName]; // var excelRange = sheet.Cells["b:g"]; // int row = excelRange.Count(x => x.Address.Length == 2 && x.Address[1] == '2'); // var enumerable = excelRange.Select((x, i) => // { // if (x.Address.Length == 2 && x.Address[1] == '1') // { // return null; // } // // return x.Value.ToString(); // }); // var strings = enumerable.Where(x => x is not null).ToArray(); // List list = new List(); // var stringsLength = strings.Length; // for (var i = 0; (i + row - 1) < stringsLength; i += row) // { // var strings1 = new string[row]; // for (var i1 = 0; i1 < row; i1++) // { // strings1[i1] = strings[i + i1]; // } // // list.Add(strings1); // } // // foreach (var strings1 in list) // { // Debug.Log(string.Join(',', strings1)); // } objs = sheet.Cells.Value as object[,]; return objs; } // string path = Application.streamingAssetsPath + "/Excel/" + excelName; // //string path = Application.dataPath + "/" + excelName; // FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); // IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); // // DataSet result = excelReader.AsDataSet(); // //int columns = result.Tables[0].Columns.Count; // //int rows = result.Tables[0].Rows.Count; // // //tables可以按照sheet名获取,也可以按照sheet索引获取 // //return result.Tables[0].Rows; // return result.Tables[sheetName].Rows; } private static DataTable GetDataTable() { return new DataTable(""); } /// /// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll; /// /// excel文件名 /// sheet索引 /// DataRow的集合 static DataRowCollection Read(string excelName, int sheetIndex) { string path = Application.streamingAssetsPath + "/Excel/" + excelName; //string path = Application.dataPath + "/" + excelName; FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); //int columns = result.Tables[0].Columns.Count; //int rows = result.Tables[0].Rows.Count; //tables可以按照sheet名获取,也可以按照sheet索引获取 //return result.Tables[0].Rows; return result.Tables[sheetIndex].Rows; } } }