Skip to content

Commit 8f5a111

Browse files
committed
Added method that returns the cell name
1 parent 3e635b0 commit 8f5a111

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCell.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
44
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
5+
import io.github.mbenincasa.javaexcelutils.tools.ExcelUtility;
56
import lombok.AllArgsConstructor;
67
import lombok.EqualsAndHashCode;
78
import lombok.Getter;
@@ -166,6 +167,17 @@ public void writeValue(Object val) {
166167
}
167168
}
168169

170+
/**
171+
* Returns the cell name
172+
* @return cell name
173+
* @since 0.4.2
174+
*/
175+
public String getCellName() {
176+
int row = this.cell.getRowIndex();
177+
int col = this.cell.getColumnIndex();
178+
return ExcelUtility.getCellName(row, col);
179+
}
180+
169181
/**
170182
* Format text according to the pattern provided
171183
* @param dataFormat The Apache POI library CellStyle dataFormat

src/main/java/io/github/mbenincasa/javaexcelutils/tools/ExcelUtility.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.mbenincasa.javaexcelutils.enums.Extension;
44
import io.github.mbenincasa.javaexcelutils.exceptions.ExtensionNotValidException;
55
import org.apache.commons.io.FilenameUtils;
6+
import org.apache.poi.ss.util.CellReference;
67

78
/**
89
* {@code ExcelUtility} is the static class with the implementations of some utilities on Excel files
@@ -33,4 +34,16 @@ public static String checkExcelExtension(String filename) throws ExtensionNotVal
3334
public static Boolean isValidExcelExtension(String extension) {
3435
return extension.equalsIgnoreCase(Extension.XLS.getExt()) || extension.equalsIgnoreCase(Extension.XLSX.getExt());
3536
}
37+
38+
/**
39+
* Returns the cell name
40+
* @param row row index
41+
* @param col column index
42+
* @return cell name
43+
* @since 0.4.2
44+
*/
45+
public static String getCellName(int row, int col) {
46+
String colName = CellReference.convertNumToColString(col);
47+
return colName + (row + 1);
48+
}
3649
}

src/test/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCellTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,14 @@ void remove() throws OpenWorkbookException, ExtensionNotValidException, IOExcept
121121
Assertions.assertNull(excelCell.getCell());
122122
Assertions.assertNull(excelCell.getIndex());
123123
}
124+
125+
@Test
126+
void getCellName() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, CellNotFoundException {
127+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
128+
ExcelSheet excelSheet = excelWorkbook.getSheet();
129+
ExcelRow excelRow = excelSheet.getRow(0);
130+
ExcelCell excelCell = excelRow.getCell(0);
131+
String cellName = excelCell.getCellName();
132+
Assertions.assertEquals("A1", cellName);
133+
}
124134
}

src/test/java/io/github/mbenincasa/javaexcelutils/tools/ExcelUtilityTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ void isValidExcelExtension() {
2424
String extension = FilenameUtils.getExtension(filename);
2525
Assertions.assertEquals(true, ExcelUtility.isValidExcelExtension(extension));
2626
}
27+
28+
@Test
29+
void getCellName() {
30+
int row = 0;
31+
int col = 0;
32+
String cellName = ExcelUtility.getCellName(row, col);
33+
Assertions.assertEquals("A1", cellName);
34+
}
2735
}

0 commit comments

Comments
 (0)