自用Unity工具:读取excel题库的答题模块

效果图:

自用Unity工具:读取excel题库的答题模块_第1张图片

代码(使用了插件EPPlus)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class EXCELReader : MonoBehaviour
{

    public static List QuesionsList=new List();
    public class QuesionClass
    {
        public string question, image, answer, analysis,BIJI;
    }
    public GameObject ButtonPrefab,ButtonParent;
    public InputField BIJI;
    public static string ReadingExcel;//正在读取的表格

    #region 按钮初始化

    public Button Full,Save,Delete,Answer,Next;
    public GameObject InputFiledOBJ,ShowAnswer,ShowTip;
    private bool IsFull;
    private List SavingQuestions = new List();//存储看过的题目
    private void ButtonUpdate()
    {
        Full.onClick.AddListener(() =>
        {
            InputFiledOBJ.transform.position =
                IsFull ? new Vector3(683.0f, -126.0f, 0.0f) : new Vector3(683.0f, 284.0f, 0.0f);
            IsFull = !IsFull;
        });
        Save.onClick.AddListener(() =>
        {

            FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath+"\\Questions\\题库我的收藏.xlsx");
            Debug.Log(fileInfo);
            using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
            {

                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
                int Row = worksheet.Dimension.End.Row+1;
                worksheet.Cells[Row, 1].Value =
                    EXCELReader.QuesionsList[EXCELReader.RandomNum].question;
                worksheet.Cells[Row, 2].Value =
                    EXCELReader.QuesionsList[EXCELReader.RandomNum].image;
                worksheet.Cells[Row, 3].Value =
                    EXCELReader.QuesionsList[EXCELReader.RandomNum].answer;
                worksheet.Cells[Row, 4].Value =
                    EXCELReader.QuesionsList[EXCELReader.RandomNum].analysis;
                worksheet.Cells[Row, 5].Value =
                    InputFiledOBJ.GetComponent().text;
                excelPackage.Save();
                //取得第一行第一列的数据
            }
        });
        
        Delete.onClick.AddListener(() =>
        {
            FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath+"\\Questions\\"+EXCELReader.ReadingExcel);
            Debug.Log(fileInfo);
            using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
            {
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
                if ( worksheet.Dimension.End.Row>1)
                {
                    Debug.Log(   worksheet.Cells[RandomNum+2, 5].Value.ToString());
                    worksheet.DeleteRow(EXCELReader.RandomNum+2);
                    excelPackage.Save();
                }
            }
            MakeQuestions(EXCELReader.ReadingExcel);
            UpdateBoard();


        });
        Answer.onClick.AddListener(() =>
        {
            ShowAnswer.SetActive(true);
            ShowTip.SetActive(true);
        });
        Next.onClick.AddListener(UpdateBoard);
    }

    #endregion
    void Start()
    {
        MakeButtons();
        ButtonUpdate();
    }
    /// 
    /// 根据读取到的表格制作Button
    /// 
    void MakeButtons()
    {
        foreach (var VARIABLE in ReadFile())
        {

            if (VARIABLE.Contains("题库"))
            {
                GameObject go = Instantiate(ButtonPrefab, ButtonParent.transform);
                go.name = VARIABLE.Substring(VARIABLE.LastIndexOf("题库"),VARIABLE.Length-VARIABLE.LastIndexOf("题库"));
                string g= go.name.Replace("题库", "");
                g = g.Replace(".xlsx", "");
                go.transform.Find("Text").GetComponent().text =g;
                go.GetComponent

组件布局:

自用Unity工具:读取excel题库的答题模块_第2张图片

你可能感兴趣的:(unity,excel,游戏引擎)