Problems when reading a Excel file with numbers.
-
Hi! When i run my code with a excel document with words in, it works, but at fast as i read a document with numbers only i get error on marked line in code.
if (c.DataType.Value == CellValues.SharedString)
and the error is:
An error occurred!!!: The object reference has not been specified to an instance of an object.
I have a little hard to see why this happends, hope on you experts!
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(input, true))
{WorkbookPart workbookPart = myDoc.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); SheetData sheetData = worksheetPart.Worksheet.Elements().First(); foreach (Row r in sheetData.Elements()) { foreach (Cell c in r.Elements()) { string value = c.InnerText; **if (c.DataType.Value == CellValues.SharedString)** { var stringTable = workbookPart.GetPartsOfType() .FirstOrDefault(); if (stringTable != null) { value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; GenerateBarcodeFromExcel(value, output); } } else { GenerateBarcodeFromExcel(c.CellValue.Text, output); } } } myDoc.Close();
-
Hi! When i run my code with a excel document with words in, it works, but at fast as i read a document with numbers only i get error on marked line in code.
if (c.DataType.Value == CellValues.SharedString)
and the error is:
An error occurred!!!: The object reference has not been specified to an instance of an object.
I have a little hard to see why this happends, hope on you experts!
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(input, true))
{WorkbookPart workbookPart = myDoc.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); SheetData sheetData = worksheetPart.Worksheet.Elements().First(); foreach (Row r in sheetData.Elements()) { foreach (Cell c in r.Elements()) { string value = c.InnerText; **if (c.DataType.Value == CellValues.SharedString)** { var stringTable = workbookPart.GetPartsOfType() .FirstOrDefault(); if (stringTable != null) { value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; GenerateBarcodeFromExcel(value, output); } } else { GenerateBarcodeFromExcel(c.CellValue.Text, output); } } } myDoc.Close();
Not all Cells are hard typed, only when you set the property on the cell explicitly. Excel makes only strings
SharedString
by default. So you need to check if theDataType
property has been set, that is 'c.DataType != null
'. Cheers
If you can read this, you don't have WingDings installed
-
Not all Cells are hard typed, only when you set the property on the cell explicitly. Excel makes only strings
SharedString
by default. So you need to check if theDataType
property has been set, that is 'c.DataType != null
'. Cheers
If you can read this, you don't have WingDings installed