繁体   English   中英

Powershell无法找到Excel并打开Excel文件

[英]Powershell Unable to find Excel and open Excel File

我正在尝试复制 Powershell,重命名而不是编辑 excel 文件。 它按预期复制并重命名文件,但是当我使用 excel 打开文件时,它无法找到该文件。 见附件代码。

感谢您的帮助。

#Export Textbox outputs
$S0 = $textBox1.Text
$jobname = $textBox2.Text
$contractor = $TextBox3.Text

#combine textbox outputs
$folder = "$S0" + "_" + "$jobname" + "_" + "$contractor"
$subsubfolder = ".\"+"$folder" + "\Dir"
$takeoffname = "$s0" + "_takeoff.xlsx"

#Excel
$xl = New-Object -ComObject excel.application
Start-Sleep -Seconds 5
$xl.Visible = $true
Start-Sleep -Seconds 5
$wb = $xl.Workbooks.Open("$subsubfolder\$takeoffname")
$data = $wb.Worksheets.Item("Storm")
$Data.Cells.Item(1,2) = "$jobname"
$data.Cells.Item(1,7) = "$S0"
$wb.Save()
$xl.Quit()

新的更新代码 - 添加了加入路径并打破了创建文件夹循环。 抱歉,如果添加文件夹的要求会产生额外的问题。

$S0     = $TextBox1.Text
    $jobname    = $TextBox2.Text
    $contractor = $TextBox3.Text

$folder = ' {0}_{1}_{2}' -f $S0, $jobname, $Contractor
$file  = '{0}_takeoff.xlsx' -f $S0
$PILname = 'PIL_{0}.xlsx' -f $S0
Write-host $folder

New-Item -ItemType Directory "./$folder"
foreach($line in Get-Content $Filenames)
{
New-Item $folder\$line -ItemType Directory
}
$subfolder   = '{0}\1 - Estimating Original Quote Material' -f $folder
$subsubfolder = Join-Path -Path $PWD - ChildPath $Subfolder
$filePath = Join-Path -Path $PWD -ChildPath (Join-Path -Path $subsubfolderfolder -ChildPath $file)
$PILpath = Join-Path -Path $PWD -ChildPath (Join-Path -Path $subsubfolderfolder -ChildPath $PILname)
Write-host $filePath
Write-host $subsubfolder
pause
#Copy Files
Copy-Item '.\_master_takeoff.xlsx' "$subsubfolder\_master_takeoff.xlsx"
Copy-Item '.\PIL_S0XXXXX .xlsx' $subsubfolder
#Rename Files
Rename-Item -Path "$subsubfolder\_master_takeoff.xlsx" -newname $takeoffname
Rename-Item -Path "$subsubfolder\PIL_S0XXXXX .xlsx" -newname $PILpath

$xl = New-Object -ComObject excel.application
Start-Sleep -Seconds 5
$xl.Visible = $true
Start-Sleep -Seconds 5
$wb = $xl.Workbooks.Open("$subsubfolder\$takeoffname")
$data = $wb.Worksheets.Item("Storm")
$Data.Cells.Item(1,2) = "$jobname"
$data.Cells.Item(1,7) = "$S0"
$wb.Save()
$xl.Quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

通过从文本框中取出字符串并将其组合到带有字符串连接的文件路径,您最终会得到一个不存在的路径。 话虽如此,错误来自在路径中使用.\\

Powershell 可能知道它在哪里,但 Excel 不知道在哪里查找文件。 Excel 有自己的默认路径,通常指向Documents文件夹,当给出相对路径时,它将使用它。

始终使用现有的绝对文件路径在外部应用程序中打开内容。

最好使用这样的东西

#Export Textbox outputs
$prefix     = $TextBox1.Text
$jobname    = $TextBox2.Text
$contractor = $TextBox3.Text

#combine textbox outputs to form the directory (I like using the -f format operator)
$file     = '{0}_takeoff.xlsx' -f $prefix
$folder   = '{0}_(1}_{2}\Dir' -f $prefix, $jobname, $contractor
$filePath = Join-Path -Path $PWD -ChildPath (Join-Path -Path $folder -ChildPath $file)

# test if the file can be founc
if (Test-Path $filePath -PathType Leaf) {
    $xl = New-Object -ComObject excel.application
    $xl.Visible = $true
    $wb = $xl.Workbooks.Open($filePath)
    $data = $wb.Worksheets.Item("Storm")
    $Data.Cells.Item(1,2) = $jobname
    $data.Cells.Item(1,7) = $prefix
    $wb.Save()
    $xl.Quit()

    # important: clean-up COM objects after use
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}
else {
    Write-Warning "File '$filePath' not found"
}

除了使用$PWD (打印工作目录),您还可以使用Get-Location ,这实际上是相同的


由于我不知道为什么您更新后的代码会创建子文件夹,因此我将在此处省略。

请查看-f Format 运算符的工作原理,因为现在您做错了。

此外,为了不再混淆 PowerShell 的工作目录和 Excel 的默认路径,请首先在代码中定义完整的根路径。 下面我$workingDir使用了一个名为$workingDir的变量。

Copy-Item可以同时复制和重命名。

# let's forget about the 'Set-Location' and use absolute paths from the beginning
$workingDir = '\\Server\Share\Folder'  # set this to the real path

# Export Textbox outputs
$S0         = $TextBox1.Text
$jobname    = $TextBox2.Text
$contractor = $TextBox3.Text

# combine textbox outputs to form the directory (I like using the -f format operator)
$PILname  = 'PIL_{0}.xlsx' -f $S0
$file     = '{0}_takeoff.xlsx' -f $S0
$folder   = '{0}_(1}_{2}\1 - Estimating Original Quote Material' -f $S0, $jobname, $contractor
$folderPath = Join-Path -Path $workingDir -ChildPath $folder      # --> Full absolute path to the working folder
$filePath   = Join-Path -Path $folderPath -ChildPath $file        # --> Full absolute path to the file

Write-host $filePath
Write-host $folderPath

#Copy and rename master Files
$masterFile = Join-Path -Path $workingDir -ChildPath '_master_takeoff.xlsx'
$pilFile    = Join-Path -Path $workingDir -ChildPath 'PIL_S0XXXXX.xlsx'
Copy-Item -Path $masterFile -Destination $filePath
Copy-Item -Path $pilFile -Destination (Join-Path -Path $folderPath -ChildPath $PILname)

############################
#Write to new take off file
############################

# Call excel and open file
$xl = New-Object -ComObject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Open($filePath)
$data = $wb.Worksheets.Item("Storm")
$Data.Cells.Item(1,2) = $jobname
$data.Cells.Item(1,7) = $S0
$wb.Save()
$xl.Quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

代码看起来一切正常,但您使用的是相对路径。

如果这样做,则需要在打开 excel 之前更改工作目录。

例如:设置位置 C:\\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM