[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
回复 29# 5i365

发现一个问题, 经常有下面的错误提示, 但是也能显示结果,
不知道是不是获取数据时出错了, 这个挺重要, 要是数据错了, 成绩汇总肯定也错了, 学生就哭晕了

ERROR: New-Object : Exception calling ".ctor" with "1" argument(s): "The file is not an valid Package file. If the file is encrypted, please supply the password in th
ERROR: e constructor."
EP3.ps1 (7, 11): ERROR: At Line: 7 char: 11
ERROR: + ...     $pack = New-Object OfficeOpenXml.ExcelPackage -ArgumentList $_.Fu ...
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: ( [New-Object], MethodInvocationException
ERROR:     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
ERROR:
ERROR: Cannot index into a null array.
EP3.ps1 (9, 3): ERROR: At Line: 9 char: 3
ERROR: +         $a2d = $pack.Workbook.Worksheets[1].Cells.Value
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : NullArray
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

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

本帖最后由 5i365 于 2022-9-20 19:28 编辑

回复 32# flashercs

多谢大侠
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

回复 2# idwma


    大师,我在很多地方抄袭你这回答的第一句,非常好用
  1. #@&cls&powershell "type '%~0'|out-string|iex"&pause&exit
复制代码
请问,第二个字符,那个@在这里起什么作用,删掉似乎也能运行,只是不知道有什么影响,多谢解惑

TOP

回复 34# wanghan519


不是不是当不起
也是借鉴前辈们的
还真不知道有没得啥子影响
1

评分人数

TOP

比较一下
  1. #@&echo "ok"
  2. #&echo "ok"
复制代码

TOP

返回列表