NPOI Nuget Package curRow.GetCell (0) .DateCellValue ошибка ТОЛЬКО ПОСЛЕ второй попытки - PullRequest
0 голосов
/ 04 августа 2020

Я пытаюсь заставить работать следующий код. С c# и Blazor он работал отлично. Однако по какой-то причине теперь, когда я обновляю sh страницу, попробуйте это действие в том же файле Excel, я получаю следующую ошибку:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Но это происходит только для curRow.GetCell(0).DateCellValue и ни один из других getcells.

Таким образом, при первом выполнении этого действия все строки отлично читаются, но при второй загрузке того же файла возникает эта ошибка.

Если я перезапущу приложение, оно снова будет работать при первой загрузке.

FileStream fs = new FileStream(ffs.FullPath, FileMode.Open, FileAccess.Read);


        IWorkbook workbook;

        //FileStream fs = reader;
        workbook = new HSSFWorkbook(fs);
        ISheet sheet = workbook.GetSheetAt(1);
        int rowCount = sheet.LastRowNum; // This may not be valid row count.
        rowCount = rowCount - 1;

        int read = 0;
        int totrows = rowCount - 7;

        // If first row is table head, i starts from 1
        for (int i = 8; i <= rowCount;)
        {

            IRow curRow = sheet.GetRow(i);
            // Works for consecutive data. Use continue otherwise
            if (curRow == null)
            {

                // Valid row count
                rowCount = i - 1;

            }
            else
            {
                try
                {
                    // Get data from the 4th column (4th cell of each row)

                    var Date1 = curRow.GetCell(0).DateCellValue;
                    var Des = curRow.GetCell(1).StringCellValue;
                    var Reference = curRow.GetCell(3).StringCellValue;
                    var recon = curRow.GetCell(4).StringCellValue;
                    var source = curRow.GetCell(5).StringCellValue;
                    var amount = curRow.GetCell(6).NumericCellValue;
                    var bal = curRow.GetCell(7).NumericCellValue;
                    //var id = Date1.ToString("yyyyMMdd") + Des + amount.ToString() + bal.ToString();
                    //id = String.Concat(id.Where(c => !Char.IsWhiteSpace(c)));
                    //MessageBox.Show(id);
                    //dt.Rows.Add(Date1, Des, bal, amount, Reference, recon, source, id);
                    read = read + 1;
                }
                catch { }
            }
            

            i++;
        }
...