如何在 Powershell 中对特定列 A-Z 进行排序?
How to Sort a specific column A-Z in Powershell?
我有一个将 .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
我建议您注意输入 Get-Help -Name Sort-Object -Full
可获得的信息。假设,为了讨论的目的,您的 Column G 在 CSV 文件中有一个 header 的 "G",您需要一些代码
Import-CSV -Path $inputcsv | Sort-Object -Property G | Export-CSV -Append -Path $sortedcsv -NoTypeInformation
然后将 $sortedcsv 传递给您的 workbook-building 例程。
我有一个将 .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
我建议您注意输入 Get-Help -Name Sort-Object -Full
可获得的信息。假设,为了讨论的目的,您的 Column G 在 CSV 文件中有一个 header 的 "G",您需要一些代码
Import-CSV -Path $inputcsv | Sort-Object -Property G | Export-CSV -Append -Path $sortedcsv -NoTypeInformation
然后将 $sortedcsv 传递给您的 workbook-building 例程。