0
       public static void ReadExcelFileDOM(string fileName)
        {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
                string text;
                foreach (Row r in sheetData.Elements<Row>())
                {
                    foreach (Cell c in r.Elements<Cell>())
                    {
                        
                        text = c.CellValue.Text;
                        
                        Console.Write(text + " ");
                        
                    }
                }
               
            }

Here is my code. When l run it, l am getting the error "c.CellValue.Text is null". That cause some of values returns null when excel file is finish but i do not understand which values are null. Could anyone help me with that? Thanks a lot.

  • Are you paraphrasing the error message, or is that the exact wording of the error? – Robert Harvey Jun 20 '20 at 15:51
  • System.NullReferenceException:'Object reference not set to an instance of an object.' DocumentFormat.OpenXml.Spreadsheet.CellType.CellValue.get returned null. on "text = c.CellValue.Text; – Mustafa Mert Sevim Jun 20 '20 at 15:55
  • [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Robert Harvey Jun 20 '20 at 16:02
  • Have you tried using the debugger to see where it fails in visual studio? – Jawad Jun 20 '20 at 16:04
  • Since it's not the cell itself that is null but "c.CellValue", you could check inside "c" what are the values inside. You should be able to find the coordinates of the cell. – Arcord Jun 20 '20 at 16:04
  • You should (always) check whether an object is null before accessing properties or methods of that object. `if(c.CellValue is not null) { text = c.CellValue.Text; ...}` – H.G. Sandhagen Jun 20 '20 at 16:06

0 Answers0