Created
September 1, 2016 21:14
-
-
Save ahjashish/8d485f6dde68991265f0bdd922e573b6 to your computer and use it in GitHub Desktop.
Groovy Script to read xls data and fire get requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grapes([ | |
@Grab('org.apache.poi:poi:3.10.1'), | |
@Grab('org.apache.poi:poi-ooxml:3.10.1')]) | |
import groovy.io.FileType | |
import groovy.json.JsonOutput | |
import groovy.json.JsonSlurper | |
import groovyx.net.http.RESTClient | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook | |
import java.nio.file.Paths | |
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC | |
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING | |
def header = [] | |
def values = [] | |
def list = [] | |
def client = new RESTClient('http://localhost:8080') | |
def dir = new File("resources") | |
dir.eachFileRecurse(FileType.FILES) { file -> | |
if (file.getName().endsWith('xlsx')) { | |
list << file | |
println(file.path) | |
} | |
} | |
def createJsonData = { | |
Paths.get(it.path).withInputStream { input -> | |
def workbook = new XSSFWorkbook(input) | |
def sheet = workbook.getSheetAt(0) | |
for (cell in sheet.getRow(0).cellIterator()) { | |
header << cell.stringCellValue | |
} | |
def headerFlag = true | |
for (row in sheet.rowIterator()) { | |
if (headerFlag) { | |
headerFlag = false | |
continue | |
} | |
def rowData = [:] | |
for (cell in row.cellIterator()) { | |
def value = '' | |
switch (cell.cellType) { | |
case CELL_TYPE_STRING: | |
value = cell.stringCellValue | |
break | |
case CELL_TYPE_NUMERIC: | |
value = cell.numericCellValue | |
break | |
default: | |
value = '' | |
} | |
rowData << ["${header[cell.columnIndex]}": value] | |
} | |
values << rowData | |
} | |
} | |
Paths.get(it.path.replaceAll('.xlsx', '') + '.json').withWriter { jsonWriter -> | |
jsonWriter.write JsonOutput.prettyPrint(JsonOutput.toJson(values)) | |
} | |
} | |
list.forEach(createJsonData) | |
dir.eachFileRecurse(FileType.FILES) { file -> | |
if (file.getName().endsWith('json')) { | |
def slurper = new JsonSlurper().parse(file) | |
slurper.each { | |
println(client.get( | |
path: '/greeting', | |
query: [name: it.CurrencyName] | |
).getData()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment