【问题标题】:Cant Delete All rows in Excel through Powershell无法通过Powershell删除Excel中的所有行
【发布时间】:2017-06-14 07:14:11
【问题描述】:

我在使用 PowerShell 删除 excel 中的某些行时遇到了一些麻烦,目前我已经删除了某些列,它们的标题包含某个单词(由于某种原因,它不能完全按我的预期工作,但我允许它工作)现在我必须删除excel文件中的所有行超过某个点,我尝试了很多方法,但它们似乎都不起作用,几乎所有被注释掉的东西都是以前的尝试,但没有奏效,如果有人有任何可能的提示或解决方案,我真的很喜欢帮助。 下面是我的代码。

#list of columns wanted
$want_list = ("CustomerCompanyName","InvoiceNumber","ChargeStartDate","ChargeEndDate",
"SubscriptionId","ServiceName","ServiceType","ResourceName","Region","ConsumedQuantity",
"IncludedQuantity","OverageQuantity","ListPrice","PretaxCharges","ChargeType","DomainName")
$bad_list = ("PartnerId","PartnerName","PartnerBillableAccountId","MpnId","SubscriptionName",
"SubscriptionDescription","OrderId","ResourceGuid","Sku","DetailLineItemId","TaxAmount",
"PostTaxTotal","Currency","PretaxEffectiveRate","PostTaxEffectiveRate","CustomerId")

 $file = Read-Host "Enter Billing Info Location: example C:\Users\tyarn\Desktop\Billing_Folder\Billing_Info.csv"
    #create new object
    $excel = New-Object -ComObject Excel.Application
    $excel.DisplayAlerts = $false
    $excel.visible = $false

    #open csv file
    $workbook = $excel.WorkBooks.open($file)
    $sheet = $workbook.Sheets.Item(1)

    #get num columns
    $num_cols = $sheet.UsedRange.Columns.Count



    #for loop with deleting entire column if name of column isnt in list
    For($i=1;$i -lt $num_cols;$i++){
        if($sheet.Cells.Item(2,$i).Text -notin $want_list){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
        if($sheet.Cells.Item(2,$i).Text -in $bad_list){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
        if($sheet.Cells.Item(2,$i).Text -eq "OrderId"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
        if($sheet.Cells.Item(2,$i).Text -eq "Currency"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
        if($sheet.Cells.Item(2,$i).Text -eq "PretaxEffectiveRate"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
        if($sheet.Cells.Item(2,$i).Text -eq "PartnerId"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
    }
    ##Regular for loop going through and deleteing every row after a certain point doesnt work
    #get number of rows
    $num_rows = $sheet.UsedRange.Rows.Count
    #doesnt work yet
    #loop deleting unneeded rows
    #$truth_value = 0
    #For($i = 1; $i -lt $num_rows+1;$i++){
    #if($sheet.Cells.Item($i,1).Text -eq ""){
    #$truth_value = 1
    #For($j = $i;$j-lt $num_rows; $j++){
    #[void]$sheet.Cells.Item($j,1).EntireRow.Delete()}
    #}
    #if($truth_value -eq 1){break}
    #} 

    #new try
    ##find row to start delete on
    $row_start = $null
    For($j=1;$j -lt $num_rows;$j++){
        if($sheet.Cells.Item($j,1).Text -eq "Daily Usage"){
            $row_start = $j
            $start_delete = $row_start


        }
    }
    ##deleting row 56 everytime cause it moves up one when its deleted? doesnt work dont think
    #For($row_start;$row_start -lt $num_rows; $row_start++){
       # [void]$sheet.Cells.Item($row_start,1).EntireRow.Delete()
        #$start_delete +=1
    #}

    #long solution
    #For($i=1;$i -lt $num_cols;$i++){
       # For($j=$start_delete;$j -lt $num_rows;$j++){
          #  [void]$sheet.Cells.Item($i,$j).clear()
       # }
   # }
    ##GET RID OF
    #save
    $file_name = Read-Host "What would you like the new files name to be"
     #must create a folder for this 
    $workbook.SaveAs("C:\Users\tyarn\desktop\New_Billing\$file_name")

    #close and release com
    $workbook.Close($true)
    $excel.Quit()
    [void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
    Remove-Variable excel

我只包括了“坏列表”,因为某些列没有被删除,并且出于某种原因,我只包括了循环的另一个删除列,因为仍然有一些没有被删除,但这不是主要问题,主要问题是无论我尝试循环删除某个点之后的所有行,它都不会起作用,有时它会从所有行中删除一列或半列,但不会全部删除。

【问题讨论】:

    标签: powershell powershell-3.0 powershell-remoting


    【解决方案1】:

    你的一次尝试就快到了。使用以下代码,当 $start_delete 已知时,$start_delete 行中的所有行都将被删除:

    For($j = $start_delete; $j -le $num_rows; $j++){ [void]$sheet.Cells.Item($start_delete,1).EntireRow.Delete() }

    重要的是使用-le,这也会删除最后一行。使用$start_delete 作为行的指示,将确保每次都删除同一行,因为文件正在缩小。

    【讨论】:

    • 我试过了,从逻辑上讲,它应该像我的其他几次尝试一样工作,但事实并非如此,我认为问题不在于代码,而在于实际的 CSV 文件,因为我尝试了一种不同的方法并尝试导出某些列,并且对行没有做任何事情,它甚至没有导出列,只有非常奇怪的标题,因为我直接从有信誉的来源复制了代码
    猜你喜欢
    • 1970-01-01
    • 2020-03-12
    • 2019-03-30
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多