文件不存在(但存在)并且为 "AddPicture" 和参数计数找到多个不明确的重载:“2”

File does not exist (but it's there) and Multiple ambiguous overloads found for "AddPicture" and the argument count: "2"

我正在尝试使用 powershell 5 和 VSCode.

将图像添加到 excel 工作表

我收到这些错误:

C:\CC_SnapViews\EndToEnd_view\path is correct\file.bmp does not exist (but it's there)

Multiple ambiguous overloads found for "AddPicture" and the argument count: "2"

当我搜索 Internet 时,搜索中没有出现此错误。我正在关注这些示例:

addPicture

addPicture github

这是我的代码:

$xlsx = $result | Export-Excel -Path $outFilePath -WorksheetName $errCode -Autosize -AutoFilter -FreezeTopRow -BoldTopRow  -PassThru # -ClearSheet can't ClearSheet every time or it clears previous data  ###left off
  $ws = $xlsx.Workbook.Worksheets[$errCode]
  for ($row = 2 ;( $row -le $tempRowCount ); $row++)
  {
     #Write-Host $($ws.Dimension.Rows)
     #Write-Host $($row)
     $ws.Row($row).Height
     $ws.Row($row).Height = 150
     $ws.Row($row)[3]
     $result.GetValue($row) #$ws.Row($row)[3]
     $pictureName=$result[$row].PictureID
     $pictureNamePath=$result[$row].ImageFileName
     #place the image in spreadsheet
     #https://github.com/dfinke/ImportExcel/issues/1041  https://github.com/dfinke/ImportExcel/issues/993
     $drawingName = "$($pictureName)_Col3_$($row)" #Name_ColumnIndex_RowIndex   
     #Write-Host $image
     Write-Host $drawingName
     ####
     if($null -ne $pictureNamePath)
     {
        $image = Get-Image -imageFileName $pictureNamePath ###error pictureNamePath does not exist but it does
     }
     else 
     {
        Write-Host "Did not find an image file for $pictureName in $pictureNamePath"
     }
     $picture = $ws.Drawings.AddPicture($pictureNamePath,$image)  ###error message here
   }

知道为什么 powershell 认为图像文件不存在吗?

更新:

我在 foreach 中为行添加了一些调试:

for ($row = 2 ;( $row -le $tempRowCount ); $row++) { #写主机 $($ws.Dimension.Rows) #Write-Host $($行) $ws.Row($row).高度 $ws.Row($row).Height = 150 $ws.Row($行)[3] $result.GetValue($row) #打印整行信息 $pictureName=$result[$row].PictureID $pictureNamePath=$result[$row].ImageFileName 如果(测试路径 $pictureNamePath) { Write-Host "$($pictureNamePath) exists" ##prints ...文件名路径...存在(看起来不错) } Write-Host "pic path = $pictureNamePath" ##prints pic path = ..文件名路径...(看起来不错) ...

更新2: 添加获取图像功能:

Function Get-Image{
        [cmdletbinding()]
          Param ([string]$imageFileName) 
          Process
          {
            #find image file name to look for
            
                if($imageFileName.Exists) #if($imageFile2.Exists)
                {
                    [System.Drawing.Image] $image = [System.Drawing.Image]::FromFile($imageFileName) #may not need this step
                    #need to figure out which is correct if there's multiple images
                    return $image
                }
                else {
                    Write-Host "$($imageFileName) does not exist"
                    return $null
                }
          } #end Process
        }# End of Function 

我更改了我的函数以使用 Test-Path,它现在设置图像。

    Function Get-Image{
    [cmdletbinding()]
      Param ([string]$imageFileName) 
      Process
      {
        #find image file name to look for
        
            if(Test-Path $imageFileName) ###instead of Exists
            {
                [System.Drawing.Image] $image = [System.Drawing.Image]::FromFile($imageFileName) #may not need this step
                #need to figure out which is correct if there's multiple images
                return $image
            }
            else {
                Write-Host "$($imageFileName) does not exist"
                return $null
            }
      } #end Process
    }# End of Function