unity读取excel表格中的单元格内容

unity读取excel表格中的单元格内容

unity 的.net需要改为4.X
需要的dll文件如下,将文件夹放在Plugins目录下
Eplus下载链接
免费下载,骑士精神万岁

// 总行数
public int rowNum; 
// 总列数   
private int columnNum;
// 一张表    
private DataTable table;    
// 所有的题目   
public List<Subject> subjects;
/// <summary>
/// 读取第一张表,获取行数和列数,返回第一张表
/// </summary>
private DataTable ReadExcelGo(string filePath)
{
    IExcelDataReader excelReader = null;
    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
    var str = filePath.Split('.');
    if (str[1] == "xlsx")
    {
 
        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    }
    if (str[1] == "xls")
    {
        excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    }
 
    DataSet result = excelReader.AsDataSet();
 
    stream.Close();<br>     //获取有效的行列数
    columnNum = result.Tables[0].Columns.Count;
    rowNum = result.Tables[0].Rows.Count;
 
    return result.Tables[0];
}
/// <summary>
/// 生成题目
/// </summary>
private void SpawnSubject()
{
    table = ReadExcelGo(Application.streamingAssetsPath + "/exam.xlsx");
    for (int r = 1; r < rowNum; r++)
    {
        Subject sub = new Subject();
        //读取所有的行
        object[] rowObjs = table.Rows[r].ItemArray;
        //读取每一行的每一列
        for (int c = 0; c < columnNum; c++)
        {
            if (c == 0)
            {<br>                    //读取每个单元格的内容
                //第一列 为题目类型
                string str = rowObjs[c].ToString();
                switch (str)
                {
                    case "1": //单选
                        sub.subType = SubjectType.Single;
                        break;
                    case "2": //多选
                        sub.subType = SubjectType.Multiple;
                        break;
                    case "3": //判断
                        sub.subType = SubjectType.Judge;
                        break;
                }
            }
            else if (c == 1)
            {
                //第二列 为题干
                sub.subTitle = rowObjs[c].ToString();
            }
            else if (c == 2)
            {
                //第三列 为选项A
                sub.A = rowObjs[c].ToString();
            }
            else if (c == 3)
            {
                //第四列 为选项B
                sub.B = rowObjs[c].ToString();
            }
            else if (c == 4)
            {
                //第五列 为选项C
                sub.C = rowObjs[c].ToString();
            }
            else if (c == 5)
            {
                //第六列 为选项D
                sub.D = rowObjs[c].ToString();
            }
            else if (c == 6)
            {
                //第七列 为正确答案
                string str = rowObjs[c].ToString();
                sub.currentAnswer = str.Split(',');
            }
            else if (c == 7)
            {
                //第八列 为分值
                sub.score = float.Parse(rowObjs[c].ToString());
            }
 
        }
        subjects.Add(sub);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity读取Excel表格可以使用第三方插件,比如ExcelDataReader和NPOI等。以下是使用ExcelDataReader读取Excel表格的步骤: 1. 在Unity导入ExcelDataReader插件。可以在Unity Asset Store搜索ExcelDataReader并下载导入。 2. 准备需要读取Excel表格文件,将其拖拽到Unity项目的Assets文件夹。 3. 编写脚本,使用ExcelDataReader读取Excel表格数据。以下是一个简单的示例: ```csharp using UnityEngine; using System.IO; using System.Data; using ExcelDataReader; public class ExcelReader : MonoBehaviour { public string fileName; // Excel文件名 void Start() { // 构造Excel文件路径 string filePath = Path.Combine(Application.dataPath, fileName); // 读取Excel文件 FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); // 创建ExcelReader对象 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); // 读取表格数据 DataSet result = excelReader.AsDataSet(); // 输出表格数据 foreach (DataTable table in result.Tables) { foreach (DataRow row in table.Rows) { foreach (DataColumn col in table.Columns) { Debug.Log(row[col]); } } } // 关闭ExcelReader和文件流 excelReader.Close(); stream.Close(); } } ``` 在上述示例,我们使用ExcelDataReader的CreateOpenXmlReader方法创建了一个ExcelReader对象,并传入了Excel表格的FileStream。然后,我们使用AsDataSet方法读取表格数据,并通过遍历DataTable、DataRow和DataColumn输出了表格数据。最后,我们需要手动关闭ExcelReader和文件流。 需要注意的是,在使用ExcelDataReader读取Excel表格时,需要根据Excel表格的格式选择不同的读取方法。例如,如果需要读取xls格式的Excel表格,则需要使用CreateBinaryReader方法创建ExcelReader对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值