aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2024-07-07 05:03:15 +0000
committerDominik Stadler <centic@apache.org>2024-07-07 05:03:15 +0000
commit164c239a52ac44383c4cbad9cbf607a9e93b7f57 (patch)
tree399c67b7cca9ced5e19ab0477525b48be8e57b5f
parentd090c6b77ab8162b6cd2fcd7f57b67e694a53c68 (diff)
downloadpoi-164c239a52ac44383c4cbad9cbf607a9e93b7f57.tar.gz
poi-164c239a52ac44383c4cbad9cbf607a9e93b7f57.zip
HSSFWorkbook.getSheet(): Return first found sheet
We do not need to loop over all sheets always but should be able to return the first found sheet. This may change semantics for cases where there are multiple sheets where name only differs in case, but the JavaDoc did not state which one will be returned. All three implementations (HSSF, XSSF, SXSSF) now behave the same way. Closes #653 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1918982 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java3
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java3
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java13
3 files changed, 13 insertions, 6 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
index da64a880f2..401529839b 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
@@ -808,6 +808,9 @@ public class SXSSFWorkbook implements Workbook {
/**
* Get sheet with the given name
*
+ * If there are multiple matches, the first sheet from the list
+ * of sheets is returned.
+ *
* @param name of the sheet
* @return Sheet with the name provided or <code>null</code> if it does not exist
*/
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
index 7c03b27fa4..3703f0dd4b 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
@@ -1208,6 +1208,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
/**
* Get sheet with the given name (case insensitive match)
*
+ * If there are multiple matches, the first sheet from the list
+ * of sheets is returned.
+ *
* @param name of the sheet
* @return XSSFSheet with the name provided or {@code null} if it does not exist
*/
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index 336fc2bc3e..9442589189 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -1055,24 +1055,25 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
}
/**
- * Get sheet with the given name (case insensitive match)
+ * Get sheet with the given name (case insensitive match).
+ *
+ * If there are multiple matches, the first sheet from the list
+ * of sheets is returned.
*
* @param name of the sheet
* @return HSSFSheet with the name provided or {@code null} if it does not exist
*/
-
@Override
public HSSFSheet getSheet(String name) {
- HSSFSheet retval = null;
-
for (int k = 0; k < _sheets.size(); k++) {
String sheetname = workbook.getSheetName(k);
if (sheetname.equalsIgnoreCase(name)) {
- retval = _sheets.get(k);
+ return _sheets.get(k);
}
}
- return retval;
+
+ return null;
}
/**