POSH:将Listview导出为CSV

时间:2014-06-30 12:31:12

标签: listview powershell export-to-csv

我有一个包含6列的列表视图,我想将它们导出为CSV

列1;列2;栏3 DATA1;数据2; DATA3

我以为我应该可以使用Listview1.Items | Export-csv C:\ temp \ test.csv 但我得到的只是Listview对话框而不是Items.SubItems.text

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$listView1 = New-Object System.Windows.Forms.ListView
$columnHeader1 = New-Object System.Windows.Forms.ColumnHeader
$columnHeader2 = New-Object System.Windows.Forms.ColumnHeader
$columnHeader3 = New-Object System.Windows.Forms.ColumnHeader
$columnHeader4 = New-Object System.Windows.Forms.ColumnHeader
$columnHeader5 = New-Object System.Windows.Forms.ColumnHeader
$columnHeader6 = New-Object System.Windows.Forms.ColumnHeader
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 471
$System_Drawing_Size.Width = 931
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"


$listView1.Columns.Add($columnHeader1)|Out-Null
$listView1.Columns.Add($columnHeader2)|Out-Null
$listView1.Columns.Add($columnHeader3)|Out-Null
$listView1.Columns.Add($columnHeader4)|Out-Null
$listView1.Columns.Add($columnHeader5)|Out-Null
$listView1.Columns.Add($columnHeader6)|Out-Null
$listView1.DataBindings.DefaultDataSourceUpdateMode = 0
$listView1.FullRowSelect = $True
$listView1.GridLines = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$listView1.Location = $System_Drawing_Point
$listView1.Name = "listView1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 447
$System_Drawing_Size.Width = 907
$listView1.Size = $System_Drawing_Size
$listView1.TabIndex = 0
$listView1.UseCompatibleStateImageBehavior = $False
$listView1.View = 1

$form1.Controls.Add($listView1)

$columnHeader1.Width = 102
$columnHeader2.Width = 134
$columnHeader3.Width = 147
$columnHeader4.Width = 149
$columnHeader5.Width = 91
$columnHeader6.Width = 194

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

1 个答案:

答案 0 :(得分:0)

您好我自己整理了一下但是我不想在可能的情况下使用Foreach循环。

所以,如果有人有更好的想法,请让我们知道!

Function Export-lvData {
    $CsvArray = @()
    $listView1.Items | %{
        $Object = New-Object PSObject
        $lvItem = $_
        $listview1.Columns | %{
            $Object | add-member Noteproperty -Name $_.Text -Value  $lvItem.SubItems[$_.index].text -Force
        }
        $CsvArray += $Object
        Remove-Variable Object
    }
    Return ,$CsvArray
}

返回CsvArray后,我可以将输出传递给Export-csv -notype