Skip to content

Commit 2e4e91c

Browse files
committed
[test] updated Spreadsheet write from Map data tests for xls and xlsx
1 parent b37e32e commit 2e4e91c

File tree

6 files changed

+193
-95
lines changed

6 files changed

+193
-95
lines changed

src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public AbstractSpreadsheetWriter(final Workbook workbook) {
5252
// ------------------------------------------------------------------------
5353

5454

55-
// Sheet :: Add
55+
// Sheet :: Bean
5656

5757
@Override
5858
public <T> void addSheet(final Class<T> beanType, final List<T> rowObjects, final String inSheetName,
5959
final List<String> inHeaders) {
6060
// Sanity checks
6161
if (Objects.isNull(beanType)) {
62-
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Bean Type is NULL");
62+
throw new IllegalArgumentException("#addSheet :: Bean Type is NULL");
6363
}
6464

6565
// Sheet config
@@ -110,15 +110,18 @@ public <T> void addSheet(final Class<T> beanType, final List<T> rowObjects, fina
110110
}
111111
}
112112

113+
114+
// Sheet :: Map<String, Object>
115+
113116
@Override
114-
public void addSheet(final String inSheetName, final List<String> inHeaders,
115-
final List<Map<String, String>> rowsData) {
117+
public void addSheet(final List<Map<String, Object>> rowsData, final String inSheetName,
118+
final List<String> inHeaders) {
116119
// Sanity check
117120
if (Objects.isNull(rowsData)) {
118-
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Rows data map is NULL");
121+
throw new IllegalArgumentException("#addSheet :: Rows data map is NULL");
119122
}
120-
if (Objects.isNull(inHeaders)) {
121-
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Headers list is NULL");
123+
if (Objects.isNull(inHeaders) || inHeaders.isEmpty()) {
124+
throw new IllegalArgumentException("#addSheet :: Headers list is NULL or EMPTY");
122125
}
123126

124127
try {
@@ -144,19 +147,19 @@ public void addSheet(final String inSheetName, final List<String> inHeaders,
144147
// Data Rows
145148
for (int i = 0, rowNum = 1; i < rowsData.size(); i++, rowNum++) {
146149
final Row row = sheet.createRow(rowNum);
147-
final Map<String, String> rowData = rowsData.get(i);
148-
149-
// Skip if row is null
150-
if (Objects.isNull(rowData)) {
151-
continue;
150+
final Map<String, Object> rowData = rowsData.get(i);
151+
if (Objects.isNull(rowData) || rowData.isEmpty()) {
152+
continue; // Skip if row is null
152153
}
153154

154155
for (int cellNum = 0; cellNum < inHeaders.size(); cellNum++) {
155156
final String key = inHeaders.get(cellNum);
156-
final String value = rowData.get(key);
157+
final Object value = rowData.get(key);
157158

158159
final Cell cell = row.createCell(cellNum);
159-
cell.setCellValue(value);
160+
if (Objects.nonNull(value)) {
161+
cell.setCellValue(String.valueOf(value));
162+
}
160163
}
161164
}
162165
} catch (Exception ex) {
@@ -166,18 +169,21 @@ public void addSheet(final String inSheetName, final List<String> inHeaders,
166169
}
167170

168171

169-
// Sheet :: Append to existing
170-
171-
172172
// Write
173173

174174
@Override
175-
public void write(final String filepath) throws IOException {
176-
try (final OutputStream outputStrem = new FileOutputStream(new File(filepath))) {
175+
public void write(final File file) throws IOException {
176+
// Sanity checks
177+
if (Objects.isNull(file)) {
178+
throw new IllegalArgumentException("#write :: Input File object is NULL");
179+
}
180+
181+
try (final OutputStream outputStrem = new FileOutputStream(file)) {
177182
workbook.write(outputStrem);
178183
workbook.close();
184+
179185
} catch (Exception ex) {
180-
final String errMsg = String.format("Failed to write workbook data to file : %s", filepath);
186+
final String errMsg = String.format("Failed to write workbook data to file : %s", file.getPath());
181187
LOGGER.error(errMsg);
182188
throw ex;
183189
}

src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.millij.poi.ss.writer;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.util.List;
56
import java.util.Map;
67

8+
import org.apache.logging.log4j.util.Strings;
9+
710

811
/**
912
* Representation of a Spreadsheet Writer. To write data to files, create a new Instance of {@link SpreadsheetWriter},
@@ -13,9 +16,12 @@
1316
*/
1417
public interface SpreadsheetWriter {
1518

19+
//
20+
// Add Sheet :: Beans
21+
1622
/**
17-
* This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
18-
* {@link #write(String)} method once the data addition is completed.
23+
* Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
24+
* once the data addition is completed.
1925
*
2026
* <br/>
2127
* <b>Sheet Name</b> : default sheet name will be used
@@ -31,8 +37,8 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans) {
3137
}
3238

3339
/**
34-
* This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
35-
* {@link #write(String)} method once the data addition is completed.
40+
* Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
41+
* once the data addition is completed.
3642
*
3743
* <br/>
3844
* <b>Headers</b> : all possible properties as defined by the Type will be used as headers
@@ -46,8 +52,8 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, String sheetName) {
4652
}
4753

4854
/**
49-
* This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
50-
* {@link #write(String)} method once the data addition is completed.
55+
* Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
56+
* once the data addition is completed.
5157
*
5258
* <br/>
5359
* <b>Sheet Name</b> : default sheet name will be used
@@ -61,9 +67,10 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, List<String> headers)
6167
this.addSheet(beanClz, beans, null, headers);
6268
}
6369

70+
6471
/**
65-
* This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
66-
* {@link #write(String)} method once the data addition is completed.
72+
* Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
73+
* once the data addition is completed.
6774
*
6875
* @param beanClz The Class type to serialize the rows data
6976
* @param beans List of Data beans of the parameterized type
@@ -74,27 +81,58 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, List<String> headers)
7481
<T> void addSheet(Class<T> beanClz, List<T> beans, String sheetName, List<String> headers);
7582

7683

84+
//
85+
// Add Sheet :: Map<String, Object>
86+
87+
/**
88+
* Add a new sheet and add the rows of data defined by a {@link Map}. The <code>null</code> entry in the rows data
89+
* list will be skipped and no rows will be added to the sheet.
90+
*
91+
* @param rowsData List of Rows defined by a {@link Map}s where the map key is header and value is the row value
92+
* @param headers a {@link List} of Header names to write in the file
93+
*/
94+
default void addSheet(List<Map<String, Object>> rowsData, List<String> headers) {
95+
this.addSheet(rowsData, null, headers);
96+
}
97+
7798
/**
78-
* This method will attempt to add a new sheet and add the rows of data from the rows data. The
79-
* <code>null</code> entry in the rows data list will be skipped and no rows will be added to the
80-
* sheet.
99+
* Add a new sheet and add the rows of data defined by a {@link Map}. The <code>null</code> entry in the rows data
100+
* list will be skipped and no rows will be added to the sheet.
81101
*
102+
* @param rowsData List of Rows defined by a {@link Map}s where the map key is header and value is the row value
82103
* @param sheetName Name of the Sheet. (set it to <code>null</code> for default name)
83-
* @param headers a {@link List} of Header names to write in the file. <code>null</code> or empty
84-
* list will default to all writable properties.
85-
* @param rowsData List of Map, representing the sheet data. Each Map represents the row data where
86-
* the map elements stores a value for a header as key.
104+
* @param headers a {@link List} of Header names to write in the file
87105
*/
88-
void addSheet(String sheetName, List<String> headers, List<Map<String, String>> rowsData);
106+
void addSheet(List<Map<String, Object>> rowsData, String sheetName, List<String> headers);
89107

90108

109+
//
110+
// Write
111+
91112
/**
92113
* Writes the current Spreadsheet workbook to a file in the specified path.
93114
*
94115
* @param filepath output filepath (including filename).
95116
*
96117
* @throws IOException if the filepath is not writable.
97118
*/
98-
void write(String filepath) throws IOException;
119+
default void write(final String filepath) throws IOException {
120+
// Sanity checks
121+
if (Strings.isBlank(filepath)) {
122+
throw new IllegalArgumentException("#write :: Input File Path is BLANK");
123+
}
124+
125+
this.write(new File(filepath));
126+
}
127+
128+
/**
129+
* Writes the current Spreadsheet workbook to a file
130+
*
131+
* @param fiel output file
132+
*
133+
* @throws IOException if the file is not writable.
134+
*/
135+
void write(File file) throws IOException;
136+
99137

100138
}

src/test/java/io/github/millij/poi/ss/reader/XlsReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void test_read_xls_single_sheet_with_callback() throws SpreadsheetReadExc
163163

164164
// Read
165165
XlsReader reader = new XlsReader();
166-
reader.read(Employee.class, xlsFile, new RowListener<>() {
166+
reader.read(Employee.class, xlsFile, new RowListener<Employee>() {
167167

168168
@Override
169169
public void row(int rowNum, Employee employee) {

src/test/java/io/github/millij/poi/ss/reader/XlsxReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void test_read_xlsx_single_sheet_with_callback() throws SpreadsheetReadEx
163163

164164
// Read
165165
final XlsxReader reader = new XlsxReader();
166-
reader.read(Employee.class, xlsxFile, new RowListener<>() {
166+
reader.read(Employee.class, xlsxFile, new RowListener<Employee>() {
167167

168168
@Override
169169
public void row(int rowNum, Employee employee) {

0 commit comments

Comments
 (0)