【问题标题】:Excel.Application: Microsoft Excel cannot access the file '[<filename>]' There are several possible reasons:Excel.Application:Microsoft Excel 无法访问文件 '[<filename>]' 有几个可能的原因:
【发布时间】:2019-05-01 09:24:24
【问题描述】:

我有一个可以工作的 PowerShell 脚本,它可以帮助我对多个服务器运行多个查询,并将每个输出保存在不同的 CSV 中,然后将它们合并到一个 Excel 文件中。

$Servers = get-content -Path "Servers.txt"                                                                  
$DatabaseName ="master"                                                                                                                             
#$credential = Get-Credential #Prompt for user credentials 
$secpasswd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("sa", $secpasswd)
$QueriesFolder = "Queries\"                                                                                                                         
$ResultFolder = "Results\"                                                                                                                          

ForEach($Server in $Servers)                                                                                                                        
    {
    $DateTime = (Get-Date).tostring("yyyy-MM-dd")                                                                                                   
    ForEach ($filename in get-childitem -path $QueriesFolder -filter "*.sql" | sort-object {if (($i = $_.BaseName -as [int])) {$i} else {$_}} )     
        {
        $oresults = invoke-sqlcmd -ServerInstance $Server -Database $DatabaseName -Credential $credential -InputFile $filename.fullname             
        write-host "Executing $filename on $Server"                                                                                                 
        $BaseNameOnly = Get-Item $filename.fullname | Select-Object -ExpandProperty BaseName
        $oresults | export-csv $ResultFolder$BaseNameOnly.csv -NoTypeInformation -Force
        } 

    $All_CSVs = get-childitem -path $ResultFolder -filter "*.csv" | sort-object {if (($i = $_.BaseName -as [int])) {$i} else {$_}} 
    $Count_CSVs = $All_CSVs.Count
    Write-Host "Detected the following CSV files: ($Count_CSVs)"
    Write-Host " "$All_CSVs.Name"`n"
    $ExcelApp = New-Object -ComObject Excel.Application
    $ExcelApp.SheetsInNewWorkbook = $All_CSVs.Count
    $output = "C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx"
    if (Test-Path $output) 
        {
        Remove-Item $output
        Write-Host Removing: $output because it exists already
        }
    $xlsx = $ExcelApp.Workbooks.Add()
    for($i=1;$i -le $Count_CSVs;$i++) 
        {
        $worksheet = $xlsx.Worksheets.Item($i)
        $worksheet.Name = $All_CSVs[$i-1].Name
        $file = (Import-Csv $All_CSVs[$i-1].FullName)
        $file | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip
        $worksheet.Cells.Item(1).PasteSpecial()|out-null
        }

    $xlsx.SaveAs($output)
    Write-Host Creating: $output 
    $ExcelApp.Quit() 
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsx) | Out-Null; 
    Write-Host "Closing all worksheet"
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelApp) | Out-Null; 
    Write-Host "Closing Excel"
    [System.GC]::Collect(); 
    [System.GC]::WaitForPendingFinalizers()
    Remove-Item "$ResultFolder\*" -Include *.csv
    Write-Host "Cleaning all *.csv"
    Start-Sleep -Seconds 3
    }

为了使这个脚本更便携,我希望将其中提到的所有路径存储到一个变量中,然后连接起来。 但是一旦我改变:

$output = "C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx" 

进入:

$output = $ResultFolder + $Server + " $DateTime.xlsx"

事情变得糟糕,我收到错误:

Microsoft Excel cannot access the file 'C:\Users\FrancescoM\Documents\Results\0DC80000'.
There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
At C:\Users\FrancescoM\Desktop\CSV\QueryLauncher.ps1:50 char:2
+     $xlsx.SaveAs($output)
+     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

我不明白,我想我把事情串联起来了。

我也关注了StackOverflow post,并在添加 "C:\Windows\SysWOW64\config\systemprofile\desktop" 后重新启动了我的计算机,但问题并未解决。

可变路径如何在 Excel 中搞砸?

【问题讨论】:

    标签: excel powershell scripting windows-scripting excel.application


    【解决方案1】:

    因为您没有在$ResultFolder 变量中定义完整路径,所以它将使用当前工作目录进行扩展。

    只要看看你想要的路径:

    "C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx"
    

    以及使用部分 $ResultFolder 变量的结果路径:

    C:\Users\FrancescoM\Documents\Results\0DC80000
    

    由于您希望将输出文件放在桌面上的文件夹中,请将$output 设置为

    $output = Join-Path $([Environment]::GetFolderPath("Desktop")) "CSV\Results\$Server $DateTime.xlsx"
    


    编辑

    从您的上一条评论中,我了解到您希望输出位于名为“Results”的子文件夹中,该子文件夹位于脚本本身所在的文件夹内。 在这种情况下,这样做:

    # get the folder this script is running from
    $ScriptFolder = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path $MyInvocation.MyCommand.Path }
    # the following paths are relative to the path this script is in
    $QueriesFolder = Join-Path -Path $ScriptFolder -ChildPath 'Queries'
    $ResultFolder  = Join-Path -Path $ScriptFolder -ChildPath 'Results'
    # make sure the 'Results' folder exists; create if not
    if (!(Test-Path -Path $ResultFolder -PathType Container)) {
        New-Item -Path $ResultFolder -ItemType Directory | Out-Null
    }
    

    然后,当需要保存 xlsx 文件时,使用以下命令创建完整路径和文件名:

    $output = Join-Path -Path $ResultFolder -ChildPath "$Server $DateTime.xlsx"
    $xlsx.SaveAs($output)
    

    附注我建议使用Join-Path cmdlet 来组合文件路径或使用[System.IO.Path]::Combine(),而不是像使用此行一样将路径连接在一起:$oresults | export-csv $ResultFolder$BaseNameOnly.csv。如果您忘记在第一个路径部分加上反斜杠,则使用后者可能会导致无法预料的路径名。

    P.S.2 Excel 在工具->选项->常规->默认文件位置中设置了自己的默认输出路径,并且不知道脚本的相对路径。这就是为什么您应该使用完整路径和文件名进行保存的原因。

    【讨论】:

    • 谢谢@Theo,但我需要一些更便携的东西,因为 CSV 文件夹有时会在这里,有时在那里。我需要指向"Results\" 文件夹的东西,该文件夹位于可执行文件.ps1 的同一目录中
    • @FrancescoMantovani 我已将输出文件夹的答案编辑为运行此脚本的路径的子文件夹。我想这同样适用于$QueriesFolder
    • 你是专业人士!我还修复了export-csv $ResultFolder$BaseNameOnly.csv。你今天教我一些东西! StackOverflow 万岁,谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多