使用powershell更新excel工作表中的单元格

iibxawm4  于 2022-12-26  发布在  Shell
关注(0)|答案(3)|浏览(243)

我需要在电子表格的某行中搜索某个单词,并使用不同的值更新同一行中的另一个单元格。例如,我有这样的数据。我需要从下面的电子表格中搜索人员“Smith”,并将该行的“状态”列的值从“已启用”更新为“已禁用”。

"Region","Zone","Customer","Process","Status"
"TEST","East","Smith","HR","Disabled"
"TEST","East","Allen","Finance","Enabled"
"TEST","East","Jake","Payroll","Enabled"

我试过regex和其他一些函数,但我不能让他们工作。
谢谢。

lo8azlld

lo8azlld1#

将Excel与PowerShell配合使用非常简单:

Add-Type -AssemblyName Microsoft.Office.Interop.Excel

$excelFile = 'C:\test\testsheet.xlsx' 

$searchFor            = 'Smith'

$excel                = New-Object -ComObject Excel.Application
$excel.Visible        = $true
$excel.ScreenUpdating = $true

$workbook  = $excel.Workbooks.Open( $excelFile ,$null, $false )

$ws        = $workbook.WorkSheets.item(1) 

[void]$ws.Activate()

$searchRange  = $ws.UsedRange

$searchResult = $searchRange.Find( $searchFor, [System.Type]::Missing, [System.Type]::Missing, 
                                               [Microsoft.Office.Interop.Excel.XlLookAt]::xlWhole, 
                                               [Microsoft.Office.Interop.Excel.XlSearchOrder]::xlByColumns, 
                                               [Microsoft.Office.Interop.Excel.XlSearchDirection]::xlNext )

while( $searchResult ) {

    $row = $searchResult.Row
    $col = $searchResult.Column

    $ws.Cells( $row, $col + 2 ).Value2 = 'Disabled'
    $searchResult = $searchRange.FindNext( $searchResult )

    if( $searchResult -and $searchResult.Row -le $row ) {
        break
    }
}

[void]$workbook.Save()
[void]$workbook.Close()
[void]$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
68bkxrlz

68bkxrlz2#

我用下面的脚本让它工作。

$TGTSERVER = "testservwc01"

$name = "ORATDLLSTR"
        $input = Invoke-Command -ComputerName "$TGTSERVER" -ScriptBlock {Import-Csv 'C:\test.csv'}
        $value = "Disabled"
        $Output = foreach ($i in $input) { 
            if ($i.Process_Instance -match "$name") {$i.Status = "$value"} $i } 

        $OutArray = $Output | Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName
        $OutArray | Invoke-Command -ComputerName "$TGTSERVER" -ScriptBlock {Export-Csv 'C:\test.csv' -NoTypeInformation}

        if ( $LastExitCode -ge 1) 
              {
                    Write-Warning -Message "$Computer : Disable Step failed"
                    exit 1
        }

但是,即使脚本在远程服务器上使用正确的值更新了csv文件,脚本也会失败,退出代码为1。

mwyxok5s

mwyxok5s3#

我已经找到了满足我的需求的解决方案...使用PowerShell不是主题中提到的问题...但整体模块有很多选项,这可能有助于使用PowerShell修改Excel文件
https://www.powershellgallery.com/packages/PSWriteExcel/0.1.15https://github.com/EvotecIT/PSWriteExcel/blob/master/Examples/Run-Example-FindReplace.ps1

Install-Module -Name PSWriteExcel

Import-Module PSWriteExcel -Force
$FilePath = "D:\Excel_test.xlsx"
$FilePathOutput = "D:\Excel_test1.xlsx"
Find-ExcelDocumentText -FilePath $FilePath -Find 'evotec' -Replace -ReplaceWith 'somethingelse' -FilePathTarget $FilePathOutput -OpenWorkBook -Regex -Suppress $true

相关问题