EPPlus 阵列维度超出了支持的范围。 System.OutOfMemoryException

EPPlus Array dimensions exceeded supported range. System.OutOfMemoryException

好的,所以我正在尝试将 CSVStream 加载到 ExcelPackage 中(我正在使用 EPPlus)。

无论我选择什么选项,它总是在第 221482 行失败。我在 x64 上 运行,我的 app.config...

给出的错误是标题中的错误:(

    public ExcelPackage ExcelPackageFromCsvStream(Stream csvStream)
    {
        var excelPackage = new ExcelPackage();
        var workSheet = excelPackage.Workbook.Worksheets.Add("Sheet1");

        var csvFormat = new ExcelTextFormat
        {
            Delimiter = ',',
            TextQualifier = '"',
            DataTypes = new[] { eDataTypes.String }
        };

        using (var sr = new StreamReader(csvStream))
        {
            int i = 1;
            foreach (var line in sr.ReadLines("\r\n"))
            {
                workSheet.Cells["A" + i].LoadFromText(line, csvFormat);
                i++;
            }
        }

        return excelPackage;
    }

通过创建多个 ExcelPackages 解决了这个问题,而且我已经分批读取流(例如一次读取 20 万行)

    public List<ExcelPackage> ExcelPackagesFromCsvStream(Stream csvStream, int batchSize)
    {
        var excelPackages = new List<ExcelPackage>();
        int currentPackage = -1; // so that first package will have the index 0

        var csvFormat = new ExcelTextFormat
        {
            Delimiter = ',',
            TextQualifier = '"',
            DataTypes = new[] {eDataTypes.String}
        };


        using (var sr = new StreamReader(csvStream))
        {
            int index = 1;

            foreach (var line in sr.ReadLines("\r\n"))
            {
                if ((index - 1) % batchSize == 0)
                {
                    var excelPackage = new ExcelPackage();
                    excelPackage.Workbook.Worksheets.Add("Sheet1");

                    excelPackages.Add(excelPackage);
                    currentPackage++;
                    index = 1;
                }

                excelPackages[currentPackage].Workbook.Worksheets.First().Cells["A" + index].LoadFromText(line, csvFormat);
                index++;
            }
        }

        return excelPackages;
    }