从外面更新unity需要用的题库

unity中必须要有Plugins文件夹以及dll文件

官方dll文件下载链接: https://archive.codeplex.com/?p=exceldatareader


dll下载:点击下载


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,操作更方便哦

 

 

 

二.新的读取表格方式 (简单明了)

using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class ReadExcelController1 : MonoBehaviour
{
  
    public Transform cubetrans;
    Dictionary<string, string > tableData = new Dictionary<string, string>();
    // Start is called before the first frame update
    void Start()
    {
        ReadDataGame(ExcelControl(Application.streamingAssetsPath + "/大华设备统计.xlsx"));
        //BianLiModel();
    }
    public void BianLiModel()
    {
        foreach (var item in cubetrans.GetComponentsInChildren <MeshRenderer>())
        {
            if (tableData.ContainsKey(item.name))
            {
                item.name = tableData[item.name];
            }
        }
      
    }
    /// <summary>
    /// 表格数据集合
    /// </summary>
    //private DataSet mResultSet;

    /// <summary>
    /// 读取表数据
    /// </summary>
    /// <param name="excelFile">Excel file.</param>
    public DataSet ExcelControl(string excelFile)
    {
        DataSet mResultSet=new DataSet ();
        FileStream mStream = File.Open(excelFile, FileMode.Open, FileAccess.Read);
        IExcelDataReader mExcelReader = ExcelReaderFactory.CreateOpenXmlReader(mStream);
        mResultSet = mExcelReader.AsDataSet();
        return mResultSet;
    }
    public void ReadDataGame(DataSet mResultSet)
    {
        if (mResultSet.Tables.Count < 1)
            return;

        //默认读取第一个数据表
        DataTable mSheet = mResultSet.Tables[5];

        //判断数据表内是否存在数据
        if (mSheet.Rows.Count < 1)
            return;

        //读取数据表行数和列数
        int rowCount = mSheet.Rows.Count;
        int colCount = mSheet.Columns.Count;
        Debug.Log("行:" + rowCount + "列:" + colCount);
        //准备一个列表存储整个表的数据
        List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();

        //读取数据
        for (int i = 1; i < rowCount; i++)
        {
            //准备一个字典存储每一行的数据
            Dictionary<string, object> row = new Dictionary<string, object>();
            string strname="";
            string strname1 = "";
            for (int j = 0; j < 2; j++)
            {
                //读取第1行数据作为表头字段
                string field = mSheet.Rows[0][j].ToString();
         
                //Key-Value对应
                row[field] = mSheet.Rows[i][j];
                //因为我只需要表格前两个数据,所以只保存前两个,有需要全部的把上面的for j<2 改成j<colCount就行了
                strname = (mSheet.Rows[i][0].ToString());
                strname1 = (mSheet.Rows[i][1].ToString());

            }
            Debug.Log(strname1 + "  " + strname);
            if (!tableData.ContainsKey (strname1))
            {
                tableData.Add(strname1, strname);
            }
           
            //添加到表数据中
            table.Add(row);
        }
        Debug.Log("添加结束");
    }
    private void Update()
    {

        if (Input.GetKeyDown (KeyCode.K))
        {
            BianLiModel();
        }
    }
}

 

posted @ 2019-04-28 15:07  剑起苍穹  阅读(513)  评论(0编辑  收藏  举报
/*鼠标点击特效*/