WaiXie_QuestionSystem/Assets/Script/Excel/ExcelManager.cs

230 lines
8.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OfficeOpenXml;
using Script.Excel;
using Script.UI;
using UnityEngine;
using UnityEngine.UI;
using ZXL.Excel;
using ZXL.ID;
public class ExcelManager : SingleManager<ExcelManager>
{
private SingleChoice_QuestionBank_A_Data _aData;
private SingleChoice_QuestionBank_B_Data _bData;
private TureOrFalse_QuestionBank_C_Data _cData;
private MultipleChoice_QuestionBank_D_Data _dData;
private Player_Data _playerData;
public SingleChoice_QuestionBank_A_DataInfo GetRandomAInfo => _aData.RandomGet();
public SingleChoice_QuestionBank_B_DataInfo GetRandomBInfo => _bData.RandomGet();
public TureOrFalse_QuestionBank_C_DataInfo GetRandomCInfo => _cData.RandomGet();
public MultipleChoice_QuestionBank_D_DataInfo GetRandomDInfo => _dData.RandomGet();
public Player_DataInfo GetPlayerInfo => _playerData.GetAndRemove();
public ExcelManager()
{
var aData = ReadExcel.SelectATable("QuestionBank_A");
_aData = new SingleChoice_QuestionBank_A_Data(aData);
var bData = ReadExcel.SelectBTable("QuestionBank_B");
_bData = new SingleChoice_QuestionBank_B_Data(bData);
var cData = ReadExcel.SelectCTable("QuestionBank_C");
_cData = new TureOrFalse_QuestionBank_C_Data(cData);
var dData = ReadExcel.SelectDTable("QuestionBank_D");
_dData = new MultipleChoice_QuestionBank_D_Data(dData);
var payerData = ReadExcel.SelectPlayerTable("PlayerData");
_playerData = new Player_Data(payerData);
Debug.Log("excel data load finish !!");
}
public SingleChoice_QuestionBank_A_DataInfo GetRandomA()
{
return _aData.RandomGet(false);
}
#region 存入内容并导出Excel文件
public void SaveAndExp(SaveExcelData data) // 目前弃用了
{
string path = Application.streamingAssetsPath + "/程序输出数据" + DateTime.Now.Ticks + ".xlsx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
using (var pck = new ExcelPackage(fs))
{
var sheet = pck.Workbook.Worksheets.Add("Sheet1");
for (int i = 0; i < data.hands.Length; i++)
{
sheet.SetValue(1, i + 2, data.hands[i]);
}
for (var i = 0; i < data.infos.Count; i++)
{
for (var j = 0; j < data.infos[i].strings.Length; j++)
{
//第0行、列留白
// sheet.Cells[i + 2, j + 2].Value = data.infos[i].strings[j];
sheet.SetValue(i + 2, j + 2, data.infos[i].strings[j]);
}
}
pck.Save();
}
fs.Dispose();
}
public void SaveAndExpIndex(SaveDataInfo info, int index)
{
string path = Application.streamingAssetsPath + "/程序输出数据" + ".xlsx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
using (var pck = new ExcelPackage(fs))
{
ExcelWorksheet worksheet;
if (pck.Workbook.Worksheets.Count <= 0)
{
worksheet = pck.Workbook.Worksheets.Add("Sheet1");
}
else
{
worksheet = pck.Workbook.Worksheets[1];
}
if (index == 0)
{
string[] hands = new[] { "选手ID", "赛项", "科目", "第一题", "第二题", "第三题", "第四题", "第五题", "分数", "正确数量", "错误数量", "未答数量", "正确率", "用时" };
for (int i = 0; i < hands.Length; i++)
{
worksheet.SetValue(1, i + 2, hands[i]);
}
}
int j = 0;
worksheet.SetValue(index + 2, j + 2, info.id);
worksheet.SetValue(index + 2, j + 3, info.title);
worksheet.SetValue(index + 2, j + 4, info.subject);
int num = 0;
foreach (var infoAnswerData in info.answerDatas)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("题目:");
sb.AppendLine(infoAnswerData.title);
sb.AppendLine("选项:");
foreach (var option in infoAnswerData.options)
{
sb.AppendLine(option);
}
sb.AppendLine("正确答案:");
foreach (var answer in infoAnswerData.answers)
{
sb.AppendLine(answer);
}
sb.AppendLine("回答结果:");
foreach (var result in infoAnswerData.result)
{
sb.AppendLine(result);
}
worksheet.SetValue(index + 2, num + j + 5, sb.ToString());
num++;
}
worksheet.SetValue(index + 2, j + 10, info.score);
worksheet.SetValue(index + 2, j + 11, info.trueCount);
worksheet.SetValue(index + 2, j + 12, info.falseCount);
worksheet.SetValue(index + 2, j + 13, info.nullCount);
worksheet.SetValue(index + 2, j + 14, info.accuracy);
worksheet.SetValue(index + 2, j + 15, info.time);
pck.Save();
}
fs.Dispose();
}
public void SaveAndExpIndex(SaveData saveData)
{
List<SaveDataInfo> infos = saveData.DataInfos;
string path = Application.streamingAssetsPath + "/程序输出数据" + DateTime.Now.Ticks + ".xlsx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
using (var pck = new ExcelPackage(fs))
{
ExcelWorksheet worksheet;
if (pck.Workbook.Worksheets.Count <= 0)
{
worksheet = pck.Workbook.Worksheets.Add("Sheet1");
}
else
{
worksheet = pck.Workbook.Worksheets[1];
}
string[] hands = new[] { "选手ID", "赛项", "科目", "第一题", "第二题", "第三题", "第四题", "第五题", "分数", "正确数量", "错误数量", "未答数量", "正确率", "用时" };
for (int i = 0; i < hands.Length; i++)
{
worksheet.SetValue(1, i + 2, hands[i]);
}
int index = 0;
foreach (var info in infos)
{
int j = 0;
worksheet.SetValue(index + 2, j + 2, info.id);
worksheet.SetValue(index + 2, j + 3, info.title);
worksheet.SetValue(index + 2, j + 4, info.subject);
int num = 0;
foreach (var infoAnswerData in info.answerDatas)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("题目:");
sb.AppendLine(infoAnswerData.title);
sb.AppendLine("选项:");
foreach (var option in infoAnswerData.options)
{
sb.AppendLine(option);
}
sb.AppendLine("正确答案:");
foreach (var answer in infoAnswerData.answers)
{
sb.AppendLine(answer);
}
sb.AppendLine("回答结果:");
foreach (var result in infoAnswerData.result)
{
sb.AppendLine(result);
}
worksheet.SetValue(index + 2, num + j + 5, sb.ToString());
num++;
}
worksheet.SetValue(index + 2, j + 10, info.score);
worksheet.SetValue(index + 2, j + 11, info.trueCount);
worksheet.SetValue(index + 2, j + 12, info.falseCount);
worksheet.SetValue(index + 2, j + 13, info.nullCount);
worksheet.SetValue(index + 2, j + 14, info.accuracy);
worksheet.SetValue(index + 2, j + 15, info.time);
index++;
}
pck.Save();
}
fs.Dispose();
}
#endregion
}
//模拟数据写入