我需要找到excel文件中存在的所有重复行,并使用powershell脚本将这些重复行复制到新的excel文件中。有人可以帮我吗?
我已经尝试了一些代码,但是无法正常工作!下面的代码仅检查两列是否重复,但我需要检查所有列;(
$dxl= New-Object -ComObject Excel.application
$dwb= $dxl.Workbooks.Add()
$dws= $dwb.Worksheets.Item(1)
$z=1
foreach ($file in (Get-ChildItem “C:\sample\” -include *.xls, *.xlsx -exclude ResultSheet.xlsx -recurse ))
{
$Col=1
$objxl = New-Object -ComObject Excel.Application
$objwb = $objxl.Workbooks.Open($file.fullname)
$objws = $objwb.Worksheets.Item(1)
$rowMax = ($objws.UsedRange.Rows).count
for($i=2; $i -le $rowMax ;$i++)
{
for($j=3; $j -le $rowMax ;$j++)
{
if($objws.Cells.Item($i,$Col).value() -eq $objws.Cells.Item($j,$Col).value() -and $objws.Cells.Item($i,$Col+1).value() -eq $objws.Cells.Item($j,$Col+1).value())
{
$dup=$objws.Cells.Item(1).range($i,$col).EntireRow
$dup.copy | out-null
$dws.Paste($dup)
}
$z++
}
}
$dxl.visible = $True
$range = $dws.usedRange
$range.EntireColumn.AutoFit() | out-null
}