1

I have to do a Excel Manager with C# and i choose to use Gem Box Spreadsheet Free

        var ef = new ExcelFile();
        ef = ExcelFile.Load(File_Lettura);
        ExcelWorksheet ws = ef.Worksheets.ActiveWorksheet;

        int riga = 13;
        string s = (ws.Cells["B6"]).ToString();
        string[] r = s.Split('-');
        int c = 0;

        while (ws.Cells["B"+riga.ToString()].Value != null)
        {

            if (ws.Cells["F"+riga.ToString()].Value.ToString() != "")
            {
                // add row
                dgwFile.Rows.Add();

                dgwFile.Rows[c].Cells[0].Value = r[0] + "-" + r[1] + "-" + ws.Cells["B"+riga.ToString()].Value.ToString();
                dgwFile.Rows[c].Cells[1].Value = ws.Cells["D" + riga.ToString()].Value.ToString() + ws.Cells["G" + riga.ToString()].Value.ToString() + ws.Cells["H" + riga.ToString()].Value.ToString() + ws.Cells["I" + riga.ToString()].Value.ToString();
                dgwFile.Rows[c].Cells[2].Value = ws.Cells["F" + riga.ToString()].Value.ToString();
                dgwFile.Rows[c].Cells[3].Value = "0";
                c++;
            }
            riga++;
        }

VS give me a problem at the first "IF" with error :

An unhandled exception of type 'System.NullReferenceException' occurred.

I think the wrong rows are the first 3 thanks in advance Smile | :)

i've also tried like it

ExcelFile ef = ExcelFile.Load(File_Lettura);
ExcelWorksheet ws = ef.Worksheets.ActiveWorksheet;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
stewgh
  • 11
  • 1

1 Answers1

0

I don't believe the first 3 lines are the problem, however in order to be 100% sure you would need to specify exactly where the exception is thrown.

Nevertheless I presume one of the ExcelCell's that you are targeting is null. So try this:

while (ws.Cells["B"+riga.ToString()] != null &&
       ws.Cells["B"+riga.ToString()].Value != null)
{
    if (ws.Cells["F"+riga.ToString()] != null &&
        ws.Cells["F"+riga.ToString()].Value != null)
        {
Mario Z
  • 4,328
  • 2
  • 24
  • 38