| Add-Type -LiteralPath .\EPPlus.dll -ErrorAction Stop |
| $fields = @('姓名:', '语文:', '数学:', '社会:', '历史:') |
| $keyField = '姓名:' |
| $groupPso = Get-ChildItem -Path .\*.xlsx -Filter *.xlsx | Where-Object { -not $_.PSIsContainer } | ForEach-Object { |
| try { |
| $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.FullName |
| $pso = New-Object psobject |
| $a2d = $pack.Workbook.Worksheets[1].Cells.Value |
| for ($x = $a2d.GetLowerBound(0); $x -le $a2d.GetUpperBound(0); $x++) { |
| $propName = $null |
| for ($y = $a2d.GetLowerBound(1); $y -le $a2d.GetUpperBound(1); $y++) { |
| if ($null -eq $propName) { |
| if ($fields -contains $a2d[$x, $y]) { |
| $propName = $a2d[$x, $y] |
| } |
| } else { |
| if ($null -ne $a2d[$x, $y]) { |
| $pso | Add-Member -MemberType NoteProperty -Name $propName -Value $a2d[$x, $y] |
| $propName = $null |
| } |
| } |
| } |
| } |
| $pso |
| } finally { |
| if ($pack) { |
| $pack.Dispose() |
| $pack = $null |
| } |
| } |
| trap {} |
| } | Group-Object -Property $keyField |
| function Get-StudentScore { |
| param ( |
| [string[]]$UserName |
| ) |
| $groupPso | Where-Object { $null -eq $UserName -or $UserName -contains '*' -or $UserName -contains $_.Name } | ForEach-Object { |
| $pso = New-Object psobject -Property @{$keyField = $_.Name } |
| $_.Group | Measure-Object -Property ($fields -ne $keyField) -Sum | ForEach-Object { |
| $pso | Add-Member -MemberType NoteProperty -Name $_.Property -Value ([int]$_.Sum) |
| } |
| $pso |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Add-Type -AssemblyName System.Windows.Forms |
| Add-Type -AssemblyName System.Drawing |
| |
| $form = New-Object System.Windows.Forms.Form |
| $form.Text = '选择列表' |
| $form.Size = New-Object System.Drawing.Size(300, 200) |
| $form.StartPosition = 'CenterScreen' |
| |
| $OKButton = New-Object System.Windows.Forms.Button |
| $OKButton.Location = New-Object System.Drawing.Point(75, 120) |
| $OKButton.Size = New-Object System.Drawing.Size(75, 23) |
| $OKButton.Text = 'OK' |
| $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK |
| $form.AcceptButton = $OKButton |
| $form.Controls.Add($OKButton) |
| |
| $CancelButton = New-Object System.Windows.Forms.Button |
| $CancelButton.Location = New-Object System.Drawing.Point(150, 120) |
| $CancelButton.Size = New-Object System.Drawing.Size(75, 23) |
| $CancelButton.Text = 'Cancel' |
| $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel |
| $form.CancelButton = $CancelButton |
| $form.Controls.Add($CancelButton) |
| |
| $label = New-Object System.Windows.Forms.Label |
| $label.Location = New-Object System.Drawing.Point(10, 20) |
| $label.Size = New-Object System.Drawing.Size(280, 20) |
| $label.Text = '选择你要查询的学生的姓名:' |
| $form.Controls.Add($label) |
| |
| $listBox = New-Object System.Windows.Forms.Listbox |
| $listBox.Location = New-Object System.Drawing.Point(10, 40) |
| $listBox.Size = New-Object System.Drawing.Size(260, 20) |
| |
| $listBox.SelectionMode = 'MultiExtended' |
| |
| $listBox.DataSource = [System.Collections.ArrayList]@($groupPso.Name) |
| |
| $listBox.Height = 70 |
| $form.Controls.Add($listBox) |
| $form.Topmost = $true |
| |
| $result = $form.ShowDialog() |
| |
| if ($result -eq [System.Windows.Forms.DialogResult]::OK) { |
| $x = $listBox.SelectedItems |
| $x |
| } |
| |
| Get-StudentScore -UserName $x | Out-GridView -Title 学生成绩汇总 -Wait |
| ($form, $OKButton, $CancelButton, $label, $listBox).Dispose()COPY |