繁体   English   中英

JAVA ODFDOM:如何从ODF表获取整数值

[英]JAVA ODFDOM: How to get integer values from ODF sheet

我正在使用此代码从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;

    }

但是此代码仅返回非整数值计数,如果工作表中某列的任何行包含整数值,则将其跳过,并且此函数返回的行计数无效

它仅计算字母数字值的行

有什么办法可以让我获得正确的行数

JAR使用odfdom-java-0.8.7-jar-with-dependencies

尽管我认为您已经找到了答案,但我仍在努力解决类似问题。 很难获得这个很好的工具的范例。 这是您在电子表格中应该期望的:

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

不幸的是,情况更加复杂。 当您在电子表格中并且逐步浏览将行添加到工作表的代码时,此方法将返回确切的行数。 但是,当它加载文档然后读取行数时,它将返回可能的最大行数,因此:1048576。

但是,可以使用表具有的row类型的节点数。

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());

亲切的问候,

ek

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM