Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.