Skip to content

Instantly share code, notes, and snippets.

/OpenCSV.ps1 Secret

Created July 26, 2013 07:41
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 anonymous/81160f837827ad44e35f to your computer and use it in GitHub Desktop.
Save anonymous/81160f837827ad44e35f to your computer and use it in GitHub Desktop.
$csv = Get-Item $args[0]
$excel = New-Object -ComObject excel.application
$excel.visible = $true
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.worksheets.Item(1)
$worksheet.Name = $csv.basename
$arrFormats = @()
$columnCount = 255 #$worksheet.Cells.Columns.Count
for ($i=1;$i -le $columnCount; $i++) {$arrFormats += 2}
$TxtConnector = ("TEXT;" + $csv.fullname)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = $arrFormats
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
Remove-Item variable:arrFormats
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment