WaiXie_QuestionSystem/Assets/Script/Excel/Read/ReadExcel.cs

267 lines
9.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<Player_DataInfo> SelectPlayerTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<Player_DataInfo> array = new List<Player_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //ÁÐ
{
string id = objs[i, 1].ToString().Replace(" ", "");
string playerName = objs[i, 2].ToString().Replace(" ", "");
Player_DataInfo info = new Player_DataInfo(
id,
playerName
);
array.Add(info);
}
return array;
}
public static List<SingleChoice_QuestionBank_A_DataInfo> SelectATable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<SingleChoice_QuestionBank_A_DataInfo> array = new List<SingleChoice_QuestionBank_A_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //ÁÐ
{
string id = objs[i, 1].ToString().Replace(" ", "");
string topic = objs[i, 2].ToString().Replace(" ", "");
// var strings = objs[i, 3].ToString().Replace(" ", "").Split("\n");
string[] options = ConvertToStrings_Option(objs[i, 3].ToString());
CheckLog(objs[i, 3].ToString(), options, 4, i);
string[] answer = ConvertToStrings_Answer(objs[i, 4].ToString());
SingleChoice_QuestionBank_A_DataInfo info = new SingleChoice_QuestionBank_A_DataInfo(
id,
topic,
options,
answer
);
array.Add(info);
}
return array;
}
static string[] ConvertToStrings_Option(string str)
{
string s = str;
s = str.Replace(" ", "");
s = s.Replace("\n", "");
var strings = s.Split(";");
var removeNullStrings = RemoveNullStrings(strings);
return removeNullStrings;
}
static string[] ConvertToStrings_Answer(string str)
{
string s = str;
s = str.Replace(" ", "");
s = s.Replace("\n", "");
var strings = s.Split(",");
var removeNullStrings = RemoveNullStrings(strings);
return removeNullStrings;
}
static string[] RemoveNullStrings(string[] strings)
{
List<string> list = new List<string>();
for (var i = 0; i < strings.Length; i++)
{
if (strings[i] != "")
list.Add(strings[i]);
}
return list.ToArray();
}
static bool CheckLog(string str, string[] strings, int count, int number)
{
if (strings.Length != count)
{
Debug.Log($"{str}--{strings}ÕâÀï²»¶Ô¾¢{strings.Length}£¬ËÙ²é{number}");
return false;
}
return true;
}
public static List<SingleChoice_QuestionBank_B_DataInfo> SelectBTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<SingleChoice_QuestionBank_B_DataInfo> array = new List<SingleChoice_QuestionBank_B_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //ÁÐ
{
string id = objs[i, 1].ToString().Replace(" ", "");
string topic = objs[i, 2].ToString().Replace(" ", "");
string[] options = ConvertToStrings_Option(objs[i, 3].ToString());
CheckLog(objs[i, 3].ToString(), options, 4, i);
string[] answer = ConvertToStrings_Answer(objs[i, 4].ToString());
SingleChoice_QuestionBank_B_DataInfo info = new SingleChoice_QuestionBank_B_DataInfo(
id,
topic,
options,
answer
);
array.Add(info);
}
return array;
}
public static List<TureOrFalse_QuestionBank_C_DataInfo> SelectCTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<TureOrFalse_QuestionBank_C_DataInfo> array = new List<TureOrFalse_QuestionBank_C_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //ÁÐ
{
string id = objs[i, 1].ToString().Replace(" ", "");
string topic = objs[i, 2].ToString().Replace(" ", "");
string[] options = ConvertToStrings_Option(objs[i, 3].ToString());
CheckLog(objs[i, 3].ToString(), options, 2, i);
string[] answer = ConvertToStrings_Answer(objs[i, 4].ToString());
TureOrFalse_QuestionBank_C_DataInfo info = new TureOrFalse_QuestionBank_C_DataInfo(
id,
topic,
options,
answer
);
array.Add(info);
}
return array;
}
public static List<MultipleChoice_QuestionBank_D_DataInfo> SelectDTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<MultipleChoice_QuestionBank_D_DataInfo> array = new List<MultipleChoice_QuestionBank_D_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //ÁÐ
{
string id = objs[i, 1].ToString().Replace(" ", "");
string topic = objs[i, 2].ToString().Replace(" ", "");
string[] options = ConvertToStrings_Option(objs[i, 3].ToString());
CheckLog(objs[i, 3].ToString(), options, 5, i);
string[] answer = ConvertToStrings_Answer(objs[i, 4].ToString());
MultipleChoice_QuestionBank_D_DataInfo info = new MultipleChoice_QuestionBank_D_DataInfo(
id,
topic,
options,
answer
);
array.Add(info);
}
return array;
}
/// <summary>
/// ¶ÁÈ¡ Excel ; ÐèÒªÌí¼Ó Excel.dll; System.Data.dll;
/// </summary>
/// <param name="excelName">excelÎļþÃû</param>
/// <param name="sheetName">sheetÃû³Æ</param>
/// <returns>DataRowµÄ¼¯ºÏ</returns>
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];
objs = sheet.Cells.Value as object[,];
return objs;
}
}
void CheckAndRemovePunctuationMark(string str)
{
}
private static DataTable GetDataTable()
{
return new DataTable("");
}
/// <summary>
/// ¶ÁÈ¡ Excel ; ÐèÒªÌí¼Ó Excel.dll; System.Data.dll;
/// </summary>
/// <param name="excelName">excelÎļþÃû</param>
/// <param name="sheetName">sheetË÷Òý</param>
/// <returns>DataRowµÄ¼¯ºÏ</returns>
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;
}
}
}