【问题标题】:How to use powershell to select range and dump that to csv file如何使用 powershell 选择范围并将其转储到 csv 文件
【发布时间】:2021-11-22 04:50:21
【问题描述】:

实际上,这是问题的一个版本: How to use powershell to select and copy columns and rows in which data is present in new workbook.

目标是从多个 Excel 工作簿中获取某些列并将所有内容转储到一个 csv 文件中。列始终相同。

我是手动做的:

$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
$xl.DisplayAlerts = $false
$counter = 0

$input_folder = "C:\Users\user\Documents\excelfiles"
$output_folder = "C:\Users\user\Documents\csvdump"

Get-ChildItem $input_folder -File |
Foreach-Object {
    $counter++
    $wb = $xl.Workbooks.Open($_.FullName, 0, 1, 5, "")
    try {
        $ws = $wb.Worksheets.item('Calls') # => This specific worksheet
        $rowMax = ($ws.UsedRange.Rows).count
        for ($i=1; $i -le $rowMax-1; $i++) {
            $newRow = New-Object -Type PSObject -Property @{
                'Type'                  = $ws.Cells.Item(1+$i,1).text
                'Direction'             = $ws.Cells.Item(1+$i,2).text
                'From'                  = $ws.Cells.Item(1+$i,3).text
                'To'                    = $ws.Cells.Item(1+$i,4).text
            }
        $newRow | Export-Csv -Path $("$output_folder\$ESO_Output") -Append  -noType -Force 
            }
        }

    } catch {
        Write-host "No such workbook" -ForegroundColor Red
        # Return
    }
}

问题:

这可行,但是非常慢,因为 Excel 必须选择每个单元格,复制它,然后 Powershell 必须创建数组并在输出 csv 文件中逐行保存。

有没有一种方法可以在 Excel 中选择一个范围(列数乘以($ws.UsedRange.Rows).count),剪切标题行并将这个范围(数组?)附加到 csv 文件以使一切更快?

【问题讨论】:

标签: excel powershell


【解决方案1】:

这就是最终的解决方案

脚本比原始解决方案快 22 倍!!!

希望有人会觉得这很有用:)

PasteSpecial 是过滤掉空行。无需将它们保存到 csv 中

$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
$xl.DisplayAlerts = $false
$counter = 0

$input_folder = "C:\Users\user\Documents\excelfiles"
$output_folder = "C:\Users\user\Documents\csvdump"

Get-ChildItem $input_folder -File |
Foreach-Object {
    $counter++
    try {
        $new_ws1 = $wb.Worksheets.add()
        $ws = $wb.Worksheets.item('Calls')
        $rowMax = ($ws.UsedRange.Rows).count
        $range = $ws.Range("A1:O$rowMax")
        $x = $range.copy()
        $y = $new_ws1.Range("A1:O$rowMax").PasteSpecial([System.Type]::Missing,[System.Type]::Missing,$true,$false)
        $wb.SaveAs("$($output_folder)\$($_.Basename)",[Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)

    } catch {
        Write-host "No such  workbook" -ForegroundColor Red
        # Return
    }

}
$xl.Quit()

上面的部分会生成一堆csv文件。

下面的部分将在单独的循环中读取这些文件并将它们组合在一起。

-exclude 是一个我想省略的数组

Remove-Item 删除临时文件

下面的答案基于这篇文章:https://stackoverflow.com/a/27893253/6190661

$getFirstLine = $true
Get-ChildItem "$output_folder\*.csv" -exclude $excluded | foreach {

    $filePath = $_

    $lines = Get-Content $filePath  
    $linesToWrite = switch($getFirstLine) {
           $true  {$lines}
           $false {$lines | Select -Skip 1}
    }

    $getFirstLine = $false
    Add-Content "$($output_folder)\MERGED_CSV_FILE.csv" $linesToWrite

    Remove-Item $_.FullName

}

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2015-02-15
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多