Skip to content

Commit 8db5ae4

Browse files
authored
Excel and CSV Conversion
1 parent 2accd8f commit 8db5ae4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Add-Type -AssemblyName "System.Windows.Forms"
2+
3+
function Show-OpenFileDialog {
4+
$dialog = New-Object System.Windows.Forms.OpenFileDialog
5+
$dialog.Filter = "CSV Files (*.csv)|*.csv"
6+
$dialog.Title = "Select CSV File"
7+
if ($dialog.ShowDialog() -eq "OK") {
8+
return $dialog.FileName
9+
}
10+
return $null
11+
}
12+
13+
function Show-SaveFileDialog {
14+
$dialog = New-Object System.Windows.Forms.SaveFileDialog
15+
$dialog.Filter = "Excel Workbook (*.xlsx)|*.xlsx"
16+
$dialog.Title = "Save Excel File"
17+
if ($dialog.ShowDialog() -eq "OK") {
18+
return $dialog.FileName
19+
}
20+
return $null
21+
}
22+
23+
$inputFile = Show-OpenFileDialog
24+
if (-not $inputFile) {
25+
Write-Host "No CSV file selected. The script will be canceled."
26+
exit
27+
}
28+
29+
$outputFile = Show-SaveFileDialog
30+
if (-not $outputFile) {
31+
Write-Host "No Excel file selected. The script will be canceled."
32+
exit
33+
}
34+
35+
Install-Module -Name ImportExcel -Scope CurrentUser -Force -AllowClobber
36+
Import-Csv $inputFile | Export-Excel -Path $outputFile -WorksheetName "Sheet1"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Add-Type -AssemblyName "System.Windows.Forms"
2+
3+
function Show-OpenFileDialog {
4+
$dialog = New-Object System.Windows.Forms.OpenFileDialog
5+
$dialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
6+
$dialog.Title = "Select Excel File"
7+
if ($dialog.ShowDialog() -eq "OK") {
8+
return $dialog.FileName
9+
}
10+
return $null
11+
}
12+
13+
function Show-SaveFileDialog {
14+
$dialog = New-Object System.Windows.Forms.SaveFileDialog
15+
$dialog.Filter = "CSV Files (*.csv)|*.csv"
16+
$dialog.Title = "Save CSV File"
17+
if ($dialog.ShowDialog() -eq "OK") {
18+
return $dialog.FileName
19+
}
20+
return $null
21+
}
22+
23+
$inputFile = Show-OpenFileDialog
24+
if (-not $inputFile) {
25+
Write-Host "No Excel file selected. The script will be canceled."
26+
exit
27+
}
28+
29+
$outputFile = Show-SaveFileDialog
30+
if (-not $outputFile) {
31+
Write-Host "No CSV file selected. The script will be canceled."
32+
exit
33+
}
34+
35+
$excel = New-Object -ComObject Excel.Application
36+
$workbook = $excel.Workbooks.Open($inputFile)
37+
$worksheet = $workbook.Worksheets.Item(1)
38+
$worksheet.UsedRange | Export-Csv -Path $outputFile -NoTypeInformation
39+
$workbook.Close($false)
40+
$excel.Quit()
41+
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

0 commit comments

Comments
 (0)