【问题标题】:JAVA ODFDOM: How to get integer values from ODF sheetJAVA ODFDOM:如何从 ODF 表中获取整数值
【发布时间】:2012-05-07 17:22:01
【问题描述】:

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

【问题讨论】:

    标签: java odf odfdom


    【解决方案1】:

    虽然我认为您自己已经找到了答案,但我一直在努力解决类似的问题。对于这个原本很棒的工具,很难找到像样的例子。 这是您在电子表格中所期望的:

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

    致以诚挚的问候,

    洛克

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 2015-04-12
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2020-05-18
      相关资源
      最近更新 更多