Grails Plugin: Excel import plugin
Posted By : Shakil Pathan | 29-Sep-2014
Hi,
In this blog I am going to explain you how to use grails-import plugin.
This plugin is used for either bootstrapping date, or when you want to allow your users to enter some data using Excel spreadsheets, or CSV. You can also export into Excel using very similar configuration to importing.
The core of the plugin is a service class ExcelImportService, which contains a number of useful methods for dealing with Excel, which you should dependency inject. There are also some helper classes to help report potential problems.
_org.grails.plugins.excelimport.ExcelImportService_
Dependency:
compile ":excel-import:1.0.0"
Custom repositories:
mavenRepo "http://repo.grails.org/grails/libs-releases/"
mavenRepo "http://m2repo.spockframework.org/ext/"
The plugin is designed to parse data out of spreadsheets into format that can then be used to create objects. For example, if you have data like this:
A B
FirstName LastName
Shakil Pathan
Mohit Virmani
Raman Joshi
Here is the code for parsing data from Form-multipart-file:
import org.apache.poi.ss.usermodel.Workbook
import org.springframework.web.multipart.commons.CommonsMultipartFile
import org.apache.poi.ss.usermodel.WorkbookFactory;
//Controller
Map CONFIG_BOOK_COLUMN_MAP = [
sheet:'Sheet1',
startRow: 1,
columnMap: [
//Col, Map-Key
'A':'firstName',
'B':'lastName'
]
]
CommonsMultipartFile file = params.excelFile
Workbook workbook = WorkbookFactory.create(file.inputStream)
List userList = excelImportService.columns(workbook, CONFIG_BOOK_COLUMN_MAP)
//The List is the list of maps. After that you can access any entry easily using groovy code like:
String firstName = userList.get(0).firstName
Hope it helps!!!
Thanks
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shakil Pathan
Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .