-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVtoXLSX.ps1
More file actions
32 lines (25 loc) · 1022 Bytes
/
CSVtoXLSX.ps1
File metadata and controls
32 lines (25 loc) · 1022 Bytes
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
Function CSVtoXLSX {
$writelocation = "C:\"
$csvname = = "outfile.csv"
# Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
# Build the QueryTables.Add command and reformat the data
$TxtConnector = ("TEXT;" + ($writelocation + $csvname))
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $global:delimiter
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
# Execute & delete the import query
$query.Refresh()
$query.Delete()
# Save & close the Workbook as XLSX.
$Workbook.SaveAs($writelocation+'UsersList.xlsx')
$excel.Quit()
Remove-Item ($writelocation + $csvname) #clean up the Csv
write-host "Your file is located in $writelocation as UsersList.xlsx."
}
CSVtoXLSX