diff --git a/build.gradle b/build.gradle index dd36f80..393df86 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ apply plugin: "jacoco" group 'com.jameskleeh' -version '0.4.4' +version '0.4.5-SNAPSHOT' targetCompatibility = 1.7 sourceCompatibility = 1.7 diff --git a/src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy b/src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy index 6d0f1be..985652a 100644 --- a/src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy +++ b/src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy @@ -34,21 +34,23 @@ class ExcelBuilder { * Builds an excel document and sends the data to an output stream. The output stream is NOT closed. * * @param outputStream An output stream to push data onto + * @param template A excel file that is used a template, if null a new workbook will be created. * @param callable The closure to build the document */ - static void output(OutputStream outputStream, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Workbook) Closure callable) { - XSSFWorkbook wb = build(callable) + static void output(InputStream template = null, OutputStream outputStream, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Workbook) Closure callable) { + XSSFWorkbook wb = build(template, callable) wb.write(outputStream) } /** * Builds an excel document * + * @param template A excel file that is used a template, if null a new workbook will be created. * @param callable The closure to build the document * @return The native workbook */ - static XSSFWorkbook build(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Workbook) Closure callable) { - XSSFWorkbook wb = new XSSFWorkbook() + static XSSFWorkbook build(InputStream template = null, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Workbook) Closure callable) { + XSSFWorkbook wb = template == null ? new XSSFWorkbook() : new XSSFWorkbook(template) callable.resolveStrategy = Closure.DELEGATE_FIRST callable.delegate = new Workbook(wb) if (callable.maximumNumberOfParameters == 1) { diff --git a/src/main/groovy/com/jameskleeh/excel/Workbook.groovy b/src/main/groovy/com/jameskleeh/excel/Workbook.groovy index 5c76af7..94c5a96 100644 --- a/src/main/groovy/com/jameskleeh/excel/Workbook.groovy +++ b/src/main/groovy/com/jameskleeh/excel/Workbook.groovy @@ -52,7 +52,9 @@ class Workbook { * @return The native sheet object */ XSSFSheet sheet(String name, Map options, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Sheet) Closure callable) { - handleSheet(wb.createSheet(WorkbookUtil.createSafeSheetName(name)), options, callable) + XSSFSheet sheet = wb.getSheet(name) + sheet = sheet ?: wb.createSheet(WorkbookUtil.createSafeSheetName(name)) + handleSheet(sheet, options, callable) } /** diff --git a/src/test/groovy/com/jameskleeh/excel/WorkbookSpec.groovy b/src/test/groovy/com/jameskleeh/excel/WorkbookSpec.groovy index 0fa9d11..375bd97 100644 --- a/src/test/groovy/com/jameskleeh/excel/WorkbookSpec.groovy +++ b/src/test/groovy/com/jameskleeh/excel/WorkbookSpec.groovy @@ -1,8 +1,10 @@ package com.jameskleeh.excel +import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.Sheet +import org.apache.poi.xssf.usermodel.XSSFSheet import org.apache.poi.xssf.usermodel.XSSFWorkbook import spock.lang.Specification -import org.apache.poi.ss.usermodel.Sheet class WorkbookSpec extends Specification { @@ -29,4 +31,25 @@ class WorkbookSpec extends Specification { config.defaultRowHeightInPoints == 12F config.defaultColumnWidth == 20 } + + void "test open with template and use sheet by name access"() { + given: + XSSFWorkbook workbook = ExcelBuilder.build(this.class.getResourceAsStream('/template.xlsx')) { + sheet('sheet') { + row { + cell('created with builder') + } + } + } + + when: + XSSFSheet sheet = workbook.getSheet('sheet') + Iterator buildRow = sheet.getRow(0).cellIterator() + Iterator rowFromTemplate = sheet.getRow(1).cellIterator() + + then: + buildRow.next().stringCellValue == 'created with builder' + rowFromTemplate.next().stringCellValue == 'from template' + } + } diff --git a/src/test/resources/template.xlsx b/src/test/resources/template.xlsx new file mode 100644 index 0000000..a4ffb11 Binary files /dev/null and b/src/test/resources/template.xlsx differ