public class Workbook
extends java.lang.Object
implements java.io.Closeable
Example demonstrating the creation and manipulation of an Excel workbook using Groovy.
The code performs the following operations:
workbook().withCloseable { Workbook book ->
book.createSheet().with { sheet ->
addRow('','Jan','Feb','Mar')
addRow('Plan','10','','30')
addRow('Budget','','200','100')
addRow('Actual','','110','120')
}
book.createSheet('Sheet#2').with { sheet ->
addRow('','Apr','May','Jun')
addRow('Variance','1','2','3')
addRow('Revised','10','20','')
}
book.getSheet(0).addRow('Working','15','20','300')
book.save('Test.xlsx')
}
| Modifier and Type | Class and Description |
|---|---|
class |
Workbook.Sheet
Represents a sheet in the workbook, allowing rows to be added with specified cell values.
|
| Constructor and Description |
|---|
Workbook()
Constructs a new, empty workbook.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the workbook and releases any system resources associated with it.
|
Workbook.Sheet |
createSheet()
Creates a new sheet in the workbook with a default name (e.g., "Sheet1", "Sheet2").
|
Workbook.Sheet |
createSheet(java.lang.String sheetName)
Creates a new sheet in the workbook with the specified name.
|
Workbook.Sheet |
getSheet(int sheetIndex)
Retrieves the sheet at the specified index.
|
void |
save(java.lang.String fileName)
Saves the workbook to the specified file on the outbox.
|
void |
write(java.io.OutputStream outputStream)
Writes the workbook to the provided output stream.
|
public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOException - if an I/O error occurs while closing the workbookpublic Workbook.Sheet createSheet()
Workbook.Sheetjava.lang.Exception - if the maximum number of sheets is exceeded or workbook is closedpublic Workbook.Sheet createSheet(java.lang.String sheetName)
sheetName - the name of the new sheet; must not be nullWorkbook.Sheetjava.lang.IllegalArgumentException - if sheetName is nulljava.lang.Exception - if the maximum number of sheets is exceeded or workbook is closedpublic Workbook.Sheet getSheet(int sheetIndex)
sheetIndex - the zero-based index of the sheetWorkbook.Sheet at the given indexjava.lang.IllegalArgumentException - if the index is out of rangepublic void save(java.lang.String fileName)
throws java.io.IOException
fileName - name of the file on the outbox.
A valid file name may only contain letters, digits, underscores (_), hyphens (-), periods (.), and spaces.
java.io.IOException - if an I/O error occurs during writingjava.lang.IllegalArgumentException - if fileName is nullcom.hyperion.planning.HspRuntimeException - if the file name contains invalid characterspublic void write(java.io.OutputStream outputStream)
throws java.io.IOException
outputStream - the output stream to write to
The caller is responsible for closing this stream when finished with itjava.io.IOException - if an I/O error occurs during writingjava.lang.IllegalArgumentException - if outputStream is nullCopyright © 2017, 2025, Oracle and/or its affiliates. All rights reserved.