-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertCSV-ToExcel.ps1
More file actions
218 lines (164 loc) · 5.46 KB
/
ConvertCSV-ToExcel.ps1
File metadata and controls
218 lines (164 loc) · 5.46 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Function Release-Ref ($ref)
{
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
Function ConvertCSV-ToExcel
{
<#
.SYNOPSIS
Converts one or more CSV files into an excel file.
.DESCRIPTION
Converts one or more CSV files into an excel file. Each CSV file is imported into its own worksheet with the name of the
file being the name of the worksheet.
.PARAMETER inputfile
Name of the CSV file being converted
.PARAMETER output
Name of the converted excel file
.EXAMPLE
Get-ChildItem *.csv | ConvertCSV-ToExcel -output 'report.xlsx'
.EXAMPLE
ConvertCSV-ToExcel -inputfile 'file.csv' -output 'report.xlsx'
.EXAMPLE
ConvertCSV-ToExcel -inputfile @("test1.csv","test2.csv") -output 'report.xlsx'
.NOTES
Author: Boe Prox (http://poshcode.org/2123)
Contributors: Craig Buchanan (https://github.com/craibuc)
Revisions:
01-SEP-2010 - BP - created
04-APR-2016 - CB - adding -path parameter; adding Pester tests; reformatting code
#>
#Requires -version 2.0
[CmdletBinding(
SupportsShouldProcess = $True,
ConfirmImpact = 'low',
DefaultParameterSetName = 'file'
)]
Param (
[Parameter(
ValueFromPipeline=$True,
Position=0,
Mandatory=$True,
HelpMessage="Name of CSV/s to import")]
[ValidateNotNullOrEmpty()]
[array]$inputfile,
[Parameter(
ValueFromPipeline=$False,
Position=1,
Mandatory=$True,
HelpMessage="Name of excel file output")]
[ValidateNotNullOrEmpty()]
[string]$outputFile,
[Parameter(
ValueFromPipeline=$False,
Position=2,
Mandatory=$False,
HelpMessage="The directory where the XLSX should be created; default is the current directory")]
[string]$path='.',
[Parameter(
Mandatory=$False,
HelpMessage="Enables each sheet's auto-filter")]
[switch]$EnableAutoFilter,
[Parameter(
Mandatory=$False,
HelpMessage="Freezes each sheet's top row")]
[switch]$FreezePanes
# ,
# [Parameter(
# Mandatory=$False,
# HelpMessage="Bolds each sheet's top row")]
# [switch]$BoldHeader
)
Begin {
Write-Debug "$($MyInvocation.MyCommand.Name)::Begin"
#
# echo parameter values
#
Write-Debug "outputFile: $outputFile"
# remove ending slash
$path = (Get-Item $path).fullname.TrimEnd('\')
Write-Debug "path: $path"
#Configure regular expression to match full path of each file
[regex]$regex = "^\w\:\\"
#Find the number of CSVs being imported
$count = ($inputfile.count -1)
#Create Excel Com Object
$excel = new-object -com excel.application
#Disable alerts
$excel.DisplayAlerts = $False
#Show Excel application
$excel.Visible = $False
#Add workbook
$workbook = $excel.workbooks.Add()
#Remove other worksheets
$workbook.worksheets.Item(2).delete()
#After the first worksheet is removed,the next one takes its place
$workbook.worksheets.Item(2).delete()
#Define initial worksheet number
$i = 1
} # / Begin
Process {
Write-Debug "$($MyInvocation.MyCommand.Name)::Process"
ForEach ($input in $inputfile) {
Write-Verbose "Processing $input"
#If more than one file, create another worksheet for each file
If ($i -gt 1) {
$workbook.worksheets.Add() | Out-Null
}
#Use the first worksheet in the workbook (also the newest created worksheet is always 1)
$worksheet = $workbook.worksheets.Item(1)
#Add name of CSV as worksheet name
$worksheet.name = "$((GCI $input).basename)"
#Open the CSV file in Excel, must be converted into complete path if no already done
If ($regex.ismatch($input)) {
$tempcsv = $excel.Workbooks.Open($input)
}
ElseIf ($regex.ismatch("$($input.fullname)")) {
$tempcsv = $excel.Workbooks.Open("$($input.fullname)")
}
Else {
$tempcsv = $excel.Workbooks.Open("$($pwd)\$input")
}
$tempsheet = $tempcsv.Worksheets.Item(1)
#Copy contents of the CSV file
$tempSheet.UsedRange.Copy() | Out-Null
#Paste contents of CSV into existing workbook
$worksheet.Paste()
#Close temp workbook
$tempcsv.close()
#Select all used cells
$range = $worksheet.UsedRange
# freeze first row
if ( $FreezePanes ) {
$workSheet.Application.ActiveWindow.SplitRow = 1;
$workSheet.Application.ActiveWindow.FreezePanes = $true;
}
# enable top-row filters
if ( $EnableAutoFilter ) {
$workSheet.EnableAutoFilter = $true;
$workSheet.Cells.AutoFilter(1) | out-null;
}
# # Set the header-row bold
# if ( $BoldHeader ) {
# # The property 'Bold' cannot be found on this object.
# $workSheet.Range["A1", "A1"].EntireRow.Font.Bold = $true;
# }
#Autofit the columns
$range.EntireColumn.Autofit() | out-null
$i++
} # / ForEach
} # / Process
End {
Write-Debug "$($MyInvocation.MyCommand.Name)::End"
#Save spreadsheet
Write-Debug "Saving $path\$outputFile"
$workbook.saveas("$path\$outputFile")
Write-Host -Fore Green "File saved to $path\$outputFile"
#Close Excel
$excel.quit()
#Release processes for Excel
$a = Release-Ref($range)
} # / End
} # / ConvertCSV-ToExcel