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

unity中必须要有Plugins文件夹以及dll文件
官方dll文件下载链接: https://archive.codeplex.com/?p=exceldatareader
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,操作更方便哦

 

 

 

转载于:https://www.cnblogs.com/qq2351194611/p/10784096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值