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: 3
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_HOMEconfigured 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.
-
Download and unzip the source
or
-
Clone the Git repository:
git clone https://github.com/grails-guides/grails-file-download-excel.git
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 --features=events,geb2,hibernate5
we are using geb2 feature which includes Geb 2 and Gradle WebDriver Binaries plugin. Geb 2 requires JDK 1.8 or greater.
|
3.1 Books
Create Book POGO:
link:../../snippets/src/main/groovy/example/grails/Book.groovy[role=include]
Create a sample service which fetches several books:
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.
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.
link:../../snippets/src/main/groovy/example/grails/BookExcelStylesheet.groovy[role=include]
Create a service which generates the excel file.
link:../../snippets/grails-app/services/example/grails/BookExcelService.groovy[role=include]
3.4 Controller
Create a controller:
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.
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.
We told Grails CLI to use geb2 feature when we created the app in Writing the App section.
Grails geb2 feature includes the necessary Geb dependencies:
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.
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:
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.
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.
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 Running the app
To run the application use the ./gradlew bootRun command which will start the application on port 8080.
5 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.
-
Slack - real-time conversation with the Apache Grails community.
-
dev@grails.apache.org">Developer mailing list - design discussions and contributor coordination.
-
users@grails.apache.org">Users mailing list - end-user questions and answers.
-
Issue tracker on GitHub - file a bug or feature request against the framework.
For Grails plugins, see the matching project on the apache org or the plugin’s own GitHub repository.