Skip to content

Commit 9de320f

Browse files
committed
Methods have been added to write a list of objects in a row or read all cells in a row. Methods to get or create a cell or row have been added.
1 parent f0a4918 commit 9de320f

File tree

7 files changed

+168
-16
lines changed

7 files changed

+168
-16
lines changed

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

33
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
4+
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
45
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
56
import lombok.AllArgsConstructor;
67
import lombok.EqualsAndHashCode;
@@ -71,6 +72,20 @@ public ExcelCell getCell(Integer index) throws CellNotFoundException {
7172
return new ExcelCell(cell, cell.getColumnIndex());
7273
}
7374

75+
/**
76+
* Retrieve or create a cell by index
77+
* @param index The index of the cell requested
78+
* @return A ExcelCell
79+
* @since 0.4.1
80+
*/
81+
public ExcelCell getOrCreateCell(Integer index) {
82+
Cell cell = this.row.getCell(index);
83+
if (cell == null) {
84+
return createCell(index);
85+
}
86+
return new ExcelCell(cell, cell.getColumnIndex());
87+
}
88+
7489
/**
7590
* Removes a cell by index
7691
* @param index The index of the row to remove
@@ -82,6 +97,66 @@ public void removeCell(Integer index) throws CellNotFoundException {
8297
this.row.removeCell(excelCell.getCell());
8398
}
8499

100+
/**
101+
* Write the values in the cells of the row
102+
* @param values The values to write in the cells of the row
103+
* @since 0.4.1
104+
*/
105+
public void writeValues(List<?> values) {
106+
for (int i = 0; i < values.size(); i++) {
107+
ExcelCell excelCell = getOrCreateCell(i);
108+
excelCell.writeValue(values.get(i));
109+
}
110+
}
111+
112+
/**
113+
* Reads the values of all cells in the row
114+
* @return The list of values written in the cells
115+
* @throws ReadValueException If an error occurs while reading
116+
* @since 0.4.1
117+
*/
118+
public List<?> readValues() throws ReadValueException {
119+
List<Object> values = new LinkedList<>();
120+
for (ExcelCell excelCell : getCells()) {
121+
values.add(excelCell.readValue());
122+
}
123+
return values;
124+
}
125+
126+
/**
127+
* Reads the values of all cells in the row
128+
* @param classes A list of Classes that is used to cast the results read
129+
* @return The list of values written in the cells
130+
* @throws ReadValueException If an error occurs while reading
131+
* @since 0.4.1
132+
*/
133+
public List<?> readValues(List<Class<?>> classes) throws ReadValueException {
134+
List<ExcelCell> excelCells = getCells();
135+
if(excelCells.size() != classes.size()) {
136+
throw new IllegalArgumentException("There are " + excelCells.size() + " items in the row and classlist has " + classes.size() + " values.");
137+
}
138+
139+
List<Object> values = new LinkedList<>();
140+
for (int i = 0; i < excelCells.size(); i++) {
141+
values.add(excelCells.get(i).readValue(classes.get(i)));
142+
}
143+
144+
return values;
145+
}
146+
147+
/**
148+
* Reads the values of all cells in the row as a String
149+
* @return The list of values, such as String, written in the cells
150+
* @since 0.4.1
151+
*/
152+
public List<String> readValuesAsString() {
153+
List<String> values = new LinkedList<>();
154+
for (ExcelCell excelCell : getCells()) {
155+
values.add(excelCell.readValueAsString());
156+
}
157+
return values;
158+
}
159+
85160
/**
86161
* Returns the Sheet to which it belongs
87162
* @return A ExcelSheet

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ public ExcelRow getRow(Integer index) throws RowNotFoundException {
9595
return new ExcelRow(row, index);
9696
}
9797

98+
/**
99+
* Retrieve or create a row by index
100+
* @param index The index of the row requested
101+
* @return A ExcelRow
102+
* @since 0.4.1
103+
*/
104+
public ExcelRow getOrCreateRow(Integer index) {
105+
Row row = this.sheet.getRow(index);
106+
if (row == null) {
107+
return createRow(index);
108+
}
109+
return new ExcelRow(row, index);
110+
}
111+
98112
/**
99113
* Removes a row by index
100114
* @param index The index of the row to remove

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public ExcelWorkbook(InputStream inputStream) throws OpenWorkbookException {
7979
*/
8080
public static ExcelWorkbook open(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
8181
/* Check extension */
82-
String extension = ExcelUtility.checkExcelExtension(file.getName());
82+
ExcelUtility.checkExcelExtension(file.getName());
8383

8484
/* Open file input stream */
8585
FileInputStream fileInputStream = new FileInputStream(file);
86-
ExcelWorkbook excelWorkbook = open(fileInputStream, extension);
86+
ExcelWorkbook excelWorkbook = open(fileInputStream);
8787

8888
/* Close the stream before return */
8989
fileInputStream.close();
@@ -93,17 +93,10 @@ public static ExcelWorkbook open(File file) throws ExtensionNotValidException, I
9393
/**
9494
* Opens the workbook
9595
* @param inputStream The {@code InputStream} of the Excel file
96-
* @param extension The file's extension
9796
* @return An ExcelWorkBook that is represented in the Excel file
98-
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
9997
* @throws OpenWorkbookException If an error occurred while opening the workbook
10098
*/
101-
public static ExcelWorkbook open(InputStream inputStream, String extension) throws ExtensionNotValidException, OpenWorkbookException {
102-
/* Check the extension */
103-
if (!ExcelUtility.isValidExcelExtension(extension)) {
104-
throw new ExtensionNotValidException("Pass a file with the XLS or XLSX extension");
105-
}
106-
99+
public static ExcelWorkbook open(InputStream inputStream) throws OpenWorkbookException {
107100
return new ExcelWorkbook(inputStream);
108101
}
109102

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ public static Map<String, byte[]> excelToCsvByte(byte[] bytes) throws OpenWorkbo
842842
}
843843

844844
/**
845-
* @param excelFile The excel file
845+
* @param excelFile The Excel file
846846
* @param path The path, without file name, where the files will be saved
847847
* @return A map where the key represents the Sheet name and the value is a CSV file for each Sheet
848848
* @throws IOException If an I/O error has occurred

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.File;
88
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.LinkedList;
911
import java.util.List;
1012

1113
class ExcelRowTest {
@@ -89,4 +91,65 @@ void removeCell() throws OpenWorkbookException, ExtensionNotValidException, IOEx
8991
Assertions.assertDoesNotThrow(() -> excelRow.removeCell(0));
9092
Assertions.assertThrows(CellNotFoundException.class, () -> excelRow.getCell(0));
9193
}
94+
95+
@Test
96+
void getOrCreateCell() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException {
97+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
98+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
99+
ExcelRow excelRow = excelSheet.getRow(0);
100+
ExcelCell excelCell = excelRow.getOrCreateCell(20);
101+
Assertions.assertEquals(20, excelCell.getIndex());
102+
Assertions.assertNotNull(excelCell.getCell());
103+
}
104+
105+
@Test
106+
void writeValues() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, ReadValueException {
107+
List<Object> values = new ArrayList<>();
108+
values.add("Rossi");
109+
values.add(3);
110+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
111+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
112+
ExcelRow excelRow = excelSheet.getRow(0);
113+
excelRow.writeValues(values);
114+
List<ExcelCell> excelCells = excelRow.getCells();
115+
Assertions.assertEquals("Rossi", excelCells.get(0).readValue(String.class));
116+
Assertions.assertEquals(3, excelCells.get(1).readValue(Integer.class));
117+
}
118+
119+
@Test
120+
void readValues() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, ReadValueException {
121+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
122+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
123+
ExcelRow excelRow = excelSheet.getRow(1);
124+
List<?> values = excelRow.readValues();
125+
Assertions.assertEquals("Nocera Inferiore", values.get(0));
126+
Assertions.assertEquals("Salerno", values.get(1));
127+
Assertions.assertEquals(40.0, values.get(2));
128+
}
129+
130+
@Test
131+
void testReadValues() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, ReadValueException {
132+
List<Class<?>> classes = new LinkedList<>();
133+
classes.add(String.class);
134+
classes.add(String.class);
135+
classes.add(Integer.class);
136+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
137+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
138+
ExcelRow excelRow = excelSheet.getRow(1);
139+
List<?> values = excelRow.readValues(classes);
140+
Assertions.assertEquals("Nocera Inferiore", values.get(0));
141+
Assertions.assertEquals("Salerno", values.get(1));
142+
Assertions.assertEquals(40, values.get(2));
143+
}
144+
145+
@Test
146+
void readValuesAsString() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException {
147+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
148+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
149+
ExcelRow excelRow = excelSheet.getRow(1);
150+
List<?> values = excelRow.readValuesAsString();
151+
Assertions.assertEquals("Nocera Inferiore", values.get(0));
152+
Assertions.assertEquals("Salerno", values.get(1));
153+
Assertions.assertEquals("40", values.get(2));
154+
}
92155
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ void removeRow() throws OpenWorkbookException, ExtensionNotValidException, IOExc
9191
Assertions.assertDoesNotThrow(() -> excelSheet.removeRow(0));
9292
Assertions.assertThrows(RowNotFoundException.class, () -> excelSheet.getRow(0));
9393
}
94+
95+
@Test
96+
void getOrCreateRow() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
97+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
98+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
99+
ExcelRow excelRow = excelSheet.getOrCreateRow(20);
100+
Assertions.assertEquals(20, excelRow.getIndex());
101+
Assertions.assertNotNull(excelRow.getRow());
102+
}
94103
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.github.mbenincasa.javaexcelutils.tools.Converter;
1212
import io.github.mbenincasa.javaexcelutils.tools.utils.Address;
1313
import io.github.mbenincasa.javaexcelutils.tools.utils.Person;
14-
import org.apache.commons.io.FilenameUtils;
1514
import org.junit.jupiter.api.Assertions;
1615
import org.junit.jupiter.api.BeforeAll;
1716
import org.junit.jupiter.api.Test;
@@ -43,10 +42,9 @@ void open() throws OpenWorkbookException, ExtensionNotValidException, IOExceptio
4342
}
4443

4544
@Test
46-
void testOpen() throws IOException, OpenWorkbookException, ExtensionNotValidException {
47-
String extension = FilenameUtils.getExtension(excelFile.getName());
45+
void testOpen() throws IOException, OpenWorkbookException {
4846
FileInputStream fileInputStream = new FileInputStream(excelFile);
49-
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileInputStream, extension);
47+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileInputStream);
5048
Assertions.assertNotNull(excelWorkbook);
5149
Assertions.assertNotNull(excelWorkbook.getWorkbook());
5250
}
@@ -208,7 +206,7 @@ void writeAndClose() throws ExtensionNotValidException, IOException, SheetAlread
208206
List<ObjectToExcel<?>> list = new ArrayList<>();
209207
list.add(new ObjectToExcel<>("Person", Person.class, personStream));
210208
list.add(new ObjectToExcel<>("Address", Address.class, addressStream));
211-
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) Converter.objectsToExcelStream(list, Extension.XLSX, true);
209+
ByteArrayOutputStream outputStream = Converter.objectsToExcelStream(list, Extension.XLSX, true);
212210
ExcelWorkbook excelWorkbook = new ExcelWorkbook(Extension.XLSX);
213211
Assertions.assertDoesNotThrow(() -> excelWorkbook.writeAndClose(outputStream));
214212
}

0 commit comments

Comments
 (0)