Write a Grid to a CSV File

I was looking at the API the other day an saw a cool function exportDataToFile. This will allow you to export any grid object to a flat file!!! Here is a quick example where I am taking the current grid a user is working with and then saving it as a csv file.

//Get current Data Form
DataGrid curgrid = operation.getGrid()
String curruser =  operation.User.getFullName()
def GridMap = curgrid.getSourceInfo()
String GridName=GridMap.get("name")
//Filename
String ExportFileName
//Format the time date
SimpleDateFormat timestamp = new SimpleDateFormat("MM-dd-yyyy");
String FormatTime
Date date 
date = new Date()
FormatTime= timestamp.format(date)
ExportFileName = "$curruser-$GridName-pre-$FormatTime-.txt" 
operation.exportDataToFile(curgrid, ExportFileName)

This function will save the file in the Inbox/Outbox where you can download it later. The file format is very nice to work with as you can see below.

Sample Export File

I found this functionality extremely helpful when I am creating grids in groovy using the DataGridBuilder and DataGridDefinitionBuilder. Its a great way to check your work and to also save an archive of what was in memory at the time you execute a save of the grid. These files can clog up your inbox so use it as needed!

Leave a Reply