Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/groovy/com/jameskleeh/excel/Workbook.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
25 changes: 24 additions & 1 deletion src/test/groovy/com/jameskleeh/excel/WorkbookSpec.groovy
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<Cell> buildRow = sheet.getRow(0).cellIterator()
Iterator<Cell> rowFromTemplate = sheet.getRow(1).cellIterator()

then:
buildRow.next().stringCellValue == 'created with builder'
rowFromTemplate.next().stringCellValue == 'from template'
}

}
Binary file added src/test/resources/template.xlsx
Binary file not shown.