weixin_33538392 2018-07-31 03:59 采纳率: 100%
浏览 1754
已采纳

C# 用NPOI读取Excel的问题

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NUnit.Framework;
using System.Text;

public class ExcelManager
{

    public List<string> _valType;
    public List<string> _valName;
    public string _className;
    public List<List<string>> _data;
    public int _invalidLine = 0;
    public int _dataIndex = -1;

    public StringBuilder _classSource;

    public void ReadExcel()
    {
        _valType = new List<string>();
        _valName = new List<string>();
        string path = null;
        _data = new List<List<string>>();

        string[] paths = Directory.GetFiles(@"./Assets/Resources/");
        foreach (var item in paths)
        {
            string extension = Path.GetExtension(item);
            if (extension == ".xlsx" || extension == ".xls")
            {
                path = item;
                break;
            }
        }

        IWorkbook workbook = null;
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        if (path.IndexOf(".xlsx") > 0)
        {
            //2007版
            workbook = new XSSFWorkbook(fileStream);
        }
        else if (path.IndexOf(".xls") > 0)
        {
            //2003版
            workbook = new HSSFWorkbook(fileStream);
        }
        else
        {
            Debug.LogError("Invalid path.");
        }

        ISheet sheet = workbook.GetSheetAt(0);
        _className = sheet.SheetName.Substring(1, sheet.SheetName.Length - 1);

        IRow row = sheet.GetRow(0);
        for (int i = 0; i <= sheet.LastRowNum; ++i)
        {
            row = sheet.GetRow(i);

            if (row != null)
            {   
                //若该行首列内容以'#'开头,则为无效列
                var tmp = row.GetCell(0).ToString();
                if (tmp[0] == '#')
                {
                    ++_invalidLine;
                    continue;
                }

                for (int j = 0; j < row.LastCellNum; ++j)
                {
                    var cellValue = row.GetCell(j).ToString();

                    if (cellValue[0] == '&')
                    {
                        //内容以'&'开头则记录变量名和类型
                        int index = cellValue.IndexOf('|');
                        string tmpVarName = cellValue.Substring(1, index - 1);
                        string tmpVarType = cellValue.Substring(index + 1, cellValue.Length - (index + 1));

                        _valType.Add(tmpVarType);
                        _valName.Add(tmpVarName);
                    }
                    else
                    {
                        //否则记录数据
                        if (_data.Count < i - _invalidLine)
                        {
                            ++_dataIndex;
                            _data.Add(new List<string>());
                            _data[_dataIndex].Add(cellValue);
                        }
                        else
                        {
                            _data[_dataIndex].Add(cellValue);
                        }
                    }
                }
            }
        }

        fileStream.Close();
        workbook.Close();
    }

    public void GetClassSource()
    {
        _classSource = new StringBuilder();

        _classSource.Append("using System;\n");
        _classSource.Append("using UnityEngine;\n");
        _classSource.Append("using System.Collections;\n");
        _classSource.Append("using System.Collections.Generic;\n\n");
        _classSource.Append("public class " + _className + " : ScriptableObject\n");
        _classSource.Append("{\n");
        _classSource.Append("\t[System.Serializable]\n");
        _classSource.Append("\tpublic class Field\n");
        _classSource.Append("\t{\n");
        int len = _valType.Count;
        for (int i = 0; i < len; ++i)
        {
            _classSource.Append("\t\tpublic " + _valType[i] + " " + _valName[i] + ";\n");
        }
        _classSource.Append("\t}\n");
        _classSource.Append("\n\t[SerializeField]\n");
        _classSource.Append("\tpublic List<Field> Fields;\n");
        _classSource.Append("}\n");
    }
}

在unity中做的。
请看第69行,按照https://blog.csdn.net/dcrmg/article/details/52356236 做的,原本还好好的,能够读取第i行首列的数据,但是今天莫名其妙开始报错:Object reference not set an instance of an object.

var tmp = row.GetCell(0).ToString();我尝试用Debug.Log(row.GetCell(0).ToString())
打印,发现打印了一整列的数据。

不应该取得的是该行首列的数据吗?代码什么都没改过为啥就用不了了。

链接: https://pan.baidu.com/s/1oQ7MhWbzBqXB2iutxgRs5Q 密码: h2jr
整个工程文件

  • 写回答

4条回答 默认 最新

  • 逍遥岚轩 2018-08-01 02:05
    关注

    既然之前是没问题的,那多半是有不合法的数据,或者是程序没有针对不合法的数据进行容错。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用