希望对我在这里想要实现的目标有所帮助。
我有两个数组 $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 重复了上述内容。