Groovy / Gradle

Download an Excel file in a Micronaut application

Learn how to download an Excel file with the Micronaut framework and Spreadsheet Builder library.

Sergio del Amo
On this guide
In this section

Getting Started

In this guide, we will demonstrate Micronaut file transfer capabilities by creating an application that downloads an Excel file containing a list of books.

What you will need

To complete this guide, you will need the following:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=gradle --lang=groovy
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

Writing the App

Books

Create Book POJO:

groovy/src/main/groovy/example/micronaut/Book.groovy
package example.micronaut

import io.micronaut.core.annotation.NonNull
import groovy.transform.EqualsAndHashCode
import io.micronaut.core.annotation.Introspected
import jakarta.validation.constraints.NotBlank

@EqualsAndHashCode
@Introspected
class Book {
    @NonNull
    @NotBlank
    private final String isbn

    @NonNull
    @NotBlank
    private final String name

    Book(@NonNull @NotBlank String isbn, @NonNull @NotBlank String name) {
        this.isbn = isbn
        this.name = name
    }

    @NonNull
    String getIsbn() {
        return isbn
    }

    @NonNull
    String getName() {
        return name
    }
}

Create an interface to encapsulate Book retrieval.

groovy/src/main/groovy/example/micronaut/BookRepository.groovy
package example.micronaut

import io.micronaut.core.annotation.NonNull
import io.micronaut.context.annotation.DefaultImplementation

@DefaultImplementation(BookRepositoryImpl)
interface BookRepository {

    @NonNull
    List<Book> findAll()
}

Create a bean which implements the previous interface:

groovy/src/main/groovy/example/micronaut/BookRepositoryImpl.groovy

Spreadsheet Builder

Add a dependency to Spreadsheet builder

Spreadsheet builder provides convenient way how to read and create MS Excel OfficeOpenXML Documents (XLSX) focus not only on content side but also on easy styling.

dependency:spreadsheet-builder-poi:2.2.1[groupId=builders.dsl]

Excel Creation

Create an interface to encapsulate Excel generation:

groovy/src/main/groovy/example/micronaut/BookExcelService.groovy

Externalize your styles configuration into a class implementing builders.dsl.spreadsheet.builder.api.Stylesheet interface to maximize code reuse.

groovy/src/main/groovy/example/micronaut/BookExcelStylesheet.groovy
package example.micronaut

import builders.dsl.spreadsheet.api.FontStyle
import builders.dsl.spreadsheet.builder.api.CanDefineStyle
import builders.dsl.spreadsheet.builder.api.Stylesheet

class BookExcelStylesheet implements Stylesheet {
    public static final String STYLE_HEADER = "header"

    @Override
    void declareStyles(CanDefineStyle stylable) {
        stylable.style(STYLE_HEADER, st -> {
            st.font(f -> f.style(FontStyle.BOLD))
        })
    }
}

Create a bean which generates the Excel file.

groovy/src/main/groovy/example/micronaut/BookExcelServiceImpl.groovy

Controller

Views

To use the Thymeleaf Java template engine to render views in a Micronaut application, add the following dependency on your classpath.

build.gradle
implementation("io.micronaut.views:micronaut-views-thymeleaf")

Create a controller:

groovy/src/main/groovy/example/micronaut/HomeController.groovy

The previous controller index method renders a simple view with a link to download the Excel file:

src/main/resources/views/index.html
<!DOCTYPE html>
<html>
<head>
    <title>Micronaut</title>
</head>
<body>
<p><a href="/excel">Excel</a></p>
</body>
</html>

Tests

Often, file transfers remain untested in many applications. In this section, you will see how easy it is to test that the file downloads and that the downloaded file contents match our expectations.

Create a test to verify the Excel file is downloaded and the content matches our expectations.

groovy/src/test/groovy/example/micronaut/DownloadExcelSpec.groovy

Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

Running the Application

To run the application, use the ./gradlew run command, which starts the application on port 8080.

Next Steps

Read more about Micronaut File Transfers support.

Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).