0
votes

i have a listview with 6 columns and I want to export them to CSV

column1; column2; column3 data1; data2; data3

etc

I Thought I should be possible to use Listview1.Items | Export-csv C:\temp\test.csv but all I get is Listview dialog in formation not the 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

1 Answers

0
votes

Hi i sorted it out myself but i rather don't like to use Foreach-loops if possible for this.

So if someone has a better idea please Let met know!

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
}

After Returning the CsvArray i can pipe the output to Export-csv -notype