Your
catch
block isn't helping you - you're throwing away almost all of the useful details of the exception, and preventing Visual Studio from breaking when the exception is thrown.
But at a guess, the exception is probably thrown from this line:
if (dataGridView1.Rows[i].Cells[j].Value.ToString() != null)
If the
Value
is
null
, you'll get a
NullReferenceException
when you try to call
ToString
on it.
If the
Value
is
not null
, then
ToString
will not return
null
.
Remove the
.ToString()
call on that line.
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = String.Empty;
}