XLS - Excel
Process custom Excel files.
Introduction
Netuno generates EXCEL files compatible with LibreOffice, OpenOffice, Microsoft Office, Google Drive, etc., through the XLS resource, which provides a low-code abstraction of Apache POI for the various programming languages supported by Netuno.
We can generate XLSX, XLS, or ODS files.
The application that comes with Netuno called demo (the demonstration application) contains many examples of possible code implementations in various languages, including a demonstration of spreadsheet export through the export-excel service located at:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
netuno/apps/demo/server/services/samples/javascript/export-excel.js
netuno/apps/demo/server/services/samples/typescript/export-excel.ts
netuno/apps/demo/server/services/samples/python/export-excel.py
netuno/apps/demo/server/services/samples/ruby/export-excel.rb
netuno/apps/demo/server/services/samples/kotlin/export-excel.kts
netuno/apps/demo/server/services/samples/groovy/export-excel.groovy
Initialization
To initialize the XLS resource:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const excel = _xls.create()
const excel = _xls.create()
excel = _xls.create()
excel = _xls.create()
val excel = _xls.create()
final excel = _xls.create()
Creates the object that contains the context of the new file to be built, with an empty worksheet.
Formatting
The definition of visual formatting styles—that is, date, time, currency formats, and others—must be done before defining cell values, because when defining cell values it is possible to associate their formatting style along with them.
Thus, as demonstrated in the example included in the demo application, the formats are defined at the beginning of the code.
To format dates:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleDate = excel.cellStyleFormat("dd/mm/yy")
const styleDate = excel.cellStyleFormat("dd/mm/yy")
styleDate = excel.cellStyleFormat("dd/mm/yy")
styleDate = excel.cellStyleFormat("dd/mm/yy")
val styleDate = excel.cellStyleFormat("dd/mm/yy")
final styleDate = excel.cellStyleFormat("dd/mm/yy")
To format times:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleTime = excel.cellStyleFormat("hh:mm:ss")
const styleTime = excel.cellStyleFormat("hh:mm:ss")
styleTime = excel.cellStyleFormat("hh:mm:ss")
styleTime = excel.cellStyleFormat("hh:mm:ss")
val styleTime = excel.cellStyleFormat("hh:mm:ss")
final styleTime = excel.cellStyleFormat("hh:mm:ss")
To format dates with times:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
const styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
val styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
final styleDateTime = excel.cellStyleFormat("dd/mm/yyyy hh:mm:ss")
To format currency values in euros:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
const styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
val styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
final styleMoneyEuro = excel.cellStyleFormat("€#,##0.00;\-€#,##0.00")
To format currency values in Brazilian reais:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
const styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
val styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
final styleMoneyBRL = excel.cellStyleFormat("R$#,##0.00;\-R$#,##0.00")
To format values as percentages:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const stylePercentage = excel.cellStyleFormat("0.00%")
const stylePercentage = excel.cellStyleFormat("0.00%")
stylePercentage = excel.cellStyleFormat("0.00%")
stylePercentage = excel.cellStyleFormat("0.00%")
val stylePercentage = excel.cellStyleFormat("0.00%")
final stylePercentage = excel.cellStyleFormat("0.00%")
Appearance
The definition of visual appearance styles—that is, the appearance of cells such as colors, font, borders, alignment, and others—must be done before defining cell values, because when defining cell values it is possible to associate their appearance style along with them.
Thus, as demonstrated in the example included in the demo application, the appearances are defined at the beginning of the code.
Example of defining colors and alignment:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
const styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
val styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
final styleHeader = excel.cellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(excel.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
Documentation with all methods of the Styles object in Apache POI.
Defining the border style:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
const styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
val styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
final styleData = excel.cellStyle()
styleData.setBorderBottom(_xls.borderStyle("thin"))
styleData.setBorderTop(_xls.borderStyle("thin"))
styleData.setBorderLeft(_xls.borderStyle("thin"))
styleData.setBorderRight(_xls.borderStyle("thin"))
Documentation with all methods of the Styles object in Apache POI.
Creates a new font style for the text and associates the font definitions with a specific existing cell style:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
// Set the font of styleHeader
styleHeader.setFont(fontTitle)
const fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
// Set the font of styleHeader
styleHeader.setFont(fontTitle)
fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
# Set the font of styleHeader
styleHeader.setFont(fontTitle)
fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
# Set the font of styleHeader
styleHeader.setFont(fontTitle)
val fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
// Set the font of styleHeader
styleHeader.setFont(fontTitle)
final fontTitle = excel.font()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(excel.color("yellow"))
// Set the font of styleHeader
styleHeader.setFont(fontTitle)
Documentation with all methods of the Font object in Apache POI.
Image
To insert an image into the worksheet that is located at storage/samples/export-excel/logo.png inside the application:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, // Column Number
1 // Row Number
).resize(2.3)
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, // Column Number
1 // Row Number
).resize(2.3)
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, # Column Number
1 # Row Number
).resize(2.3)
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, # Column Number
1 # Row Number
).resize(2.3)
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, // Column Number
1 // Row Number
).resize(2.3)
excel.insertPicture(
_storage.filesystem("server", "samples/export-excel", "logo.png"),
1, // Column Number
1 // Row Number
).resize(2.3)
To insert an image that is in the application at public/images/logo.png:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.insertPicture(
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
_app.file("public/images/logo.png"),
10, # Column Number
1 # Row Number
).resize(1.5)
excel.insertPicture(
_app.file("public/images/logo.png"),
10, # Column Number
1 # Row Number
).resize(1.5)
excel.insertPicture(
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
Documentation with all methods of the Image object in Apache POI.
Merging Cells
To merge cells in a region, we use the mergedRegion method.
The parameter order is:
- Starting Row
- Starting Column
- Ending Row
- Ending Column
Numeric positioning references always start at zero.
In the example below, all cells from column 10 through column 15 that are on row 3 will be merged:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.mergedRegion(2, 9, 2, 14)
excel.mergedRegion(2, 9, 2, 14)
excel.mergedRegion(2, 9, 2, 14)
excel.mergedRegion(2, 9, 2, 14)
excel.mergedRegion(2, 9, 2, 14)
excel.mergedRegion(2, 9, 2, 14)
Cells
To get the object that represents a cell:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Gets the cell located in column B and row 2:
const cell = excel.cell(1, 1)
// Gets the cell located in column B and row 2:
const cell = excel.cell(1, 1)
# Gets the cell located in column B and row 2:
cell = excel.cell(1, 1)
# Gets the cell located in column B and row 2:
cell = excel.cell(1, 1)
// Gets the cell located in column B and row 2:
val cell = excel.cell(1, 1)
// Gets the cell located in column B and row 2:
final cell = excel.cell(1, 1)
Numeric positioning references always start at zero.
Example of how to log the cell formula:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
// Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
# Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
# Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
// Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
// Logs the formula of row 2 and column B.
_log.info(
"Cell Formula B2: "+ excel.cell(1, 1).getCellFormula()
)
Example of how to get the text value of a cell:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
// Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
# Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
# Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
// Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
// Logs the text value of the cell in row 2 and column B.
_log.info(
"Cell Value B2: "+ excel.cell(1, 1).getStringCellValue()
)
Documentation with all methods of the Cell object in Apache POI.
Rows
To get the object that represents a row:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Gets the object that represents row 2.
const row = excel.row(1)
// Gets the object that represents row 2.
const row = excel.row(1)
# Gets the object that represents row 2.
row = excel.row(1)
# Gets the object that represents row 2.
row = excel.row(1)
// Gets the object that represents row 2.
val row = excel.row(1)
// Gets the object that represents row 2.
final row = excel.row(1)
Numeric positioning references always start at zero.
In the example above, if the row does not exist, a new row will be created automatically.
Documentation with all methods of the Row object in Apache POI.
Inserting Tabular Data
We can insert a data table using the addDataTable method.
Since the first two parameters define the position where the data table should be inserted, they are the row and column numbers.
Note that positioning in the code for rows and columns always starts from zero.
That is, Row 1 is position
0, and Column A is also position0.
Next, we pass a list of rows, where each row contains another list for the columns, and only then do we add a map with the cell value.
Complete example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.addDataTable(
6, 1,
_val.list() // Rows
.add(
_val.list() // Column
.add(
_val.map() // Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( // Another Row
_val.list()
.add( // First Column
_val.map()
.set("value", "Maria")
)
.add( // Second Column
_val.map()
.set("value", 26)
)
)
)
excel.addDataTable(
6, 1,
_val.list() // Rows
.add(
_val.list() // Column
.add(
_val.map() // Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( // Another Row
_val.list()
.add( // First Column
_val.map()
.set("value", "Maria")
)
.add( // Second Column
_val.map()
.set("value", 26)
)
)
)
excel.addDataTable(
6, 1,
_val.list() # Rows
.add(
_val.list() # Column
.add(
_val.map() # Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( # Another Row
_val.list()
.add( # First Column
_val.map()
.set("value", "Maria")
)
.add( # Second Column
_val.map()
.set("value", 26)
)
)
)
excel.addDataTable(
6, 1,
_val.list() # Rows
.add(
_val.list() # Column
.add(
_val.map() # Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( # Another Row
_val.list()
.add( # First Column
_val.map()
.set("value", "Maria")
)
.add( # Second Column
_val.map()
.set("value", 26)
)
)
)
excel.addDataTable(
6, 1,
_val.list() // Rows
.add(
_val.list() // Column
.add(
_val.map() // Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( // Another Row
_val.list()
.add( // First Column
_val.map()
.set("value", "Maria")
)
.add( // Second Column
_val.map()
.set("value", 26)
)
)
)
excel.addDataTable(
6, 1,
_val.list() // Rows
.add(
_val.list() // Column
.add(
_val.map() // Cell
.set("value", "Name")
)
.add(
_val.map()
.set("value", "Age")
)
)
.add( // Another Row
_val.list()
.add( // First Column
_val.map()
.set("value", "Maria")
)
.add( // Second Column
_val.map()
.set("value", 26)
)
)
)
Appearance
In the cell map, we can associate the styles already created, for example using the styles defined in the appearance and formatting section we saw earlier.
The following is an example of how to associate styles with cells:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(
_val.map()
.set("value", "Name")
.set("style", styleHeader)
)
.add(
_val.map()
.set("value", "Age")
.set("style", styleHeader)
)
)
)
Formulas
To insert formulas, we use the formula key in the cell definition map.
In this example, the sum of all values from the other cells will be performed in the last cell:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
To get the column letter from the column number, you can use the columnReference method, which can help when defining formulas. The same method can also be used to get the column number from the reference letter, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
excel.addDataTable(
6, _xls.columnReference('B'),
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set(
"formula",
"SUM("+ _xls.columnReference(1) +"7:"+ _xls.columnReference(2) +"8)"
)
)
)
)
Sheets
To create a new worksheet (sheet) within the document itself, see the following example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const newSheet = excel.createSheet("New Sheet")
const newSheet = excel.createSheet("New Sheet")
newSheet = excel.createSheet("New Sheet")
newSheet = excel.createSheet("New Sheet")
val newSheet = excel.createSheet("New Sheet")
final newSheet = excel.createSheet("New Sheet")
In the methods for manipulating cells, images, and data, the sheet object reference can be added as the first parameter.
Depending on which sheet is active, it is possible to change the active sheet with the activeSheet method.
Example of how to insert an image into the new sheet:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, # Column Number
1 # Row Number
).resize(1.5)
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, # Column Number
1 # Row Number
).resize(1.5)
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
excel.insertPicture(
newSheet,
_app.file("public/images/logo.png"),
10, // Column Number
1 // Row Number
).resize(1.5)
Example of how to insert a data table into the new sheet:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
excel.addDataTable(
newSheet,
6, 1,
_val.list()
.add(
_val.list()
.add(_val.map().set("value", 45))
.add(_val.map().set("value", 26))
)
.add(
_val.list()
.add(_val.map().set("value", 51))
.add(_val.map().set("value", 30))
)
.add(
_val.list()
.add(
_val.map().set("value", "Total")
)
.add(
_val.map().set("formula", "SUM(B7:C8)")
)
)
)
Activate
So, to avoid having to constantly indicate in the methods which sheet should be processed, as in the examples above, as an alternative you can indicate which sheet is active.
All methods for manipulating cells, images, and data use the sheet that is activated when a specific sheet is not indicated.
See two ways to activate a specific sheet, first with the object reference and then with the sheet index:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.activeSheet(mySheet) // Sheet reference object.
excel.activeSheet(1) // Index of the sheet that will be activated.
excel.activeSheet(mySheet) // Sheet reference object.
excel.activeSheet(1) // Index of the sheet that will be activated.
excel.activeSheet(mySheet) # Sheet reference object.
excel.activeSheet(1) # Index of the sheet that will be activated.
excel.activeSheet(mySheet) # Sheet reference object.
excel.activeSheet(1) # Index of the sheet that will be activated.
excel.activeSheet(mySheet) // Sheet reference object.
excel.activeSheet(1) // Index of the sheet that will be activated.
excel.activeSheet(mySheet) // Sheet reference object.
excel.activeSheet(1) // Index of the sheet that will be activated.
Useful Resources
Examples of other useful methods for manipulating sheets.
Get a sheet by name:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Gets the sheet from its name.
const mySheet = excel.getSheet("Sheet Name")
// Gets the sheet from its name.
const mySheet = excel.getSheet("Sheet Name")
# Gets the sheet from its name.
mySheet = excel.getSheet("Sheet Name")
# Gets the sheet from its name.
mySheet = excel.getSheet("Sheet Name")
// Gets the sheet from its name.
val mySheet = excel.getSheet("Sheet Name")
// Gets the sheet from its name.
final mySheet = excel.getSheet("Sheet Name")
Get a sheet by its index:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Gets the sheet from its number.
const mySheet = excel.getSheet(1)
// Gets the sheet from its number.
const mySheet = excel.getSheet(1)
# Gets the sheet from its number.
mySheet = excel.getSheet(1)
# Gets the sheet from its number.
mySheet = excel.getSheet(1)
// Gets the sheet from its number.
val mySheet = excel.getSheet(1)
// Gets the sheet from its number.
final mySheet = excel.getSheet(1)
To get the total number of sheets:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Total number of sheets.
const totalSheets = excel.getNumberOfSheets()
// Total number of sheets.
const totalSheets = excel.getNumberOfSheets()
# Total number of sheets.
totalSheets = excel.getNumberOfSheets()
# Total number of sheets.
totalSheets = excel.getNumberOfSheets()
// Total number of sheets.
val totalSheets = excel.getNumberOfSheets()
// Total number of sheets.
final totalSheets = excel.getNumberOfSheets()
To list all existing sheets in the document.
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// Lists all sheets.
const allSheets = excel.getAllSheets()
for (const sheet of allSheets) {
...
}
// Lists all sheets.
const allSheets = excel.getAllSheets()
for (const sheet of allSheets) {
...
}
# Lists all sheets.
allSheets = excel.getAllSheets()
for sheet in allSheets:
...
# Lists all sheets.
allSheets = excel.getAllSheets()
allSheets.each do |sheet|
...
end
// Lists all sheets.
val allSheets = excel.getAllSheets()
allSheets.forEach {
...
}
// Lists all sheets.
def allSheets = excel.getAllSheets()
for (sheet in allSheets) {
...
}
Documentation with all methods of the Sheet object in Apache POI.
Generate File
In the save and output methods, the final file binary is generated.
Sends the generated file as the service's data output (output):
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.output("file.xlsx")
excel.output("file.xlsx")
excel.output("file.xlsx")
excel.output("file.xlsx")
excel.output("file.xlsx")
excel.output("file.xlsx")
To save the generated file in the application's storage/filesystem folder:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.save(_storage.filesystem("server", "file.xlsx"))
excel.save(_storage.filesystem("server", "file.xlsx"))
excel.save(_storage.filesystem("server", "file.xlsx"))
excel.save(_storage.filesystem("server", "file.xlsx"))
excel.save(_storage.filesystem("server", "file.xlsx"))
excel.save(_storage.filesystem("server", "file.xlsx"))
To save the generated file to the /tmp folder using the hard drive file system:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
excel.save(_os.file("/tmp/file.xlsx"))
excel.save(_os.file("/tmp/file.xlsx"))
excel.save(_os.file("/tmp/file.xlsx"))
excel.save(_os.file("/tmp/file.xlsx"))
excel.save(_os.file("/tmp/file.xlsx"))
excel.save(_os.file("/tmp/file.xlsx"))
Edit File
We can edit files by opening them, making the necessary changes, and then saving the new edited file.
Below are some examples of how to open files.
Open a file at the root of the application:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const excel = _xls.open(_app.file("file.xlsx"))
const excel = _xls.open(_app.file("file.xlsx"))
excel = _xls.open(_app.file("file.xlsx"))
excel = _xls.open(_app.file("file.xlsx"))
val excel = _xls.open(_app.file("file.xlsx"))
final excel = _xls.open(_app.file("file.xlsx"))
To open the generated file in the application's storage/filesystem folder:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
const excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
val excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
final excel = _xls.open(_storage.filesystem("server", "file.xlsx"))
And also to open the file in the /tmp folder using the hard drive file system:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const excel = _xls.open(_os.file("/tmp/file.xlsx"))
const excel = _xls.open(_os.file("/tmp/file.xlsx"))
excel = _xls.open(_os.file("/tmp/file.xlsx"))
excel = _xls.open(_os.file("/tmp/file.xlsx"))
val excel = _xls.open(_os.file("/tmp/file.xlsx"))
final excel = _xls.open(_os.file("/tmp/file.xlsx"))
In the examples above, the excel constant will contain the editing object with the context of the respective file, so it is possible to perform any type of manipulation.
When you finish the changes, generate the new file.
It is not possible to directly change the same opened file; it is necessary to generate a new file with a different path.
To directly change the same file, you can do so using inputStream.
The following is an example of how to change a cell value:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const fileInput = _app.file("file.xlsx").inputStream()
const excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() // Closes the opened file.
excel.save(_app.file("file.xlsx")) // Saves the new version.
const fileInput = _app.file("file.xlsx").inputStream()
const excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() // Closes the opened file.
excel.save(_app.file("file.xlsx")) // Saves the new version.
fileInput = _app.file("file.xlsx").inputStream()
excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() # Closes the opened file.
excel.save(_app.file("file.xlsx")) # Saves the new version.
fileInput = _app.file("file.xlsx").inputStream()
excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() # Closes the opened file.
excel.save(_app.file("file.xlsx")) # Saves the new version.
val fileInput = _app.file("file.xlsx").inputStream()
val excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() // Closes the opened file.
excel.save(_app.file("file.xlsx")) // Saves the new version.
final fileInput = _app.file("file.xlsx").inputStream()
final excel = _xls.open(fileInput)
excel.setCellData(
10, 1,
_val.map()
.set(
"value",
excel.getCellData(10, 1).getInt("value") + 10
)
)
fileInput.close() // Closes the opened file.
excel.save(_app.file("file.xlsx")) // Saves the new version.
Read File
Extracting data from Excel files is done with the read method.
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const data = _xls.read(_app.file("file.xlsx"))
const data = _xls.read(_app.file("file.xlsx"))
data = _xls.read(_app.file("file.xlsx"))
data = _xls.read(_app.file("file.xlsx"))
val data = _xls.read(_app.file("file.xlsx"))
final data = _xls.read(_app.file("file.xlsx"))
The data is obtained in a structure of Values objects that contains Lists for sheets, rows, and columns, and Maps (key and value) for cells.
To view all the data, we can set it as the service's data output:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_out.json(data)
_out.json(data)
_out.json(data)
_out.json(data)
_out.json(data)
_out.json(data)
To perform the iteration, we must keep in mind that the data structure is organized in the following order:
sheets- Sheetsrows- Rowscolumns- Columns
Example of a complete iteration:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
for (const sheet of data.getValues("sheets")) {
_out.println(`<h4>Sheet: ${sheet.getInt("index")} - ${sheet.getString("name")}</h4>`)
for (const row of sheet.getValues("rows")) {
_out.println(`<h4>Row: ${row.getInt("row")}</h4>`)
_out.println("<p>")
for (const column of row.getValues("columns")) {
_out.print(`${column.getInt("row")}:${column.getInt("column")}`)
_out.print(` # ${column.getString("address")}`)
_out.print(` # ${column.getString("type")}`)
_out.print(` # ${column.getString("value")}`)
_out.println("<br>")
}
_out.println("</p>")
}
}
for (const sheet of data.getValues("sheets")) {
_out.println(`<h4>Sheet: ${sheet.getInt("index")} - ${sheet.getString("name")}</h4>`)
for (const row of sheet.getValues("rows")) {
_out.println(`<h4>Row: ${row.getInt("row")}</h4>`)
_out.println("<p>")
for (const column of row.getValues("columns")) {
_out.print(`${column.getInt("row")}:${column.getInt("column")}`)
_out.print(` # ${column.getString("address")}`)
_out.print(` # ${column.getString("type")}`)
_out.print(` # ${column.getString("value")}`)
_out.println("<br>")
}
_out.println("</p>")
}
}
for sheet in data.getValues("sheets"):
_out.println(f"<h4>Sheet: {sheet.getInt('index')} - {sheet.getString('name')}</h4>")
for row in sheet.getValues("rows"):
_out.println(f"<h4>Row: {row.getInt('row')}</h4>")
_out.println("<p>")
for column in row.getValues("columns"):
_out.print(f"{column.getInt('row')}:{column.getInt('column')}")
_out.print(f" # {column.getString('address')}")
_out.print(f" # {column.getString('type')}")
_out.print(f" # {column.getString('value')}")
_out.println("<br>")
_out.println("</p>")
data.getValues("sheets").each do |sheet|
_out.println("<h4>Sheet: #{sheet.getInt("index")} - #{sheet.getString("name")}</h4>")
sheet.getValues("rows").each do |row|
_out.println("<h4>Row: #{row.getInt("row")}</h4>")
_out.println("<p>")
row.getValues("columns").each do |column|
_out.print("#{column.getInt("row")}:#{column.getInt("column")}")
_out.print(" # #{column.getString("address")}")
_out.print(" # #{column.getString("type")}")
_out.print(" # #{column.getString("value")}")
_out.println("<br>")
end
_out.println("</p>")
end
end
data.getValues("sheets").forEach {
val sheet = it
_out.println("<h4>Sheet: ${sheet.getInt("index")} - ${sheet.getString("name")}</h4>")
sheet.getValues("rows").forEach {
val row = it
_out.println("<h4>Row: ${row.getInt("row")}</h4>")
_out.println("<p>")
row.getValues("columns").forEach {
val column = it
_out.print("${column.getInt("row")}:${column.getInt("column")}")
_out.print(" # ${column.getString("address")}")
_out.print(" # ${column.getString("type")}")
_out.print(" # ${column.getString("value")}")
_out.println("<br>")
}
_out.println("</p>")
}
}
for (sheet in data.getValues("sheets")) {
_out.println("<h4>Sheet: ${sheet.getInt('index')} - ${sheet.getString('name')}</h4>")
for (row in sheet.getValues("rows")) {
_out.println("<h4>Row: ${row.getInt('row')}</h4>")
_out.println("<p>")
for (column in row.getValues("columns")) {
_out.print("${column.getInt('row')}:${column.getInt('column')}")
_out.print(" # ${column.getString('address')}")
_out.print(" # ${column.getString('type')}")
_out.print(" # ${column.getString('value')}")
_out.println("<br>")
}
_out.println("</p>")
}
}
The cell type is obtained through the code:
column.getString("type")
And the supported types are:
string- Text content.numeric- Numeric content or date and/or time content.boolean- True or false.blank- Blank cell.formula- Cell that contains a formula.error- Error in cell processing.
Additional Parameters
For each cell type, we have some additional parameters.
string - Cell type with textual content.
value- Cell text.richValue- Cell formatting.
numeric - Cell type with numeric content, or dates and also times.
value- Value of the cell's numeric content.
In the case of dates and times, we have these additional parameters:
localDateTime- Java Time LocalDateTime object that represents the cell's date and time value.localDate- Java Time LocalDate object that represents the cell's date value.localTime- Java Time LocalTime object that represents the cell's time value.instant- Java Time Instant object that represents the cell's date and/or time value.date- Java Date object that represents the cell's date and/or time value.
boolean - Cell type with boolean content, that is, true or false.
value- Value of type boolean,trueorfalse.
blank - Cell type without content, therefore it does not contain additional parameters.
formula - Cell type that contains calculation formulas.
value- Final value resulting from the calculation.formula- Contains the formula expression used for the calculation.
error - Cell type that contains an error.
value- Byte value of the generated error.code- If there are errors in the formula calculation, provides the code of the error that occurred here.
Videos
Below are some example videos on how to generate Excel/XLSX files programmatically: