JAVA ODFDOM: How to get integer values from ODF sheet

1k views Asked by At

I am using this code to get maximum row count from a sheet in ODF

public int getRowCount(String sheetName) throws XPathExpressionException, Exception

{

//reset rowCount to zero

        rowCount=0;

//xpath to reach Nodes of cell in first row

        int parameterCount=getColumnCount(sheetName);
        //System.out.println("Debug:ParameterCount="+parameterCount);
        for (int i=1;i<=parameterCount;i++)
        {
            String xpathStr="//table:table[@table:name='"+sheetName+"']/table:table-row/table:table-cell["+i+"][@office:value-type='void']";
            DTMNodeList nodeList = (DTMNodeList) xpath.evaluate(xpathStr, spreadSheet.getContentDom(), XPathConstants.NODESET);
            //System.out.println("Debug:RowsFoundWithData="+nodeList.getLength());
            System.out.println(nodeList.getLength());
            for(int j=0 ; j<nodeList.getLength();j++)
            {
                System.out.println("Debug:RowValue="+nodeList.item(j).getTextContent());
            }
            if(nodeList.getLength()-1>rowCount)
            {
                rowCount=nodeList.getLength()-1;
            }

    }

return rowCount;

    }

But this code only returns me non integer value count, if any row of a column in sheet contains integer value then it skips it and row Count returned by this function is not valid

It only counts alphanumeric values rows

Is there any way by which i can get correct row count

JAR used odfdom-java-0.8.7-jar-with-dependencies

1

There are 1 answers

0
Loek Bergman On

Although I think you have found your answer yourself, I was struggling with a like problem. It is hard to get decent examples for this otherwise great tool. This is what you should expect in a spreadsheet:

OdfSpreadsheetDocument ods = OdfSpreadsheetDocument.newSpreadsheetDocument();
OdfTable table = SpreadsheetHandling.setNewTable(ods, "names");
System.out.println("Number of rows: " + table.getRowCount());

Unfortunately, the situation is some more complicated. When you are in a spreadsheet and you walk through the code adding rows to a worksheet, will this method return the exact numbers of rows. However, when it is loading a document and then reading the number of rows, then will it return the maximum number of rows possible, hence: 1048576.

It is possible however using the number of nodes of type row the table has.

OdfSpreadsheetDocument ods = OdfSpreadsheetDocument.newSpreadsheetDocument();
OdfTable table = SpreadsheetHandling.setNewTable(ods, "rowCount");
TableTableElement tte = table.getOdfElement();
NodeList nl = tte.getElementsByTagName("table:table-row");
System.out.println("Number of nodeelements: " + nl.getLength());

With kind regards,

Loek