-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadexcel.ps1
150 lines (138 loc) · 4.07 KB
/
readexcel.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
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
$SetExcel = {
echo $FileList[$i]
$script:workbook = $excel.Workbooks.Open($filepath+"\"+$FileList[$i])
}
$OpenSheet = {
try{
$script:worksheet = $workbook.Sheets($sheetname)
} catch{
echo "!!! No exist the default sheet. Read first sheet."
echo "-------------------"
$script:worksheet = $excel.worksheets.Item(1)
}
}
$ReadExcel = {
##<< test code. Please change this part.
$name=$worksheet.Range("C2").Text
echo "Name: $name"
$profile=$worksheet.Range("C3").Text
echo "profile: $profile"
## << test code.
}
$CloseBook = {
Set-Variable -Name workbook -Scope script
Set-Variable -Name worksheet -Scope script
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) > $null
$workbook.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) > $null
}
$SelectFile = {
Clear-Host
echo "--- Excel file list. ---"
$k=0
$FileList | foreach{
echo "$k : $_"
$k++
}
echo ""
$Filenum = Read-Host "Please select number of file to read."
& $CloseBook
try {
$script:workbook = $excel.Workbooks.Open($filepath+"\"+$FileList[$Filenum])
} catch {
echo "!!! No exist this file. Reread previous file."
echo "-------------------"
$script:workbook = $excel.Workbooks.Open($filepath+"\"+$FileList[$i])
}
Write-Host "Please any key..." -NoNewLine
[Console]::ReadKey($true) > $null
Clear-host
}
$GetSheet ={
Clear-Host
$sheetTotal=$workbook.worksheets.count
echo "Total Sheet: $sheetTotal"
$sheetList= @($workbook.worksheets | ForEach-Object {
$_.Name })
echo ""
echo "--- Sheet list ---"
$j=0
$Sheetlist | foreach{
echo "$j : $_"
$j++
}
echo ""
$sheetnum = Read-Host "Please select sheet number to read"
$script:sheetname= $sheetlist[$sheetnum]
Write-Host "Please any key..." -NoNewLine
[Console]::ReadKey($true) > $null
Clear-host
}
$SetSheetName = {
echo ""
echo "--- Set sheetname ---"
echo "Change default sheetname to read."
echo "now default name: $script:SheetName"
$script:SheetName= Read-Host "Please input sheetname."
echo "new default sheetname : $SheetName"
echo ""
}
#-------------------------------------------------------------------
$filepath=(pwd).Path
$FileList=@(dir -NAME | Select-String "^.*\.xlsx$" |sort)
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $False
$excel.DisplayAlerts = $False
$script:sheetname= Read-Host "Please input default sheetname"
$i=0
& $SetExcel
& $OpenSheet
& $ReadExcel
while ($i -ge 0 -and $i -le ($FileList.Length-1 )) {
# Keyboad input
Write-Host "Next file: n, Before file: b, Default SheetName: d, Sheet Select: s, File Select: f :" -NoNewLine
$keyInfo = [Console]::ReadKey($true)
switch ($keyInfo.Key){
"n" { #nextfile
Write-Host "next"
& $Closebook
Clear-Host
$i++
if ($i -ge ($FileList.Length) ) {break}
& $SetExcel
& $OpenSheet
& $ReadExcel
}
"b" { #before file
write-host "before"
& $Closebook
Clear-Host
$i--
if ($i -le -1 ) {break}
& $SetExcel
& $OpenSheet
& $ReadExcel
}
"d" { #default sheet name setting
write-host "Default Sheet Name"
& $SetSheetName
}
"s" { #sheet select
write-host "Sheet Select"
& $GetSheet
& $OpenSheet
& $ReadExcel
}
"f" { # file select
write-host "File List"
& $SelectFile
& $OpenSheet
& $ReadExcel
}
}
}
$excel.Quit()
echo "Finish read."
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) > $null
Write-Host "Please any key to finish..." -NoNewLine
[Console]::ReadKey($true) > $null