Home Ask Login Register

Developers Planet

Your answer is one click away!

Manjunath February 2016

How to read column value using grails excel import plugin?

I am using Grails excel import plugin to import an excel file.

      static Map propertyConfigurationMap = [
          name:([expectedType: ExcelImportService.PROPERTY_TYPE_STRING, defaultValue:null]),
          age:([expectedType: ExcelImportService.PROPERTY_TYPE_INT, defaultValue:0])]

      static Map CONFIG_BOOK_COLUMN_MAP = [
          startRow: 1,
          columnMap:  [
           //Col, Map-Key

I am able to retrieve the array list by using the code snippet:

def usersList = excelImportService.columns(workbook, CONFIG_USER_COLUMN_MAP)

which results in

[[name: Mark, age: 25], [name: Jhon, age: 46], [name: Anil, age: 62], [name: Steve, age: 32]]

And also I'm able to read each record say [name: Mark, age: 25] by using usersList.get(0)

How do I read the each column value? I know I can read something like this

String[] row = usersList.get(0)
for (String s : row)
    println s

I wonder is there any thing that plugin supports so that I can read column value directly rather manipulating it to get the desired result.


Emmanuel Rosa February 2016

Your usersList is basically a List<Map<String, Object>> (list of maps). You can read a column using the name you gave it in the config. In your example, you named column A name and column B age. So using your iteration example as a basis, you can read each column like this:

Map row = usersList.get(0)

for(Map.Entry entry : row) {
    println entry.value

Groovy makes this easier to do with Object.each(Closure):

row.each { key, value -> 
    println value 

If you want to read a specific column value, here are a few ways to do it:

println row.name // One
println row['name'] // Two
println row.getAt('name') // Three

Hint: These all end up calling row.getAt('name')

Post Status

Asked in February 2016
Viewed 3,302 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life