gpt4 book ai didi

java - 在我的应用程序中没有获得正确的行号或值。(Apache poi)

转载 作者:行者123 更新时间:2023-12-01 09:57:07 24 4
gpt4 key购买 nike

在此应用程序中,我正在使用 apache-poi 读取 xlsx 文件,但我不仅仅获得行号,它还提供了一些其他数据,请检查我的代码和输出。

这是我的代码:

public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
System.out.println("The Value of Row is : "+attributes.getValue("r"));
if(name.equals("c")) {
// Print the cell reference
// Figure out if the value is an index in the SST
String cellType = attributes.getValue("t");
if(cellType != null && cellType.equals("s")) {
nextIsString = true;
} else {
nextIsString = false;
}
}

当我使用下面的行打印行号时,我得到以下输出。

System.out.println("The Value of Row is : "+attributes.getValue("r"));

输出是:

The Value of Row is : null The Value of Row is : 1 The Value of Row is : A1 The Value of Row is : null The Value of Row is : B1 The Value of Row is : null The Value of Row is : C1 The Value of Row is : null The Value of Row is : D1
The Value of Row is : null
The Value of Row is : E1
The Value of Row is : null
The Value of Row is : F1
The Value of Row is : null
The Value of Row is : G1
The Value of Row is : null
The Value of Row is : H1
The Value of Row is : null
The Value of Row is : I1
The Value of Row is : null
The Value of Row is : J1
The Value of Row is : null
The Value of Row is : AMD1
The Value of Row is : AME1
The Value of Row is : AMF1
The Value of Row is : AMG1
The Value of Row is : AMH1
The Value of Row is : AMI1
The Value of Row is : AMJ1
The Value of Row is : 2
The Value of Row is : A2
The Value of Row is : null
The Value of Row is : B2
The Value of Row is : null
The Value of Row is : C2
The Value of Row is : null
The Value of Row is : D2

如何只获取行号?1,2,3,4,5,6,.....这样?

如何获取总列数和行数?

最佳答案

正如评论中已经指出的,这不是使用 apache poi 读取 Excel 工作表的默认方式。请阅读https://poi.apache.org/spreadsheet/quick-guide.html 。特别是https://poi.apache.org/spreadsheet/quick-guide.html#Iterator .

对于您的 sax.ContentHandler 代码:

Excel 工作表的 XML 如下所示:

...
<sheetData>
<row r="1">
<c r="A1">
<v>1</v>
</c>
<c r="B1">
<v>2</v>
</c>
</row>
...

如您所见,有不同的元素,不仅仅是行,而且“r”属性在这些元素中具有不同的含义。在元素“v”内它为空。

所以你必须首先检查是否有行元素:

...
if(name.equals("row")) {
System.out.println("The Value of Row is : "+attributes.getValue("r"));
}

但是,如上所述,我不知道这是否真的是最适合您的方法。

编辑:

根据您的评论,如果您有多达一百万行,那么使用 sax.ContentHandler 代码确实是一个很好的方法。所以对于你的具体问题,正如我所说:

public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
if(name.equals("row")) {
System.out.println("The Value of Row is : "+attributes.getValue("r"));
}
...

关于java - 在我的应用程序中没有获得正确的行号或值。(Apache poi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37108990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com