-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map to Excel Sheet #1
Changes from 5 commits
f8bf8b7
ae76c0d
6ad089f
6151127
f96643a
201bdb3
690f01e
e9c03ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,89 @@ public <T> void addSheet(final Class<T> beanType, final List<T> rowObjects, fina | |
} | ||
} | ||
|
||
@Override | ||
public void addSheet(final List<Map<String, String>> rowsData, final String inSheetName, | ||
final List<String> inHeaders) { | ||
// Sanity check | ||
if (Objects.isNull(rowsData)) { | ||
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Rows data map is NULL"); | ||
} | ||
if (Objects.isNull(inHeaders)) { | ||
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Headers list is NULL"); | ||
} | ||
|
||
try { | ||
final Sheet exSheet = workbook.getSheet(inSheetName); | ||
if (Objects.nonNull(exSheet)) { | ||
String errMsg = String.format("A Sheet with the passed name already exists : %s", inSheetName); | ||
throw new IllegalArgumentException(errMsg); | ||
} | ||
|
||
// Create sheet | ||
final Sheet sheet = Objects.isNull(inSheetName) || inSheetName.isBlank() // | ||
? workbook.createSheet() // | ||
: workbook.createSheet(inSheetName); | ||
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName()); | ||
|
||
// Header | ||
final Row headerRow = sheet.createRow(0); | ||
for (int i = 0; i < inHeaders.size(); i++) { | ||
final Cell cell = headerRow.createCell(i); | ||
cell.setCellValue(inHeaders.get(i)); | ||
} | ||
|
||
// Data Rows | ||
for (int i = 0, rowNum = 1; i < rowsData.size(); i++, rowNum++) { | ||
final Row row = sheet.createRow(rowNum); | ||
final Map<String, String> rowData = rowsData.get(i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. recommend below to hedge against
|
||
|
||
for (int cellNo = 0; cellNo < inHeaders.size(); cellNo++) { | ||
final String key = inHeaders.get(cellNo); | ||
final String value = rowData.get(key); | ||
|
||
final Cell cell = row.createCell(cellNo); | ||
cell.setCellValue(value); | ||
} | ||
} | ||
} catch (Exception ex) { | ||
String errMsg = String.format("Error while preparing sheet with passed row objects : %s", ex.getMessage()); | ||
LOGGER.error(errMsg, ex); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void createTemplate(final String sheetName, final List<String> headers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method name is ambiguous (based on the implementation) Also, why not just use |
||
// Sanity check | ||
if (Objects.isNull(headers)) { | ||
throw new IllegalArgumentException("AbstractSpreadsheetWriter :: Headers list is NULL"); | ||
} | ||
|
||
try { | ||
final Sheet exSheet = workbook.getSheet(sheetName); | ||
if (Objects.nonNull(exSheet)) { | ||
String errMsg = String.format("A Sheet with the passed name already exists : %s", sheetName); | ||
throw new IllegalArgumentException(errMsg); | ||
} | ||
|
||
// Create sheet | ||
final Sheet sheet = Objects.isNull(sheetName) || sheetName.isBlank() // | ||
? workbook.createSheet() // | ||
: workbook.createSheet(sheetName); | ||
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName()); | ||
|
||
// Header | ||
final Row headerRow = sheet.createRow(0); | ||
for (int i = 0; i < headers.size(); i++) { | ||
final Cell cell = headerRow.createCell(i); | ||
cell.setCellValue(headers.get(i)); | ||
} | ||
} catch (Exception ex) { | ||
String errMsg = String.format("Error while preparing sheet with passed row objects : %s", ex.getMessage()); | ||
LOGGER.error(errMsg, ex); | ||
} | ||
} | ||
|
||
|
||
// Sheet :: Append to existing | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
|
@@ -73,6 +74,29 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, List<String> headers) | |
<T> void addSheet(Class<T> beanClz, List<T> beans, String sheetName, List<String> headers); | ||
|
||
|
||
/** | ||
* This method will attempt to add a new sheet and add the rows of data from the rows data. | ||
* | ||
* @param rowsData List of row data as Map. The map elements contains the value for the header as | ||
* key. | ||
* @param sheetName Name of the Sheet. (set it to <code>null</code> for default name) | ||
* @param headers a {@link List} of Header names to write in the file. <code>null</code> or empty | ||
* list will default to all writable properties. | ||
*/ | ||
void addSheet(List<Map<String, String>> rowsData, String sheetName, List<String> headers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be consistent, reorder the args
|
||
|
||
|
||
/** | ||
* This method will attempt to add a new empty sheet with just headers. The resulting sheet can be | ||
* treated as a template to fill the data by users. | ||
* | ||
* @param sheetName Name of the Sheet. (set it to <code>null</code> for default name) | ||
* @param headers a {@link List} of Header names to write in the file. <code>null</code> or empty | ||
* list will default to all writable properties. | ||
*/ | ||
void createTemplate(String sheetName, List<String> headers); | ||
|
||
|
||
/** | ||
* Writes the current Spreadsheet workbook to a file in the specified path. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
cellNo
instead ofi
for consistency ?