Skip to content

Instantly share code, notes, and snippets.

@jeffpatton1971
Last active October 4, 2017 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffpatton1971/423f8ecdb1c448133bf3334db6e4fc62 to your computer and use it in GitHub Desktop.
Save jeffpatton1971/423f8ecdb1c448133bf3334db6e4fc62 to your computer and use it in GitHub Desktop.
param
(
$strFileName = "C:scriptsfile.xlsx",
$strSheetName = 'Out'
)
if (!(($strSheetName.Substring($strSheetName.Length-1,1)) -eq '$'))
{
$strSheetName = "$($strSheetName)`$"
}
$strProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
$strDataSource = "Data Source = $strFileName"
$strExtend = "Extended Properties=`"Excel 12.0 Xml;HDR=YES;IMEX=1`""
$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()
While($DataReader.read())
{
$data = $DataReader[0].Tostring()
write-host $data
}
$dataReader.close()
$objConn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment