使用EPPlus脚本的TypeInitializationException

时间:2017-02-15 21:26:02

标签: c# excel visual-studio epplus

我正在尝试运行脚本以使用EPPlus从Excel工作表中读取值并将其加载到元组列表中。 但是,当我运行脚本时,我遇到两个错误,第一个是:

An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll

我在其他帖子中看到我需要检查内部异常,但Visual Studio 15没有提供。 这是所有可用的异常细节。

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'CGCompare2.Program' threw an exception.

然后,当我关闭VS15异常窗口时,我会弹出一个对话框:

Cannot access a disposed object.
Object name: 'HwndSourceAdapter'

我不确定问题是什么,如果这是由我的代码引起的。任何帮助,非常感谢。

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;

namespace CGComparer
{
    class Program
    {
        private static List<Tuple<string, string>> _listTop;
        private static List<Tuple<string, string>> _listGNED;
        private static Base _baseCell;
        private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
        private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "Compare GNED and TOP V1.0.xlsx");

        static void Main(string[] args)
        {
            _baseCell = new Base(1, 2);
            _listTop = ColumnsToList(_baseCell.Column(),_baseCell.Row());

            _baseCell = new Base(3, 2);
            _listGNED = ColumnsToList(_baseCell.Column(),_baseCell.Row());
        }

        public static List<Tuple<string, string>> ColumnsToList(int column, int row)
        {
            var list = new List<Tuple<string, string>>();
            var ws = _package.Workbook.Worksheets[1];
            var ListIsValid = true;
            do
            {
                var userEmail = (string)ws.Cells[column, row].Value;
                var customerGroup = (string)ws.Cells[column + 1, row].Value;

                if (!string.IsNullOrEmpty(userEmail))
                {
                    list.Add(new Tuple<string, string>(userEmail, customerGroup));
                    row = row++;
                }
                else
                {
                    ListIsValid = false;
                }

            } while (ListIsValid);

            return list;
        }
    }
}

Base.cs

namespace CGComparer
{
    public class Base
    {
        private static int _column;
        private static int _row;

        public Base(int column, int row)
        {
            _column = column;
            _row = row;
        }

        public int Column()
        {
            return _column;
        }

        public int Row()
        {
            return _row;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

所以,事实证明这个问题一直盯着我,我经历了以下步骤,Hans Passant在问题评论中说:

  

&#34; VS2015中的调试器是一个蹩脚的包包,它不会让你看起来   在InnerException。使用工具&gt;选项&gt;调试&gt;一般&gt;   勾选&#34;使用托管兼容模式&#34;现在你可以看到它。小心   使用这些静态,他们的初始化程序可以在后端字节   严重&#34;

这是一个空引用异常,是由于我声明了一个路径参数尚未声明的excel文件。

private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "Compare GNED and TOP V1.0.xlsx");