[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 flashercs 于 2022-9-19 19:54 编辑
  1. Add-Type -LiteralPath .\EPPlus.dll -ErrorAction Stop
  2. $fields = @('姓名:', '语文:', '数学:', '社会:', '历史:')
  3. $keyField = '姓名:'
  4. $groupPso = Get-ChildItem -Path .\*.xlsx -Filter *.xlsx | Where-Object { -not $_.PSIsContainer } | ForEach-Object {
  5.   try {
  6.     $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.FullName
  7.     $pso = New-Object psobject
  8.     $a2d = $pack.Workbook.Worksheets[1].Cells.Value
  9.     for ($x = $a2d.GetLowerBound(0); $x -le $a2d.GetUpperBound(0); $x++) {
  10.       $propName = $null
  11.       for ($y = $a2d.GetLowerBound(1); $y -le $a2d.GetUpperBound(1); $y++) {
  12.         if ($null -eq $propName) {
  13.           if ($fields -contains $a2d[$x, $y]) {
  14.             $propName = $a2d[$x, $y]
  15.           }
  16.         } else {
  17.           if ($null -ne $a2d[$x, $y]) {
  18.             $pso | Add-Member -MemberType NoteProperty -Name $propName -Value $a2d[$x, $y]
  19.             $propName = $null
  20.           }
  21.         }
  22.       }
  23.     }
  24.     $pso
  25.   } finally {
  26.     if ($pack) {
  27.       $pack.Dispose()
  28.       $pack = $null
  29.     }
  30.   }
  31.   trap {}
  32. } | Group-Object -Property $keyField
  33. function Get-StudentScore {
  34.   param (
  35.     [string[]]$UserName
  36.   )
  37.   $groupPso | Where-Object { $null -eq $UserName -or $UserName -contains '*' -or $UserName -contains $_.Name } | ForEach-Object {
  38.     $pso = New-Object psobject -Property @{$keyField = $_.Name }
  39.     $_.Group | Measure-Object -Property ($fields -ne $keyField) -Sum | ForEach-Object {
  40.       $pso | Add-Member -MemberType NoteProperty -Name $_.Property -Value ([int]$_.Sum)
  41.     }
  42.     $pso
  43.   }
  44. }
  45. # main
  46. # search 佟玉
  47. # Get-StudentScore -UserName 佟玉 | Format-Table -AutoSize
  48. # search 张三,李四
  49. # Get-StudentScore -UserName 张三, 李四 | Format-Table -AutoSize
  50. # search 所有学生
  51. # Get-StudentScore -UserName * | Format-Table -AutoSize
  52. # search 所有学生
  53. # Get-StudentScore | Format-Table -AutoSize
  54. # 左对齐
  55. Get-StudentScore | Out-GridView -Title 学生成绩汇总 -Wait
  56. # 筛选姓名
  57. Get-StudentScore | Out-GridView -Title 学生成绩汇总_多选 -OutputMode Multiple | Out-GridView -Title 学生成绩汇总_已选择 -Wait
复制代码
微信:flashercs
QQ:49908356

TOP

回复 17# 5i365


    有没有个样本.xlsx ?网盘?
微信:flashercs
QQ:49908356

TOP

回复 19# 5i365


    16楼 已修改.
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2022-9-19 19:31 编辑

回复 24# 5i365
  1. # 左对齐
  2. Get-StudentScore | Out-GridView -Title 学生成绩汇总 -Wait
复制代码
可以在GridView中进行筛选姓名,也可以筛选成绩.


微信:flashercs
QQ:49908356

TOP

回复 23# 5i365


利用powershell_ise的 WPF GUI组件 GridView来选择对象,比winform选择要方便得多.可以多选,按Ctrl或Shift进行多选.
16楼又修改了一下.
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2022-9-19 21:08 编辑

回复 26# 5i365

winform选择
  1. Add-Type -LiteralPath .\EPPlus.dll -ErrorAction Stop
  2. $fields = @('姓名:', '语文:', '数学:', '社会:', '历史:')
  3. $keyField = '姓名:'
  4. $groupPso = Get-ChildItem -Path .\*.xlsx -Filter *.xlsx | Where-Object { -not $_.PSIsContainer } | ForEach-Object {
  5.   try {
  6.     $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.FullName
  7.     $pso = New-Object psobject
  8.     $a2d = $pack.Workbook.Worksheets[1].Cells.Value
  9.     for ($x = $a2d.GetLowerBound(0); $x -le $a2d.GetUpperBound(0); $x++) {
  10.       $propName = $null
  11.       for ($y = $a2d.GetLowerBound(1); $y -le $a2d.GetUpperBound(1); $y++) {
  12.         if ($null -eq $propName) {
  13.           if ($fields -contains $a2d[$x, $y]) {
  14.             $propName = $a2d[$x, $y]
  15.           }
  16.         } else {
  17.           if ($null -ne $a2d[$x, $y]) {
  18.             $pso | Add-Member -MemberType NoteProperty -Name $propName -Value $a2d[$x, $y]
  19.             $propName = $null
  20.           }
  21.         }
  22.       }
  23.     }
  24.     $pso
  25.   } finally {
  26.     if ($pack) {
  27.       $pack.Dispose()
  28.       $pack = $null
  29.     }
  30.   }
  31.   trap {}
  32. } | Group-Object -Property $keyField
  33. function Get-StudentScore {
  34.   param (
  35.     [string[]]$UserName
  36.   )
  37.   $groupPso | Where-Object { $null -eq $UserName -or $UserName -contains '*' -or $UserName -contains $_.Name } | ForEach-Object {
  38.     $pso = New-Object psobject -Property @{$keyField = $_.Name }
  39.     $_.Group | Measure-Object -Property ($fields -ne $keyField) -Sum | ForEach-Object {
  40.       $pso | Add-Member -MemberType NoteProperty -Name $_.Property -Value ([int]$_.Sum)
  41.     }
  42.     $pso
  43.   }
  44. }
  45. # main
  46. # search 佟玉
  47. # Get-StudentScore -UserName 佟玉 | Format-Table -AutoSize
  48. # search 张三,李四
  49. # Get-StudentScore -UserName 张三, 李四 | Format-Table -AutoSize
  50. # search 所有学生
  51. # Get-StudentScore -UserName * | Format-Table -AutoSize
  52. # search 所有学生
  53. # Get-StudentScore | Format-Table -AutoSize
  54. # 左对齐
  55. # Get-StudentScore | Out-GridView -Title 学生成绩汇总 -Wait
  56. # 筛选姓名
  57. # Get-StudentScore | Out-GridView -Title 学生成绩汇总_多选 -OutputMode Multiple | Out-GridView -Title 学生成绩汇总_已选择 -Wait
  58. Add-Type -AssemblyName System.Windows.Forms
  59. Add-Type -AssemblyName System.Drawing
  60. $form = New-Object System.Windows.Forms.Form
  61. $form.Text = '选择列表'
  62. $form.Size = New-Object System.Drawing.Size(300, 200)
  63. $form.StartPosition = 'CenterScreen'
  64. $OKButton = New-Object System.Windows.Forms.Button
  65. $OKButton.Location = New-Object System.Drawing.Point(75, 120)
  66. $OKButton.Size = New-Object System.Drawing.Size(75, 23)
  67. $OKButton.Text = 'OK'
  68. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  69. $form.AcceptButton = $OKButton
  70. $form.Controls.Add($OKButton)
  71. $CancelButton = New-Object System.Windows.Forms.Button
  72. $CancelButton.Location = New-Object System.Drawing.Point(150, 120)
  73. $CancelButton.Size = New-Object System.Drawing.Size(75, 23)
  74. $CancelButton.Text = 'Cancel'
  75. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  76. $form.CancelButton = $CancelButton
  77. $form.Controls.Add($CancelButton)
  78. $label = New-Object System.Windows.Forms.Label
  79. $label.Location = New-Object System.Drawing.Point(10, 20)
  80. $label.Size = New-Object System.Drawing.Size(280, 20)
  81. $label.Text = '选择你要查询的学生的姓名:'
  82. $form.Controls.Add($label)
  83. $listBox = New-Object System.Windows.Forms.Listbox
  84. $listBox.Location = New-Object System.Drawing.Point(10, 40)
  85. $listBox.Size = New-Object System.Drawing.Size(260, 20)
  86. $listBox.SelectionMode = 'MultiExtended'
  87. $listBox.DataSource = [System.Collections.ArrayList]@($groupPso.Name)
  88. $listBox.Height = 70
  89. $form.Controls.Add($listBox)
  90. $form.Topmost = $true
  91. $result = $form.ShowDialog()
  92. if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
  93.   $x = $listBox.SelectedItems
  94.   $x
  95. }
  96. Get-StudentScore -UserName $x | Out-GridView -Title 学生成绩汇总 -Wait
  97. ($form, $OKButton, $CancelButton, $label, $listBox).Dispose()
复制代码
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2022-9-20 19:00 编辑

出错是因为 有的.xlsx不是真正Excel文件,披着羊皮的狼而已.
  1. Add-Type -LiteralPath .\EPPlus.dll -ErrorAction Stop
  2. $fields = @('姓名:', '语文:', '数学:', '社会:', '历史:')
  3. $keyField = '姓名:'
  4. $groupPso = Get-ChildItem -Path .\*.xlsx -Filter *.xlsx | Where-Object { -not $_.PSIsContainer } | ForEach-Object {
  5.   try {
  6.     $_ | Resolve-Path -Relative | Write-Host
  7.     $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.FullName -ErrorAction Stop
  8.     $pso = New-Object psobject
  9.     $a2d = $pack.Workbook.Worksheets[1].Cells.Value
  10.     for ($x = $a2d.GetLowerBound(0); $x -le $a2d.GetUpperBound(0); $x++) {
  11.       $propName = $null
  12.       for ($y = $a2d.GetLowerBound(1); $y -le $a2d.GetUpperBound(1); $y++) {
  13.         if ($null -eq $propName) {
  14.           if ($fields -contains $a2d[$x, $y]) {
  15.             $propName = $a2d[$x, $y]
  16.           }
  17.         } else {
  18.           if ($null -ne $a2d[$x, $y]) {
  19.             $pso | Add-Member -MemberType NoteProperty -Name $propName -Value $a2d[$x, $y]
  20.             $propName = $null
  21.           }
  22.         }
  23.       }
  24.     }
  25.     $pso
  26.   } finally {
  27.     if ($pack) {
  28.       $pack.Dispose()
  29.       $pack = $null
  30.     }
  31.   }
  32.   trap { }
  33. } | Group-Object -Property $keyField
  34. function Get-StudentScore {
  35.   param (
  36.     [string[]]$UserName
  37.   )
  38.   $groupPso | Where-Object { $null -eq $UserName -or $UserName -contains '*' -or $UserName -contains $_.Name } | ForEach-Object {
  39.     $pso = New-Object psobject -Property @{$keyField = $_.Name }
  40.     $_.Group | Measure-Object -Property ($fields -ne $keyField) -Sum | ForEach-Object {
  41.       $pso | Add-Member -MemberType NoteProperty -Name $_.Property -Value $_.Sum.ToString('f2')
  42.     }
  43.     $pso
  44.   }
  45. }
  46. # main
  47. # search 佟玉
  48. # Get-StudentScore -UserName 佟玉 | Format-Table -AutoSize
  49. # search 张三,李四
  50. # Get-StudentScore -UserName 张三, 李四 | Format-Table -AutoSize
  51. # search 所有学生
  52. # Get-StudentScore -UserName * | Format-Table -AutoSize
  53. # search 所有学生
  54. # Get-StudentScore | Format-Table -AutoSize
  55. # 左对齐
  56. # Get-StudentScore | Out-GridView -Title 学生成绩汇总 -Wait
  57. # 筛选姓名
  58. # Get-StudentScore | Out-GridView -Title 学生成绩汇总_多选 -OutputMode Multiple | Out-GridView -Title 学生成绩汇总_已选择 -Wait
  59. Add-Type -AssemblyName System.Windows.Forms
  60. Add-Type -AssemblyName System.Drawing
  61. $form = New-Object System.Windows.Forms.Form
  62. $form.Text = '双击要查询的姓名'
  63. $Form.FormBorderStyle = "FixedToolWindow"
  64. $form.StartPosition = 'CenterScreen'
  65. $form.Font = New-Object System.Drawing.Font("微软雅黑", 10, [Drawing.FontStyle]::Bold)
  66. $form.ClientSize = '160, 300'
  67. $listBox = New-Object System.Windows.Forms.Listbox
  68. # $listBox.Dock = 'Fill'
  69. $listBox.Location = '0, 0'
  70. $listBox.Size = '160,280'
  71. $listBox.Anchor = 'Left,Top,Right,Bottom'
  72. $listBox.SelectionMode = 'MultiExtended'
  73. $listBox.DataSource = [System.Collections.ArrayList]@($groupPso.Name | Sort-Object)
  74. $form.Controls.Add($listBox)
  75. $OKButton = New-Object System.Windows.Forms.Button
  76. $OKButton.Location = '0,275'
  77. $OKButton.Size = '160,25'
  78. $OKButton.Text = 'OK'
  79. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  80. $OKButton.Anchor = 'Bottom'
  81. $form.AcceptButton = $OKButton
  82. $form.Controls.Add($OKButton)
  83. $form.TopMost = $true
  84. if ($groupPso.Count -gt 10) {
  85.   $form.ClientSize = '160,700'
  86. }
  87. $result = $form.ShowDialog()
  88. if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
  89.   $selectedItems = $listBox.SelectedItems
  90. }
  91. ($form, $OKButton, $listBox).Dispose()
  92. if (-not $selectedItems) {
  93.   exit
  94. }
  95. $form = New-Object System.Windows.Forms.Form
  96. $form.Text = '显示结果'
  97. $form.ClientSize = '200, 300'
  98. $Form.FormBorderStyle = "FixedToolWindow"
  99. $form.StartPosition = 'CenterScreen'
  100. $textbox1 = New-Object 'System.Windows.Forms.TextBox'
  101. $textbox1.Name = 'textbox1'
  102. $textbox1.Dock = 'Fill'
  103. $textbox1.Multiline = $True
  104. $textbox1.ScrollBars = 'Both'
  105. $textbox1.Font = New-Object System.Drawing.Font("微软雅黑", 10)
  106. $form.Controls.Add($textbox1)
  107. $textbox1.AppendText((Get-StudentScore -UserName $selectedItems | Format-List | Out-String -Width ([int]::MaxValue)))
  108. $result = $form.ShowDialog()
  109. ($form, $textbox1).Dispose()
复制代码
微信:flashercs
QQ:49908356

TOP

返回列表