Results 1 to 4 of 4
  1. #1
    kakadi is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default How to identify date cell type using XMLStreamReader (poi's XSSFReader api)

    I have a simple excel sheet(.xlsx) with a number of rows. For one of cell the data type is "Date" and I can see the date in correct format in Excel. But when I read excel using XMLStreamReader(poi's XSSFReader api) I get some double value for this cell.

    I need to be able to:

    1. determine data type of cell

    2. if it is date data type then I want to convert the double value to date, as i am not sure which cell is of type date

    How can I do this?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to identify date cell type using XMLStreamReader (poi's XSSFReader api)

    There is a DateUtil class, which has a method for checking if a Cell is date formatted.
    If it is then you can call getDateCellValue() on that Cell.

    Yes, it's not terribly obvious!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kakadi is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Re: How to identify date cell type using XMLStreamReader (poi's XSSFReader api)

    Thanks for reply
    DateUtil.isCellDateFormatted(cell) method needs Cell instance as parameter, how do I create Cell instance, as I am using low level XMLStreamReader's event based api to parse sheet XML

    I am using XMLStreamReader as I need to process very large Excel and I am getting Out of memory errors with other APIs/approches

    My example code snippet


    ......
    this.xmlReader.next();
    if (this.xmlReader.isStartElement())
    {
    if (this.xmlReader.getLocalName().equals("c"))
    {
    String cellID = this.xmlReader.getAttributeValue(null, "r");
    CellReference cellReference = new CellReference(cellID);
    String colName = CellReference.convertNumToColString(cellReference. getCol());
    String cellType = this.xmlReader.getAttributeValue(null, "t");
    String cellValue = getCellValue(cellType);
    rowValues.add(cellValue);
    }
    }
    }




    private String getCellValue(String cellType) throws XMLStreamException
    {
    String value = EMPTY; // by default
    while (this.xmlReader.hasNext())
    {
    this.xmlReader.next();
    if (this.xmlReader.isStartElement())
    {
    if (this.xmlReader.getLocalName().equals("v"))
    {
    if ((cellType != null) && cellType.equals("s"))
    {
    int idx = Integer.parseInt(this.xmlReader.getElementText());
    return new XSSFRichTextString(this.stringsTable.getEntryAt(id x)).toString();
    }
    else
    {
    return this.xmlReader.getElementText();
    }
    }
    }
    else if (this.xmlReader.isEndElement() && this.xmlReader.getLocalName().equals("c"))
    {
    break;
    }
    }
    return value;
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to identify date cell type using XMLStreamReader (poi's XSSFReader api)

    Sorry, I see now.
    At a guess you'll need to duplicate what the DateUtil does, which is essentially to check the cell styling.
    The Util class has an isADateFormat method that takes the id for the date format (I think) and the name, so if you can get those then you can use that method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 04-12-2016, 09:43 PM
  2. Replies: 1
    Last Post: 10-07-2013, 02:14 PM
  3. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  4. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  5. How to identify packet type?
    By priya deshpande in forum Networking
    Replies: 0
    Last Post: 10-11-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •