Skip to content

Instantly share code, notes, and snippets.

@WANGJIEKE
Last active February 24, 2020 01:11
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 WANGJIEKE/b43a2c301ac3d2477de1dee0a0d6e39b to your computer and use it in GitHub Desktop.
Save WANGJIEKE/b43a2c301ac3d2477de1dee0a0d6e39b to your computer and use it in GitHub Desktop.
Convert csv to xlsx
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
ForEach ($dir in $args) {
Write-Output "Converting all csv to xlsx inside $(Resolve-Path $dir)..."
Get-ChildItem -Path $(Resolve-Path $dir) | ForEach {
if ($_.Extension -ne ".csv") {
continue
}
Write-Output "Converting $_..."
$workbook = $excel.Workbooks.Open("$(Resolve-Path $dir)\$_")
$workbook.SaveAs("$(Resolve-Path $dir)\$($_.BaseName).xlsx", 51)
# 51 here stands for Open XML Workbook format
# https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
$workbook.Close()
}
}
$excel.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment