Show Navigation

Download an Excel file in Grails App

Learn how to download an excel file with Grails and Spreadsheet Builder library.

Authors: Sergio del Amo

Grails Version: 4

1 Training

Apache Grails Training

Apache Grails is now part of the Apache Software Foundation. The community-maintained training catalog is being migrated; in the meantime see the Learning page for current resources, recorded talks, and links to other community-supplied training material.

2 Getting Started

In this guide, we are going to demonstrate Grails file transfer capabilities by creating an app which downloads an excel file with a list of books.

2.1 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 11 or greater installed with JAVA_HOME configured appropriately

2.2 Solution

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

or

Then, cd into the complete folder which you will find in the root project of the downloaded/cloned project.

3 Writing the App

grails create-app example.grails.complete

3.1 Books

Create Book POGO:

src/main/groovy/example/grails/Book.groovy
link:../../snippets/src/main/groovy/example/grails/Book.groovy[role=include]

Create a sample service which fetches several books:

grails-app/services/example/grails/BookService.groovy
link:../../snippets/grails-app/services/example/grails/BookService.groovy[role=include]

3.2 Spreadsheet Builder

Add a dependency to Spreadsheet builder

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

build.gradle
dependencies {
    ...
    ..
    .
link:../../snippets/build.gradle[role=include]
}

3.3 Excel Creation

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

src/main/groovy/example/grails/BookExcelStylesheet.groovy
link:../../snippets/src/main/groovy/example/grails/BookExcelStylesheet.groovy[role=include]

Create a service which generates the excel file.

grails-app/services/example/grails/BookExcelService.groovy
link:../../snippets/grails-app/services/example/grails/BookExcelService.groovy[role=include]

3.4 Controller

Create a controller:

grails-app/controllers/example/grails/ExcelController.groovy
link:../../snippets/grails-app/controllers/example/grails/ExcelController.groovy[role=include]
1 Implement grails.core.support.GrailsConfigurationAware to configure mime types and encoding configuration.
2 A controller method can access the response object which is an instace of Servlet API’s HttpServletResponse class
3 Set Content-Disposition to indicate the file should be downloaded.
4 Set the download Content-Type.
5 Write excel file to output stream, flush and close it.

By default, a Grails application created from scratch contains a link to every controller registered in the application. We will test that clicking that links downloads an Excel file.

home

3.5 Tests

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

We use also, Geb; a browser automation solution.

By default, a Grails includes the necessary Geb dependencies:

build.gradle
link:../../snippets/build.gradle[role=include]

Grails geb2 feature generates a src/integration-test/resources/GebConfig.groovy file to configure different environments for Geb. Modify it to configure some chrome options to control the download path.

src/integration-test/resources/GebConfig.groovy
link:../../snippets/src/integration-test/resources/GebConfig.groovy[role=include]
1 Disable confirmation popups
2 Configure the download folder

Geb uses the Page concept pattern - The Page Object Pattern gives us a common sense way to model content in a reusable and maintainable way. Create a Geb Page to encapsulate the Excel link:

src/integration-test/groovy/example/grails/HomePage.groovy
link:../../snippets/src/integration-test/groovy/example/grails/HomePage.groovy[role=include]

geb2 feature installs also webdriver-binaries Gradle plugin; a plugin that downloads and caches WebDriver binaries specific to the OS the build runs on.

build.gradle
buildscript {
    repositories {
...
..
    }
    dependencies {
link:../../snippets/build.gradle[role=include]
    }
}

link:../../snippets/build.gradle[role=include]

dependencies {
...
..
.
}

link:../../snippets/build.gradle[role=include]

link:../../snippets/build.gradle[role=include]
1 Pass system property geb.env to the tests.
2 Pass system property download.folder to the tests.

Create a test which verifies the Excel file is downloaded and the content matches our expectations.

src/integration-test/groovy/example/grails/DownloadExcelSpec.groovy
link:../../snippets/src/integration-test/groovy/example/grails/DownloadExcelSpec.groovy[role=include]

To run the tests:

$ ./gradlew -Dgeb.env=chrome -Ddownload.folder=/Users/sdelamo/Downloads integrationTest
$ open build/reports/tests/test/index.html

4 Help with Grails

Help with Apache Grails

Apache Grails is supported by an active community of contributors and the Apache Software Foundation. If you need help working through a guide, want to discuss the framework, or have run into something that looks like a bug, the channels below are the right place to start.

For Grails plugins, see the matching project on the apache org or the plugin’s own GitHub repository.