Skip to content
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

Add Sheet From Data as Map #68

Open
wants to merge 1 commit into
base: v3.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,61 @@ public <T> void addSheet(final Class<T> beanType, final List<T> rowObjects, fina
}
}

@Override
public void addSheet(final String inSheetName, final List<String> inHeaders,
final List<Map<String, String>> rowsData) {
// 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 cellNum = 0; cellNum < inHeaders.size(); cellNum++) {
final Cell cell = headerRow.createCell(cellNum);
cell.setCellValue(inHeaders.get(cellNum));
}

// 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);

// Skip if row is null
if (Objects.isNull(rowData)) {
continue;
}

for (int cellNum = 0; cellNum < inHeaders.size(); cellNum++) {
final String key = inHeaders.get(cellNum);
final String value = rowData.get(key);

final Cell cell = row.createCell(cellNum);
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);
}
}


// Sheet :: Append to existing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
Expand Down Expand Up @@ -73,6 +74,20 @@ 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. The
* <code>null</code> entry in the rows data list will be skipped and no rows will be added to the
* sheet.
*
* @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.
* @param rowsData List of Map, representing the sheet data. Each Map represents the row data where
* the map elements stores a value for a header as key.
*/
void addSheet(String sheetName, List<String> headers, List<Map<String, String>> rowsData);


/**
* Writes the current Spreadsheet workbook to a file in the specified path.
*
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/io/github/millij/poi/ss/writer/XlsWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -115,4 +117,65 @@ public void test_write_xls_multiple_sheets() throws IOException {
gew.write(filepath_output_file);
}

@Test
public void test_write_map_as_xls_sheet() throws IOException {
final String filepath_output_file = _path_test_output.concat("map_2_sheets.xls");

// Excel Writer
LOGGER.info("test_write_map_as_xls_sheet :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new XlsWriter();

// Headers
final List<String> headers = new ArrayList<>();
headers.add("Slno.");
headers.add("Name");
headers.add("Age");
headers.add("Gender");
headers.add("Height (mts)");
headers.add("Address");

// Data
final List<Map<String, String>> rowsDataMap = new ArrayList<>();
final Map<String, String> rowDataMap = new HashMap<>();
rowDataMap.put("Slno.", "1");
rowDataMap.put("Name", "foo");
rowDataMap.put("Age", "1");
rowDataMap.put("Gender", "Male");
rowDataMap.put("Height (mts)", "1.6");
rowDataMap.put("Address", "Chennai, India");
rowsDataMap.add(rowDataMap);

// Add Sheets
final String sheetName = "test_sheet";
gew.addSheet(sheetName, headers, rowsDataMap);

// Write
gew.write(filepath_output_file);
}

@Test
public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException {
final String filepath_output_file = _path_test_output.concat("map_2_sheets_default_sheetname.xlsx");

// Excel Writer
LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new XlsWriter();

// Headers
final List<String> headers = new ArrayList<>();
headers.add("Slno.");
headers.add("Name");
headers.add("Age");
headers.add("Gender");
headers.add("Height (mts)");
headers.add("Address");

// Add Sheets
final String sheetName = null;
gew.addSheet(sheetName, headers, new ArrayList<>());

// Write
gew.write(filepath_output_file);
}

}
63 changes: 63 additions & 0 deletions src/test/java/io/github/millij/poi/ss/writer/XlsxWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -115,4 +117,65 @@ public void test_write_xlsx_multiple_sheets() throws IOException {
gew.write(filepath_output_file);
}

@Test
public void test_write_map_as_xlsx_sheet() throws IOException {
final String filepath_output_file = _path_test_output.concat("map_2_sheets.xlsx");

// Excel Writer
LOGGER.info("test_write_map_as_xlsx_sheet :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new XlsWriter();

// Headers
final List<String> headers = new ArrayList<>();
headers.add("Slno.");
headers.add("Name");
headers.add("Age");
headers.add("Gender");
headers.add("Height (mts)");
headers.add("Address");

// Data
final List<Map<String, String>> rowsDataMap = new ArrayList<>();
final Map<String, String> rowDataMap = new HashMap<>();
rowDataMap.put("Slno.", "1");
rowDataMap.put("Name", "foo");
rowDataMap.put("Age", "1");
rowDataMap.put("Gender", "Male");
rowDataMap.put("Height (mts)", "1.6");
rowDataMap.put("Address", "Chennai, India");
rowsDataMap.add(rowDataMap);

// Add Sheets
final String sheetName = "test_sheet";
gew.addSheet(sheetName, headers, rowsDataMap);

// Write
gew.write(filepath_output_file);
}

@Test
public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException {
final String filepath_output_file = _path_test_output.concat("map_2_sheets_default_sheetname.xlsx");

// Excel Writer
LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new XlsWriter();

// Headers
final List<String> headers = new ArrayList<>();
headers.add("Slno.");
headers.add("Name");
headers.add("Age");
headers.add("Gender");
headers.add("Height (mts)");
headers.add("Address");

// Add Sheets
final String sheetName = null;
gew.addSheet(sheetName, headers, new ArrayList<>());

// Write
gew.write(filepath_output_file);
}

}