如何使用Power Shell在Excel中查找重复值并将行导出到另一个工作表? 我有一个包含多个行和列的Excel工作表,可以说从“ A”到“ k”。仅当一行中所有列中的值都是唯一的时,我才需要查找重复的行。并且脚本应该忽略D,E,F列,即使这些列的值相同。
该脚本还应复制所有重复的行并粘贴到新的excel文件中。还应复制标题行和重复行的源,并附加输入文件的示例图片(在这种输入情况下,输出文件也应该与输入相同,因为它也应该复制源重复行。我试过了一个代码,但是它抛出了一个错误..请仔细研究一下,然后为我提供代码的解决方案。
code:
# The Text OleDB driver is only available in PowerShell x86. Start x86
shell if using x64.
# This has to be the first check this script performs.
if ($env:Processor_Architecture -ne "x86") {
Write-Warning "Switching to x86 shell"
&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe"
"$PSCommandPath $args"; return
}
# Change to your CSV file name, must end in .csv or .tsv
$csvfile = "C:\files\A01modcsv.csv"
# Does the first row contain column names?
$firstRowColumns = $True
# What's the delimiter? Use `t for tabbed.
$csvdelimter = "`t"
$firstRowColumns = $true
$checkColumns = "A"
$datasource = Split-Path $csvfile
$tablename = (Split-Path $csvfile -leaf).Replace(".","#")
switch ($firstRowColumns) {
$true { $firstRowColumns = "Yes" }
$false { $firstRowColumns = "No" }
}
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
[void][Reflection.Assembly]::LoadWithPartialName("System.Data")
# Setup OleDB using Microsoft Text Driver.
$connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$datasource;Extended Properties='text;HDR=$firstRowColumns;FMT=Delimited($csvdelimter)';"
$conn = New-Object System.Data.OleDb.OleDbconnection
$conn.ConnectionString = $connstring
$conn.Open()
$cmd = New-Object System.Data.OleDB.OleDBCommand
$cmd.Connection = $conn
# Perform select on CSV file, then add results to a datatable using ExecuteReader
$sql = "SELECT $checkColumns, COUNT(*) as DupeCount FROM [$tablename] GROUP BY $checkColumns HAVING COUNT(*) > 1"
$cmd.CommandText = $sql
$dt = New-Object System.Data.DataTable
$dt.BeginLoadData()
$dt.Load($cmd.ExecuteReader([System.Data.CommandBehaviour]::CloseConnection))
$dt.EndLoadData()
$totaltime = [math]::Round($elapsed.Elapsed.TotalSeconds,2)
# Get Total Row Count
$cmd.CommandText = "SELECT COUNT(*) as TotalRows FROM [$tablename]"
$totalrows = $cmd.ExecuteScalar()
$conn.Close()[enter image description here][1]
# Output some stats
$dupecount = $dt.Rows.Count
Write-Host "Total Elapsed Time: $totaltime seconds. $dupecount duplicates found out of $totalrows total rows. You can access these dupes using `$dt." -ForegroundColor Green
我在“ $ dt.Load($ cmd.ExecuteReader([System.Data.CommandBehaviour] :: CloseConnection))”命令中提到的代码中出现错误。...可以帮助我解决此错误谢谢。