【问题标题】:Write to Excel range from Powershell?从 Powershell 写入 Excel 范围?
【发布时间】:2015-05-22 16:17:15
【问题描述】:

如果找到某些值,我正在寻找一种写入 Excel 范围的方法。我下面的内容不断给我以下错误:

Exception setting "Range": "Cannot find an overload for "Range" and the argument count: "2
"."
At J:\ScriptTools\PowerShellScripts\Test-Excel.ps1:13 char:18
+         $ws.Range <<<< ("A1","A10")="ControlDept"
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertySetValue

我的代码如下:

$xl = New-Object -ComObject Excel.Application
$wb=$xl.WorkBooks.Open('C:\Temp\Test.xlsx')
$SheetName = "Sheet1"
$ws=$wb.sheets.item($SheetName)
$xl.Visible=$true
$testCell = $ws.Range("A1","A10").Text
if ($testCell -eq "MG" -or "1"){
$ws.Range("A1","A10")="ControlDept"}
$wb.Save
$xl.quit()

【问题讨论】:

    标签: excel powershell powershell-2.0


    【解决方案1】:

    您应该有一个使用冒号的参数,而不是用逗号分隔的两个参数。

    $ws.Range("A1:A10").Text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-07
      • 2021-01-15
      • 1970-01-01
      • 2019-07-22
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多