From f8bf8b78803c70e7ba9d485699e614b0cea43f6d Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Tue, 17 Dec 2024 17:38:37 +0530 Subject: [PATCH 1/8] [Impl]: added method to generate excel sheet from Map of data --- .../ss/writer/AbstractSpreadsheetWriter.java | 50 +++++++++++++++++++ .../poi/ss/writer/SpreadsheetWriter.java | 3 ++ 2 files changed, 53 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..5b13973 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,56 @@ public void addSheet(final Class beanType, final List rowObjects, fina } } + @Override + public void addSheet(final List> rowsData, final String inSheetName, + final List 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 rowData = rowsData.get(i); + + 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); + } + } + // 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..a0d90d4 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; /** @@ -72,6 +73,8 @@ default void addSheet(Class beanClz, List beans, List headers) */ void addSheet(Class beanClz, List beans, String sheetName, List headers); + void addSheet(List> rowsData, String sheetName, List headers); + /** * Writes the current Spreadsheet workbook to a file in the specified path. From ae76c0dee6ccc42ac9b4be00d94eb785bc7fecde Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Tue, 17 Dec 2024 17:39:08 +0530 Subject: [PATCH 2/8] [Test]: added test cases for the method --- .../millij/poi/ss/writer/XlsWriterTest.java | 61 +++++++++++++++++++ .../millij/poi/ss/writer/XlsxWriterTest.java | 61 +++++++++++++++++++ 2 files changed, 122 insertions(+) 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..d7309c9 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,63 @@ 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 + gew.addSheet(rowsDataMap, "test_sheet", headers); + + // Write + gew.write(filepath_output_file); + } + + @Test + public void test_write_map_as_template_xls_sheet() throws IOException { + final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xls"); + + // Excel Writer + LOGGER.info("test_write_map_as_template_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"); + + // Add Sheets + gew.addSheet(new ArrayList<>(), "test_sheet", headers); + + // 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..afa1442 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,63 @@ 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 + gew.addSheet(rowsDataMap, "test_sheet", headers); + + // Write + gew.write(filepath_output_file); + } + + @Test + public void test_write_map_as_template_xlsx_sheet() throws IOException { + final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xlsx"); + + // Excel Writer + LOGGER.info("test_write_map_as_template_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"); + + // Add Sheets + gew.addSheet(new ArrayList<>(), "test_sheet", headers); + + // Write + gew.write(filepath_output_file); + } + } From 6ad089fb73d3c5024bd34b882fa34d324edfdabc Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Tue, 17 Dec 2024 17:58:11 +0530 Subject: [PATCH 3/8] [Test]: added test case for default sheet name --- .../millij/poi/ss/writer/XlsWriterTest.java | 24 +++++++++++++++++++ .../millij/poi/ss/writer/XlsxWriterTest.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) 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 d7309c9..8e30f5e 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 @@ -176,4 +176,28 @@ public void test_write_map_as_template_xls_sheet() throws IOException { 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 + gew.addSheet(new ArrayList<>(), null, headers); + + // 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 afa1442..23fa869 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 @@ -176,4 +176,28 @@ public void test_write_map_as_template_xlsx_sheet() throws IOException { 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 + gew.addSheet(new ArrayList<>(), null, headers); + + // Write + gew.write(filepath_output_file); + } + } From 61511272c598dab4499cb39e466ad47e0632416a Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Tue, 17 Dec 2024 19:14:23 +0530 Subject: [PATCH 4/8] [Impl]: added an implementation to create an excel sheet as template file --- .../ss/writer/AbstractSpreadsheetWriter.java | 33 +++++++++++++++++++ .../poi/ss/writer/SpreadsheetWriter.java | 21 ++++++++++++ 2 files changed, 54 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 5b13973..094e138 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 @@ -160,6 +160,39 @@ public void addSheet(final List> rowsData, final String inSh } + @Override + public void createTemplate(final String sheetName, final List headers) { + // 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 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 a0d90d4..f099bc3 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 @@ -73,9 +73,30 @@ 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. + * + * @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 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. + */ void addSheet(List> rowsData, String sheetName, List headers); + /** + * 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 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. + */ + void createTemplate(String sheetName, List headers); + + /** * Writes the current Spreadsheet workbook to a file in the specified path. * From f96643afe948f21a9d22420d119c80d213056999 Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Tue, 17 Dec 2024 19:14:40 +0530 Subject: [PATCH 5/8] [Test]: Updated the test cases --- .../millij/poi/ss/writer/XlsWriterTest.java | 16 ++++++++-------- .../millij/poi/ss/writer/XlsxWriterTest.java | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) 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 8e30f5e..06a3bab 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 @@ -153,11 +153,11 @@ public void test_write_map_as_xls_sheet() throws IOException { } @Test - public void test_write_map_as_template_xls_sheet() throws IOException { - final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xls"); + 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_template_xls_sheet :: Writing to file - {}", filepath_output_file); + LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file); SpreadsheetWriter gew = new XlsWriter(); // Headers @@ -170,18 +170,18 @@ public void test_write_map_as_template_xls_sheet() throws IOException { headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), "test_sheet", headers); + gew.addSheet(new ArrayList<>(), null, headers); // 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"); + public void test_write_map_as_template_xls_sheet() throws IOException { + final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xls"); // Excel Writer - LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file); + LOGGER.info("test_write_map_as_template_xls_sheet :: Writing to file - {}", filepath_output_file); SpreadsheetWriter gew = new XlsWriter(); // Headers @@ -194,7 +194,7 @@ public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), null, headers); + gew.createTemplate("test_sheet", headers); // 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 23fa869..eccfc36 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 @@ -153,11 +153,11 @@ public void test_write_map_as_xlsx_sheet() throws IOException { } @Test - public void test_write_map_as_template_xlsx_sheet() throws IOException { - final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xlsx"); + 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_template_xlsx_sheet :: Writing to file - {}", filepath_output_file); + LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file); SpreadsheetWriter gew = new XlsWriter(); // Headers @@ -170,18 +170,18 @@ public void test_write_map_as_template_xlsx_sheet() throws IOException { headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), "test_sheet", headers); + gew.addSheet(new ArrayList<>(), null, headers); // 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"); + public void test_write_map_as_template_xlsx_sheet() throws IOException { + final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xlsx"); // Excel Writer - LOGGER.info("test_write_map_as_xlsx_sheet_default_sheetname :: Writing to file - {}", filepath_output_file); + LOGGER.info("test_write_map_as_template_xlsx_sheet :: Writing to file - {}", filepath_output_file); SpreadsheetWriter gew = new XlsWriter(); // Headers @@ -194,7 +194,7 @@ public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), null, headers); + gew.createTemplate("test_sheet", headers); // Write gew.write(filepath_output_file); From 201bdb3dd86529e60850f1a97f4ccdd2ac6b7e07 Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Wed, 18 Dec 2024 10:06:52 +0530 Subject: [PATCH 6/8] [refactor]: removed createTemplate method and reordered addSheet method arguments --- .../ss/writer/AbstractSpreadsheetWriter.java | 54 +++++-------------- .../poi/ss/writer/SpreadsheetWriter.java | 21 +++----- 2 files changed, 19 insertions(+), 56 deletions(-) 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 094e138..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 @@ -110,8 +110,8 @@ public void addSheet(final Class beanType, final List rowObjects, fina } @Override - public void addSheet(final List> rowsData, final String inSheetName, - final List inHeaders) { + 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"); @@ -135,9 +135,9 @@ public void addSheet(final List> rowsData, final String inSh // 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)); + for (int cellNum = 0; cellNum < inHeaders.size(); cellNum++) { + final Cell cell = headerRow.createCell(cellNum); + cell.setCellValue(inHeaders.get(cellNum)); } // Data Rows @@ -145,11 +145,16 @@ public void addSheet(final List> rowsData, final String inSh final Row row = sheet.createRow(rowNum); final Map rowData = rowsData.get(i); - for (int cellNo = 0; cellNo < inHeaders.size(); cellNo++) { - final String key = inHeaders.get(cellNo); + // 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(cellNo); + final Cell cell = row.createCell(cellNum); cell.setCellValue(value); } } @@ -160,39 +165,6 @@ public void addSheet(final List> rowsData, final String inSh } - @Override - public void createTemplate(final String sheetName, final List headers) { - // 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 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 f099bc3..dfb22c1 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 @@ -75,26 +75,17 @@ default void addSheet(Class beanClz, List beans, List headers) /** - * This method will attempt to add a new sheet and add the rows of data from the rows data. + * This method will attempt to add a new sheet and add the rows of data from the rows data. The null + * element in the rows data list will be skipped and no rows will be added to the sheet. An empty + * row in the sheet will be added in the sheet for empty {@link Map} in 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 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(List> rowsData, String sheetName, List headers); - - - /** - * 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 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. - */ - void createTemplate(String sheetName, List headers); + void addSheet(String sheetName, List headers, List> rowsData); /** From 690f01ebcb7b1581dd2e6b3d908b41f5faa59cf3 Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Wed, 18 Dec 2024 10:08:15 +0530 Subject: [PATCH 7/8] [test]: deleted test case for createTemplate method and refactor test case for addSheet with rows data as List of Map --- .../millij/poi/ss/writer/XlsWriterTest.java | 30 +++---------------- .../millij/poi/ss/writer/XlsxWriterTest.java | 30 +++---------------- 2 files changed, 8 insertions(+), 52 deletions(-) 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 06a3bab..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 @@ -146,7 +146,8 @@ public void test_write_map_as_xls_sheet() throws IOException { rowsDataMap.add(rowDataMap); // Add Sheets - gew.addSheet(rowsDataMap, "test_sheet", headers); + final String sheetName = "test_sheet"; + gew.addSheet(sheetName, headers, rowsDataMap); // Write gew.write(filepath_output_file); @@ -170,31 +171,8 @@ public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), null, headers); - - // Write - gew.write(filepath_output_file); - } - - @Test - public void test_write_map_as_template_xls_sheet() throws IOException { - final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xls"); - - // Excel Writer - LOGGER.info("test_write_map_as_template_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"); - - // Add Sheets - gew.createTemplate("test_sheet", headers); + 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 eccfc36..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 @@ -146,7 +146,8 @@ public void test_write_map_as_xlsx_sheet() throws IOException { rowsDataMap.add(rowDataMap); // Add Sheets - gew.addSheet(rowsDataMap, "test_sheet", headers); + final String sheetName = "test_sheet"; + gew.addSheet(sheetName, headers, rowsDataMap); // Write gew.write(filepath_output_file); @@ -170,31 +171,8 @@ public void test_write_map_as_xlsx_sheet_default_sheetname() throws IOException headers.add("Address"); // Add Sheets - gew.addSheet(new ArrayList<>(), null, headers); - - // Write - gew.write(filepath_output_file); - } - - @Test - public void test_write_map_as_template_xlsx_sheet() throws IOException { - final String filepath_output_file = _path_test_output.concat("map_2_template_sheets.xlsx"); - - // Excel Writer - LOGGER.info("test_write_map_as_template_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"); - - // Add Sheets - gew.createTemplate("test_sheet", headers); + final String sheetName = null; + gew.addSheet(sheetName, headers, new ArrayList<>()); // Write gew.write(filepath_output_file); From e9c03bac6d9aca4f2a20c28855434cc6dbb0720f Mon Sep 17 00:00:00 2001 From: mayankanand7 Date: Wed, 18 Dec 2024 10:14:16 +0530 Subject: [PATCH 8/8] [Javadoc]: updated javadoc of the addSheet method --- .../io/github/millij/poi/ss/writer/SpreadsheetWriter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 dfb22c1..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 @@ -75,9 +75,9 @@ default void addSheet(Class beanClz, List beans, List headers) /** - * This method will attempt to add a new sheet and add the rows of data from the rows data. The null - * element in the rows data list will be skipped and no rows will be added to the sheet. An empty - * row in the sheet will be added in the sheet for empty {@link Map} in the rows data. + * 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