Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problems when reading a Excel file with numbers.

Problems when reading a Excel file with numbers.

Scheduled Pinned Locked Moved C#
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jwradhe
    wrote on last edited by
    #1

    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();
    
    E 1 Reply Last reply
    0
    • J jwradhe

      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();
      
      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      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 the DataType property has been set, that is 'c.DataType != null'. Cheers


      If you can read this, you don't have WingDings installed


      J 1 Reply Last reply
      0
      • E Estys

        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 the DataType property has been set, that is 'c.DataType != null'. Cheers


        If you can read this, you don't have WingDings installed


        J Offline
        J Offline
        jwradhe
        wrote on last edited by
        #3

        Thanks alot, worked perfect, and i learned one new thing! :) Have a good day!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups