如何排序在PowerShell中的特定列A-Z?

问题描述 投票:0回答:1

我有一个转换.csv文件到的.xlsx PowerShell脚本,但现在我有一个额外的要求是排序(从低到高)特定的列file.Column“G”。任何建议?

我现有的代码如下。

Function conversion($inputCSV,$outputXLSX)
{


    ### Create a new Excel Workbook with one empty sheet
    $excel = New-Object -ComObject excel.application 
    $workbook = $excel.Workbooks.Add(1)
    $worksheet = $workbook.worksheets.Item(1)


    ### Build the QueryTables.Add command
    ### QueryTables does the same as when clicking "Data » From Text" in Excel
    $TxtConnector = ("TEXT;" + $inputCSV)
    $Connector =$worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))

    $query = $worksheet.QueryTables.item($Connector.name)

    ### Set the delimiter (, or ;) according to your regional settings
    $query.TextFileOtherDelimiter = $Excel.Application.International(5)

    ### Set the format to delimited and text for every column
    ### A trick to create an array of 2s is used with the preceding comma
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1

    ### Execute & delete the import query
    $query.Refresh()
    $query.Delete()



    ### Save & close the Workbook as XLSX. Change the output extension for Excel 2003
    $Workbook.SaveAs($outputXLSX,51)
    $excel.Quit()
}

$inputCSV= "C:\Users\Desktop\Book2.csv"
$outputXLSX = "C:\Users\Desktop\Book2_sorted.xlsx"

conversion $inputCSV,$outputXLSX
powershell-v3.0
1个回答
0
投票

我向你注意输入Get-Help -Name Sort-Object -Full获得的信息。另外,在假设讨论的目的,你的G列有“G”头CSV文件,你想沿着线的一些代码

Import-CSV -Path $inputcsv | Sort-Object -Property G | Export-CSV -Append -Path $sortedcsv -NoTypeInformation

然后通过$ sortedcsv到您的工作簿建设程序。

© www.soinside.com 2019 - 2024. All rights reserved.