繁体   English   中英

Powershell将数据写入Excel文件

[英]Powershell writing data into excel file

希望对我在这里想要实现的目标有所帮助。

我有两个数组$ ActiveUsers和$ InactiveUsers有4个字段,每个字段中都有以下数据

$ActiveUsers = fahe532|Allyson Sullivan|34563|Cain,Ashley J
               pers274|Martha Walsh|53732|Mckenny,Josh
               fgh8458|Kayla Benson|95463|Trot,Cook K
               ndcf846|Sam Huff|56737|Mcghee,John
               vdfg456|Mark Jones |67843|Hart,Josh W
               fasf345|Amber Sean|24678|John,Kneel G

$InavtiveUsers = fasd034|Colin Hart|35473|David, Casper
                 aertp56|Andy Matthews|56849|Debby, Gould K
                 ahshb86|Mark Michael|46848|Ty, Justin H
                 gkr5057|Josh Meeker|56848|Ashley, Rhonda R
                 irrk106|Tom Mortin|64838|Becks, Alan
                 eqer348|Nathan Willis|469894|Baker ,John T

现在,我试图从$ ActiveUsers中提取每一行并将其添加到我正在创建的Excel文件中。 我对$ InactiveUsers中的每一行都做了相同的操作,并将它们添加到新的Excel文件中

需求略有变化,而不是将两个数组添加到两个Excel文件中,而我不得不将所有内容都放在一个excel文件中,而且我还需要突出显示(用某种颜色)我从$ InactiveUsers获取的行我正在编写所有内容的一个excel文件。

有没有办法我可以同时遍历两个数组,并通过突出显示从$ InactiveUsers获得的行,将行写入一个excel文件? 以下是我到目前为止写的

$ExcelObject = new-Object -comobject Excel.Application  
$ExcelObject.visible = $false 
$ExcelObject.DisplayAlerts =$false
$date= get-date -format "yyyyMMddHHss"
$strPath1="o:\UserCert\Active_Users_$date.xlsx" 
if (Test-Path $strPath1) {  
  #Open the document  
$ActiveWorkbook = $ExcelObject.WorkBooks.Open($strPath1)  
$ActiveWorksheet = $ActiveWorkbook.Worksheets.Item(1)  
} else {  
# Create Excel file  
$ActiveWorkbook = $ExcelObject.Workbooks.Add()  
$ActiveWorksheet = $ActiveWorkbook.Worksheets.Item(1)  

#Add Headers to excel file
$ActiveWorksheet.Cells.Item(1,1) = "User_Id"  
$ActiveWorksheet.cells.item(1,2) = "User_Name" 
$ActiveWorksheet.cells.item(1,3) = "CostCenter"
$ActiveWorksheet.cells.item(1,4) = "Approving Manager"
$format = $ActiveWorksheet.UsedRange
$format.Interior.ColorIndex = 19
$format.Font.ColorIndex = 11
$format.Font.Bold = "True"
} 
#Loop through the Array and add data into the excel file created.
foreach ($line in $Activeusers){
     ($user_id,$user_name,$Costcntr,$ApprMgr) = $line.split('|')
      $introw = $ActiveWorksheet.UsedRange.Rows.Count + 1  
      $ActiveWorksheet.cells.item($introw, 1) = $user_id  
      $ActiveWorksheet.cells.item($introw, 2) = $user_name
      $ActiveWorksheet.cells.item($introw, 3) = $Costcntr
      $ActiveWorksheet.cells.item($introw, 4) = $ApprMgr 
      $ActiveWorksheet.UsedRange.EntireColumn.AutoFit();
      }

我对$ InactiveUsers重复了上述操作。

如果两个数组的记录数相同,则可以执行以下操作:

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 ($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('|')
  ...
}

但是,更好的解决方案可能是增加一个列,以指示帐户状态:

$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"
}

然后,你可以使用一个条件格式以突出的行,其中 5列的值是“x”的(式为该格式将是=(INDIRECT("E"&ROW()))="x" )。

如果您要介绍上述其他专栏,甚至可以简化如下脚本:

$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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM