Skip to content

Instantly share code, notes, and snippets.

@bungard
Created January 22, 2015 15:33
Show Gist options
  • Save bungard/17f66de0624fa77b6d3c to your computer and use it in GitHub Desktop.
Save bungard/17f66de0624fa77b6d3c to your computer and use it in GitHub Desktop.
function parseExcel{
param(
[string] $strFileName = $(throw "Please specify an excel file to parse."),
[string] $strSheetName = 'Sheet1$',
[string] $callListId = $(throw "Please specify a callListId.")
)
$strProvider = "Provider=Microsoft.Jet.OLEDB.4.0"
$strDataSource = "Data Source = $strFileName"
$strExtend = "Extended Properties=Excel 8.0"
$strQuery = "Select * from [$strSheetName]"
$objConn = New-Object System.Data.OleDb.OleDbConnection("$strProvider;$strDataSource;$strExtend")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
$objConn.open()
$DataReader = $sqlCommand.ExecuteReader()
$count = 0
if($DataReader.HasRows)
{
$DataReader.read() #Dump header row
While($DataReader.read())
{
$emplid = $DataReader[0].Tostring()
$desc = $DataReader[2].ToString()
# Do Something
$count++
}
}
$dataReader.close()
$objConn.close()
}
parseExcel -strFileName "some-file.xls" -callListId "18"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment