0

我正在创建一个powershell脚本,我想从excel表中读取一个值(VALUE1)(如果需要,我可以将其转换为XML),将它分配给一个变量($PLACEHOLDER),运行脚本的其余部分,然后循环回到开头,但不是读取原始值(VALUE1)我希望它读取它下面的值(VALUE2)并用VALUE2覆盖$PLACEHOLDER,然后重新运行脚本直到它返回一个空白值,然后我希望它停止。我对 powershell 非常陌生,它与 excel/xml 交互,所以任何帮助将不胜感激。(我是自学的,所以对参数不太了解)

Sample in Terrible Psuedo:
#Initial placeholder value here
$RowNumber = 0
#Start of the loop here, add one to previous value
$RowNumber +1
#Call the value in Column (1), Row ($RowNumber), and assign it to $RowValue
?????? = $RowValue
#Execute the command involving the data value
ECHO "C:/test/temporary/$RowValue"
#Goto the start of the loop.

如果您能这么好,请您快速解释一下您使用的功能(参数,发生了什么等)

编辑:如果它可以检测并跳过空白行,那就太棒了。

EDIT3:Ansgar 的代码

$xl = New-Object -COM 'Excel.Application'
$xl.Visible = $true   # set to $false for production

$wb = $xl.Workbooks.Open("C:\Documents and Settings\xe474109\Desktop\EXCEL FILES\testbook2.xlsx")
$ws = $wb.Sheets.Item(1)

$row = $ws.UsedRange.Rows.Count
while ( $ws.Cells.Item($row, 1).Value -ne $null ) {
  $PLACEHOLDER = $ws.Cells.Item($row, 1).Value

  #
  # do stuff with $PLACEHOLDER here
  #(I wanted to test this by just printing the $PLACEHOLDER value
  $PLACEHOLDER

  $row++
} 

$wb.Close()
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
4

2 回答 2

2

你有安装Excel吗?如果是这样,您可以像这样处理 Excel 电子表格:

$xl = New-Object -COM 'Excel.Application'
$xl.Visible = $true   # set to $false for production

$wb = $xl.Workbooks.Open('C:\path\to\your.xlsx')
$ws = $wb.Sheets.Item(1)

$row = $ws.UsedRange.Row
while ( $ws.Cells.Item($row, 1).Value -ne $null ) {
  $PLACEHOLDER = $ws.Cells.Item($row, 1).Value

  #
  # do stuff with $PLACEHOLDER here
  #

  $row++
} 

$wb.Close()
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
于 2013-07-29T17:10:50.640 回答
0
cls

$csv = Import-csv -Path 'C:\test\csvStuff.csv'

foreach ($rec in $csv) {
  if ($rec.nameofyourcolumn -ne '') {
    & "c:\test\temporary\$($rec.nameofyourcolumn)"
  }
}
于 2013-07-29T16:58:48.890 回答