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

 

 

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..