Excel 在powershell中分离CSV中的列的值
我有多个文件在一个单元格中包含一个人的全名。我想把这些名字分成两列——名字和姓氏 我用来分隔值的代码在我之前的脚本迭代中起作用,但现在不再起作用 我不知道错误在哪里,有人能给我建议吗Excel 在powershell中分离CSV中的列的值,excel,powershell,csv,Excel,Powershell,Csv,我有多个文件在一个单元格中包含一个人的全名。我想把这些名字分成两列——名字和姓氏 我用来分隔值的代码在我之前的脚本迭代中起作用,但现在不再起作用 我不知道错误在哪里,有人能给我建议吗 $path = 'C:\MAY2019correct' #XYZ $excelOut = Join-Path -Path $path -ChildPath 'XYZ.csv' $completedFile = Join-Path -Path $path -ChildPath 'Com
$path = 'C:\MAY2019correct'
#XYZ
$excelOut = Join-Path -Path $path -ChildPath 'XYZ.csv'
$completedFile = Join-Path -Path $path -ChildPath 'Completed-XYZ.csv'
$defaultValue = 'ABC'
$filter = '*XYZ*'
$excelFile = Get-ChildItem -Path $path -Filter $filter -File |
Select-Object -First 1
$allstaff = @()
if ($excelFile) {
$excel = New-Object -ComObject Excel.Application -Property @{Visible =
$false}
# Open the file
$workbook = $excel.Workbooks.Open($excelFile.FullName)
# Activate the first worksheet
$sheet = $workbook.Sheets.Item(1)
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the 2 row
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the 3 row
$workbook.SaveAs($excelOut,6)
# Close workbook and save changes
$workbook.Close($true)
# Quit Excel
$excel.Quit()
# clean-up Com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
$headers = 'Element Name','Surname','EmployeeNo','Amount','YTD'
# import the csv file and select all the above headers plus one that is created using a calculated property
$csv = Import-Csv -Path $excelOut -Header $headers -UseCulture | Select-Object *, @{Name = 'Paycentre'; Expression = {$defaultValue}}|
Write-Host "Creating completed csv file '$completedFile'"
$csv | Export-Csv -Path $completedFile -UseCulture -Force -
NoTypeInformation
}
else {
Write-Warning "Could not find a file using filter '$filter' in path
'$path'"
}
foreach($staff in $completedFile)
{
#Get the values from the CSV for this row
$Surname = $staff.Surname
$Surname = $Surname.Substring(0, $Surname.lastIndexOf(' '))
$Initial = $staff.Surname
$Initial = $Initial.Substring($Initial.lastIndexOf(' ') + 1 )
$Firstname = $staff.Surname
$Firstname = $Firstname.Substring($Initial.lastIndexOf(' ') + 1 )
$EmployeeNo = $staff.EmployeeNo
$NINumber = $staff.NINumber
$Amount = $staff.Amount
$Paycentre = $staff.Paycentre
$staff2 = New-Object System.Object
$staff2 | Add-Member -MemberType NoteProperty -Name "Surname" -Value $Surname
$staff2 | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $Firstname
$staff2 | Add-Member -MemberType NoteProperty -Name "EmployeeNo" -Value $EmployeeNo
$staff2 | Add-Member -MemberType NoteProperty -Name "NINumber" -Value $NINumber
$staff2 | Add-Member -MemberType NoteProperty -Name "Amount" -Value $Amount
$staff2 | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $Initial
$staff2 | Add-Member -MemberType NoteProperty -Name "Paycentre" -Value $Paycentre
#add to array
$allstaff += $staff2
}
$allstaff | Export-Csv -NoTypeInformation -Path $completedFile
我看到的第一件事是,您的
foreach
循环正在迭代$completedFile
变量,该变量似乎是一个文件路径,而不是一个集合
是否应该改为foreach($csv中的staff)
另外,在处理代码中的名称时要非常小心。很容易做出错误的假设来破坏事情。有关更多信息,请参见。NINumber来自哪里?它不在您从Excel创建的csv中,而且我看不到
元素名称
和年初至今
字段的用途。不要在csv中创建标题,之后您将为这些字段使用完全不同的名称。另外,在创建$staff2
时,您定义了属性FirstName
两次,并且迭代了文件路径$completedFile
(字符串),而不是$csv
中的实际数据。另一件事是,你似乎在随机的地方用换行符换行。还有更多。。您的$filter
不正确,它应该是*.xlsx
,因为现在您正试图打开名称中碰巧包含XYZ
的任何类型的文件。提醒:在堆栈溢出中,通常通过单击✓ 在左边。你还没有这样做,但你正在使用我的答案中的代码✓ 直到我获得了15个声誉——我第二次这样做,我会的。至于字段信息,这些是在我收到varios格式的信息后应用的标题,我得到xlsx、csv、txt。所有需要通过脚本运行的文档。我们应用标题,然后使用标题提取我们想要/可以用于主控文档的相关信息。并非所有接收的文件的所有行都具有相同的信息或事件完整信息。