且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell将数据写入excel文件

更新时间:2023-11-03 08:49:22

If both arrays have the same number of records you could do something like this:

for ($i = 0, $i -lt $activeUsers.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $activeUsers[$i].split('|')
  $row = 2 * $i + 2
  $ActiveWorksheet.Cells.Item($i, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i, 4) = $ApprMgr
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $inactiveUsers[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+1, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+1, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+1, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+1, 4) = $ApprMgr
  $ActiveWorksheet.Rows.Item($i+1).EntireRow.Interior.ColorIndex = 3
}

If the length of both arrays differs, you could do something like this:

if ($activeUsers.Length -gt $inactiveUsers.Length) {
  $larger  = $activeUsers
  $smaller = $inactiveUsers
} else {
  $larger  = $inactiveUsers
  $smaller = $activeUsers
}
for ($i = 0, $i -lt $smaller.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $smaller[$i].split('|')
  ...
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $larger[$i].split('|')
  ...
}
for ($i = $smaller.Length, $i -lt $larger.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $larger[$i].split('|')
  ...
}

A better solution, however, might be to introduce an additional column, indicating the state of the account:

$ActiveWorksheet.Cells.Item(1,5) = "Inactive"
...
for ($i = 0, $i -lt $activeUsers.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $activeUsers[$i].split('|')
  $row = 2 * $i + 2
  $ActiveWorksheet.Cells.Item($i, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i, 4) = $ApprMgr
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $inactiveUsers[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+1, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+1, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+1, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+1, 4) = $ApprMgr
  $ActiveWorksheet.Cells.Item($i+1, 5) = "x"
}

Then you could use a conditional format to highlight rows where the value of the 5th column is "x" (the formula for that format would be =(INDIRECT("E"&ROW()))="x").

If you're going to introduce the abovementioned additional column, you could even simplify the script like this:

$users =  $activeUsers | % { $_ + "|" }
$users += $inactiveUsers | % { $_ + "|x" }
...
$ActiveWorksheet.Cells.Item(1,5) = "Inactive"
...
for ($i = 0, $i -lt $users.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr,$inactive) = $users[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+2, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+2, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+2, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+2, 4) = $ApprMgr
  $ActiveWorksheet.Cells.Item($i+2, 5) = $inactive
}