From 304f5fa74d1f24ab798bd0d96451be0351127e69 Mon Sep 17 00:00:00 2001 From: Mateusz Szablak Date: Thu, 3 Mar 2022 04:52:25 +0100 Subject: [PATCH] BAEL-5201 Finding the Last Row in an Excel Spreadsheet From Java (#11873) --- .../poi/excel/lastrow/LastRowUnitTest.java | 67 ++++++++++++++++++ .../src/test/resources/lastRowTest.xlsx | Bin 0 -> 8949 bytes 2 files changed, 67 insertions(+) create mode 100644 apache-poi-2/src/test/java/com/baeldung/poi/excel/lastrow/LastRowUnitTest.java create mode 100644 apache-poi-2/src/test/resources/lastRowTest.xlsx diff --git a/apache-poi-2/src/test/java/com/baeldung/poi/excel/lastrow/LastRowUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/excel/lastrow/LastRowUnitTest.java new file mode 100644 index 0000000000..ea838be4ce --- /dev/null +++ b/apache-poi-2/src/test/java/com/baeldung/poi/excel/lastrow/LastRowUnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.poi.excel.lastrow; + +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.*; + +public class LastRowUnitTest { + private static final String FILE_NAME = "lastRowTest.xlsx"; + private String fileLocation; + + @Before + public void setup() throws URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); + } + + @Test + public void givenExampleGrid_whenGetRow_thenReturnRowObjectIfModified() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + + assertEquals(7, sheet.getLastRowNum()); + assertEquals(6, sheet.getPhysicalNumberOfRows()); + + assertNotNull(sheet.getRow(0)); + assertNotNull(sheet.getRow(1)); + assertNotNull(sheet.getRow(2)); + assertNotNull(sheet.getRow(3)); + assertNull(sheet.getRow(4)); + assertNull(sheet.getRow(5)); + assertNotNull(sheet.getRow(6)); + assertNotNull(sheet.getRow(7)); + assertNull(sheet.getRow(8)); + + assertSame(sheet.getRow(7), getLastRowFromSheet(sheet)); + } + + @Test + public void givenEmptySheet_whenGetRow_thenReturnNull() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(1); + + assertEquals(-1, sheet.getLastRowNum()); + assertEquals(0, sheet.getPhysicalNumberOfRows()); + + assertNull(sheet.getRow(0)); + + assertSame(sheet.getRow(0), getLastRowFromSheet(sheet)); + } + + public static Row getLastRowFromSheet(Sheet sheet) { + Row lastRow = null; + int lastRowNum = sheet.getLastRowNum(); + if (lastRowNum >= 0) { + lastRow = sheet.getRow(lastRowNum); + } + return lastRow; + } +} diff --git a/apache-poi-2/src/test/resources/lastRowTest.xlsx b/apache-poi-2/src/test/resources/lastRowTest.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..3980ceb1f2f6a7bbb2f2e2a184b453234f717d71 GIT binary patch literal 8949 zcmeHMg`$apu+2l z+S|F9+PUbfc{rFlzhr%7YeSiX2+x=efQMcG-|auR2cU_=b{*`%?!$x&4D~Wq(l6}t zxIqZ>(WGND2qt0gu`O}B{M+Y;ANqpi$x)RpQzUFx53gsVz-0DT38hroZnY-2IX#@^ zV@$S$bk}cN)-j)SGJe46)@Ga~h>eo0`OZ%dpEY_Fz+V4*$s(@Li<6=$giJ7gUxhQt--FLOvG=;O4;8XqZ=#T}3zoi(@O zNpHxl64fD>?=EMF4fAPI`+3Ir^H)3jWzCuM=CM1ejH>U~qe?ES-c1OOe-Rj?ec-_N z$xsb6+Q2}Cs@&iC08mpSiKKMG64?_<;aW`Ug^L)Y)myVBseR z10p&MsrpW)HqLCUKac-K=zlOQ|Mu!1;^h@P*@3}_Qa2&Jmy?UJSR%6S!ct9CYCe9_ z3z#)gxwNEuh8`Tjt$sLv{X*L|xT_&$8So z(}|zjL*K;a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