-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSave-ExcelasCSV.ps1
59 lines (51 loc) · 2.03 KB
/
Save-ExcelasCSV.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# also see:
# http://www.codeproject.com/Articles/451744/Extract-worksheets-from-Excel-into-separate-files
# http://social.technet.microsoft.com/Forums/scriptcenter/en-US/4de7ed7a-f1b3-4e35-98b1-ef1f7f2ee7b2/powershell-export-from-excel-to-csv-having-delimiter?forum=ITCG
function Save-ExcelasCSV {
param (
[string[]]$files = $(Throw 'No files provided.'),
[string]$OutFolder,
[switch]$Overwrite
)
BEGIN {
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
$xl = New-Object -ComObject Excel.Application
$xl.DisplayAlerts = $false
$xl.Visible = $false
}
PROCESS {
foreach ($file in $files) {
$file = Get-Item $file | ? {$_.Extension -match '^\.xlsx?$'}
if (!$file) {continue}
$wb = $xl.Workbooks.Open($file.FullName)
if ($OutFolder) {
$CSVfilename = Join-Path $OutFolder ($file.BaseName + '.csv')
} else {
$CSVfilename = $file.DirectoryName + '\' + $file.BaseName + '.csv'
}
if (!$Overwrite -and (Test-Path $CSVfilename)) {
$num = 1
$folder = Split-Path $CSVfilename
$base = (Split-Path $CSVfilename -Leaf).Substring(0, (Split-Path $CSVfilename -Leaf).LastIndexOf('.'))
$ext = $CSVfilename.Substring($CSVfilename.LastIndexOf('.'))
while (Test-Path $CSVfilename) {
$CSVfilename = Join-Path $folder $($base + "-$num" + $ext)
$num += 1
}
$wb.SaveAs($CSVfilename, 6) # 6 -> csv
} else {
$wb.SaveAs($CSVfilename, 6) # 6 -> csv
}
$wb.Close($True)
$CSVfilename
}
}
END {
$xl.Quit()
$null = $wb, $xl | % {try{ Release-Ref $_ }catch{}}
}
}