This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathExcel.psm1
124 lines (92 loc) · 3.7 KB
/
Excel.psm1
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
#requires -Version 3
Set-StrictMode -Version 3
Function Convert-CsvToExcel() {
<#
.SYNOPSIS
Converts a CSV file to an Excel file.
.DESCRIPTION
Converts a CSV file to an Excel file.
.PARAMETER CsvPath
Path to the CSV file.
.PARAMETER ExcelPath
Path to the Excel file.
.EXAMPLE
Convert-CsvToExcel -CsvPath '.\Secure-Host-Baseline\Hardware\Template.csv' -ExcelPath '.\Secure-Host-Baseline\Hardware\Template.xlsx'
#>
[CmdletBinding()]
[OutputType([void])]
Param(
[Parameter(Mandatory=$true, HelpMessage='Path to CSV file')]
[ValidateNotNullOrEmpty()]
[string]$CsvPath,
[Parameter(Mandatory=$true, HelpMessage='Path to Excel file')]
[ValidateNotNullOrEmpty()]
[string]$ExcelPath
)
$excelFiles = @(Get-ChildItem @($env:ProgramFiles,${env:ProgramFiles(x86)},$env:ProgramW6432) -Filter 'excel.exe' -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $_.PsIsContainer -eq $false } | Get-Unique)
if($excelFiles.Count -eq 0) {
throw 'Excel not installed'
}
$CsvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CsvPath)
if (-not(Test-Path -Path $CsvPath -PathType Leaf)) {
throw '$CsvPath does not exist'
}
$ExcelPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($ExcelPath)
if (Test-Path -Path $ExcelPath -PathType Leaf) {
Remove-Item -Path $ExcelPath -Force
}
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
#[System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-US'
# 51 = xlOpenXMLWorkbook
# 60 = xlOpenDocumentSpreadsheet
# 61 = xlOpenXMLStrictWorkbook
$excel.Workbooks.Open($CsvPath).SaveAs($ExcelPath,51)
$excel.Quit()
}
Function Convert-ExcelToCsv() {
<#
.SYNOPSIS
Converts an Excel file to a CSV file.
.DESCRIPTION
Converts an Excel file to a CSV file.
.PARAMETER CsvPath
Path to the CSV file.
.PARAMETER ExcelPath
Path to the Excel file.
.EXAMPLE
Convert-ExcelToCsv -CsvPath '.\Secure-Host-Baseline\Hardware\Template.csv' -ExcelPath '.\Secure-Host-Baseline\Hardware\Template.xlsx'
#>
[CmdletBinding()]
[OutputType([void])]
Param(
[Parameter(Mandatory=$true, HelpMessage='Path to CSV file')]
[ValidateNotNullOrEmpty()]
[string]$CsvPath,
[Parameter(Mandatory=$true, HelpMessage='Path to Excel file')]
[ValidateNotNullOrEmpty()]
[string]$ExcelPath
)
$excelFiles = @(Get-ChildItem @($env:ProgramFiles,${env:ProgramFiles(x86)},$env:ProgramW6432) -Filter 'excel.exe' -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $_.PsIsContainer -eq $false } | Get-Unique)
if($excelFiles.Count -eq 0) {
throw 'Excel not installed'
}
$ExcelPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($ExcelPath)
if (-not(Test-Path -Path $ExcelPath -PathType Leaf)) {
throw '$ExcelPath does not exist'
}
$CsvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CsvPath)
if (Test-Path -Path $CsvPath -PathType Leaf) {
Remove-Item -Path $CsvPath -Force
}
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
#[System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-US'
# 6 = xlCSV
# 23 = xlCSVWindows
# 24 = xlCSVMSDOS
$excel.Workbooks.Open($ExcelPath).SaveAs($CsvPath,6)
$excel.Quit()
}