2013-07-01 107 views
0

希望能够帮助我尝试在这里实现的目标。Powershell将数据写入excel文件

我有两个数组$ ActiveUsers并用4场$ InactiveUsers和下面的数据在每个

$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文件中,我写了一切。

有没有办法我可以同时循环两个数组,并通过higlighting我从$ 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。

回答

0

如果两个数组有相同数量的记录,你可以做这样的事情:

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

然后,您可以使用条件格式来突出显示列的值为“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 
} 
+0

不幸的是,在阵列中的记录数是不同的。我可以将Active和Inactive用户放入一个数组中,并通过保持条件来写入Excel文件,如果$ CostCntr为$ null,那么高亮度地记录其他人写入的数据? – user1411254

+0

我添加了解决不同数组长度的解决方案。是的,您可以根据特定字段的值对行进行着色,但在您的示例中,“$ CostCntr”对所有帐户都有一个值(即使没有,值也会是空字符串“”“不是'$ null')。 –

+0

:@Ansgar plese查看我试过的新代码片段 – user1411254