Skip to content

Instantly share code, notes, and snippets.

@kdabir
Created February 22, 2012 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdabir/1885246 to your computer and use it in GitHub Desktop.
Save kdabir/1885246 to your computer and use it in GitHub Desktop.
Reading MS Excel file in groovy
def file_path = "C:/path/to/excel/file.xlsx"
def read_only = true
// Unfortunately this will work only on windows because of this driver :(
def connection_url= """jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=$file_path;READONLY=$read_only"""
if (!new File(file_path).exists()) return "invalid file"
def sql = groovy.sql.Sql.newInstance(connection_url, '', '')
sql.eachRow ('SELECT * FROM [sheet_name$]') {
// remember,
// 1. $ should follow sheet name
// 2. if using GString, escape dollar "sheet_name\$"
println it
}
sql.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment