首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAVA ODFDOM:如何从ODF表中获取整数值

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

Stack Overflow用户
提问于 2012-05-08 01:22:02
回答 1查看 905关注 0票数 0

我正在使用此代码从ODF中的工作表中获取最大行数

代码语言:javascript
复制
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;

    }

但此代码仅返回非整数值count,如果sheet中某列的任何行包含整数值,则跳过该值,此函数返回的行数无效

它只计算字母数字值的行数

有什么方法可以让我得到正确的行数吗?

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

EN

回答 1

Stack Overflow用户

发布于 2012-08-14 06:28:35

虽然我认为你已经找到了你自己的答案,但我正在努力解决一个类似的问题。很难为这个很棒的工具找到像样的例子。以下是您在电子表格中应该看到的内容:

代码语言:javascript
复制
OdfSpreadsheetDocument ods = OdfSpreadsheetDocument.newSpreadsheetDocument();
OdfTable table = SpreadsheetHandling.setNewTable(ods, "names");
System.out.println("Number of rows: " + table.getRowCount());

不幸的是,情况要复杂一些。当您在电子表格中并遍历向工作表添加行的代码时,此方法是否会返回确切的行数。但是,当它加载一个文档,然后读取行数时,它将返回可能的最大行数,因此: 1048576。

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

代码语言:javascript
复制
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());

向您致以亲切的问候,

Loek

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10486302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档