| Add-Type -LiteralPath .\EPPlus.dll -ErrorAction Stop |
| $fields = @('姓名:', '语文:', '数学:', '社会:', '历史:') |
| $keyField = '姓名:' |
| $groupPso = Get-ChildItem -Path .\*.xlsx -Filter *.xlsx | Where-Object { -not $_.PSIsContainer } | ForEach-Object { |
| try { |
| $_ | Resolve-Path -Relative | Write-Host |
| $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.FullName -ErrorAction Stop |
| $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 $_.Sum.ToString('f2') |
| } |
| $pso |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Add-Type -AssemblyName System.Windows.Forms |
| Add-Type -AssemblyName System.Drawing |
| $form = New-Object System.Windows.Forms.Form |
| $form.Text = '双击要查询的姓名' |
| $Form.FormBorderStyle = "FixedToolWindow" |
| $form.StartPosition = 'CenterScreen' |
| $form.Font = New-Object System.Drawing.Font("微软雅黑", 10, [Drawing.FontStyle]::Bold) |
| $form.ClientSize = '160, 300' |
| |
| $listBox = New-Object System.Windows.Forms.Listbox |
| |
| $listBox.Location = '0, 0' |
| $listBox.Size = '160,280' |
| $listBox.Anchor = 'Left,Top,Right,Bottom' |
| $listBox.SelectionMode = 'MultiExtended' |
| $listBox.DataSource = [System.Collections.ArrayList]@($groupPso.Name | Sort-Object) |
| $form.Controls.Add($listBox) |
| |
| $OKButton = New-Object System.Windows.Forms.Button |
| $OKButton.Location = '0,275' |
| $OKButton.Size = '160,25' |
| $OKButton.Text = 'OK' |
| $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK |
| $OKButton.Anchor = 'Bottom' |
| $form.AcceptButton = $OKButton |
| $form.Controls.Add($OKButton) |
| |
| $form.TopMost = $true |
| if ($groupPso.Count -gt 10) { |
| $form.ClientSize = '160,700' |
| } |
| $result = $form.ShowDialog() |
| |
| if ($result -eq [System.Windows.Forms.DialogResult]::OK) { |
| $selectedItems = $listBox.SelectedItems |
| } |
| |
| ($form, $OKButton, $listBox).Dispose() |
| if (-not $selectedItems) { |
| exit |
| } |
| |
| $form = New-Object System.Windows.Forms.Form |
| $form.Text = '显示结果' |
| $form.ClientSize = '200, 300' |
| |
| $Form.FormBorderStyle = "FixedToolWindow" |
| $form.StartPosition = 'CenterScreen' |
| |
| $textbox1 = New-Object 'System.Windows.Forms.TextBox' |
| $textbox1.Name = 'textbox1' |
| $textbox1.Dock = 'Fill' |
| $textbox1.Multiline = $True |
| $textbox1.ScrollBars = 'Both' |
| $textbox1.Font = New-Object System.Drawing.Font("微软雅黑", 10) |
| $form.Controls.Add($textbox1) |
| $textbox1.AppendText((Get-StudentScore -UserName $selectedItems | Format-List | Out-String -Width ([int]::MaxValue))) |
| $result = $form.ShowDialog() |
| ($form, $textbox1).Dispose()COPY |