Created
February 22, 2012 13:51
-
-
Save kdabir/1885246 to your computer and use it in GitHub Desktop.
Reading MS Excel file in groovy
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
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