From e9465701f351bffac68742208f15404ed133a663 Mon Sep 17 00:00:00 2001 From: mayankanand7 <56910213+mayankanand7@users.noreply.github.com> Date: Wed, 18 Dec 2024 04:44:46 +0000 Subject: [PATCH] Merge pull request #1 from mayankanand7/map-to-excel-sheet Map to Excel Sheet --- .../ss/writer/AbstractSpreadsheetWriter.java | 55 ++++++++++++++++ .../poi/ss/writer/SpreadsheetWriter.java | 15 +++++ .../millij/poi/ss/writer/XlsWriterTest.java | 63 +++++++++++++++++++ .../millij/poi/ss/writer/XlsxWriterTest.java | 63 +++++++++++++++++++ 4 files changed, 196 insertions(+) diff --git a/src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java b/src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java index 17f0e3a..63d1f2c 100644 --- a/src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java +++ b/src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java @@ -109,6 +109,61 @@ public void addSheet(final Class beanType, final List rowObjects, fina } } + @Override + public void addSheet(final String inSheetName, final List inHeaders, + final List> 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 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 diff --git a/src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java b/src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java index 327fd21..0c38275 100644 --- a/src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java +++ b/src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.util.List; +import java.util.Map; /** @@ -73,6 +74,20 @@ default void addSheet(Class beanClz, List beans, List headers) void addSheet(Class beanClz, List beans, String sheetName, List headers); + /** + * This method will attempt to add a new sheet and add the rows of data from the rows data. The + * null 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 null for default name) + * @param headers a {@link List} of Header names to write in the file. null 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 headers, List> rowsData); + + /** * Writes the current Spreadsheet workbook to a file in the specified path. * diff --git a/src/test/java/io/github/millij/poi/ss/writer/XlsWriterTest.java b/src/test/java/io/github/millij/poi/ss/writer/XlsWriterTest.java index 59ac0ad..64637e1 100644 --- a/src/test/java/io/github/millij/poi/ss/writer/XlsWriterTest.java +++ b/src/test/java/io/github/millij/poi/ss/writer/XlsWriterTest.java @@ -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; @@ -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 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> rowsDataMap = new ArrayList<>(); + final Map 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 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); + } + } diff --git a/src/test/java/io/github/millij/poi/ss/writer/XlsxWriterTest.java b/src/test/java/io/github/millij/poi/ss/writer/XlsxWriterTest.java index 6ea1c4b..5dc274a 100644 --- a/src/test/java/io/github/millij/poi/ss/writer/XlsxWriterTest.java +++ b/src/test/java/io/github/millij/poi/ss/writer/XlsxWriterTest.java @@ -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; @@ -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 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> rowsDataMap = new ArrayList<>(); + final Map 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 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); + } + }