0

The following code executes fine on a first pass, but on the second pass

reader = ExcelReaderFactory.CreateOpenXmlReader(stream);

throws a System.ArgumentNullException: 'value cannot be null'

    public IEnumerable<String[]> GetFileContents()
    {
        Stream stream = _originalFile.InputStream; // _originalFile is of type HttpPostedFileBase

        IExcelDataReader reader = null;

        if (_originalFile.FileName.EndsWith(".xls"))
        {
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        }
        else if (_originalFile.FileName.EndsWith(".xlsx"))
        {
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        }
        else
        {
            throw new Exception("File format is not supported");
        }

        List<String[]> result = new List<String[]>();
        Int32 fieldCount = reader.FieldCount;
        StringBuilder builder = new StringBuilder();

        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount - 1; i++)
            {
                builder.AppendFormat("{0},", reader.IsDBNull(i) ? string.Empty : reader.GetString(i));
            }

            builder.Length--;
            result.Add(builder.ToString().Split(','));
            builder.Clear();
        }
        reader.Close();
        reader.Dispose();

        return result;
    }
shA.t
  • 16,580
  • 5
  • 54
  • 111
Richard Watts
  • 954
  • 2
  • 8
  • 21
  • I think using a `using` for your `stream` can solve it, I think it is because of not disposed previous stream - HTH ;). – shA.t Jun 11 '18 at 11:27
  • Thanks for your input ShA.t. I have tried that, unfortunately with no success. Whatever I seem to there's an seems to be an instance of it hanging around causing an issue. Im starting to think it's a bug in the ExcelReaderFactory – Richard Watts Jun 12 '18 at 08:09

0 Answers0