Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I saw this code example which allows for dumping listview to csv.

C#
private void Button1_Click(object sender, EventArgs e) 
        { 
             
            //declare new SaveFileDialog + set it's initial properties 
 
            { 
                SaveFileDialog sfd = new SaveFileDialog { 
                    Title = "Choose file to save to", 
                    FileName = "example.csv", 
                    Filter = "CSV (*.csv)|*.csv", 
                    FilterIndex = 0, 
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 
                }; 
 
                //show the dialog + display the results in a msgbox unless cancelled 
 
 
                if (sfd.ShowDialog() == DialogResult.OK) { 
                     
                    string[] headers = ListView1.Columns 
                               .OfType<ColumnHeader>() 
                               .Select(header => header.Text.Trim()) 
                               .ToArray(); 
 
                    string[][] items = ListView1.Items 
                                .OfType<ListViewItem>() 
                                .Select(lvi => lvi.SubItems 
                                    .OfType<ListViewItem.ListViewSubItem>() 
                                    .Select(si => si.Text).ToArray()).ToArray(); 
 
                    string table = string.Join(",", headers) + Environment.NewLine; 
                    foreach (string[] a in items) 
                    { 
                        //a = a_loopVariable; 
                        table += string.Join(",", a) + Environment.NewLine; 
                    } 
                    table = table.TrimEnd('\r', '\n'); 
                    System.IO.File.WriteAllText(sfd.FileName, table); 
 
                } 
            } 
  
        } 


But how could I use it to dump a specific column. I have tried different variation of modifying the code by commenting out or changing a few lines, but it either does nothing or just throws more errors.
Posted
Comments
Ralf Meier 16-Jun-15 8:50am    
In this case you have to access the specific Item or the specific Item-Index.
Could you give an example to where you want to get ?
Herboren 16-Jun-15 14:16pm    
My listview contains the first item and then 6 sub-items following it. There is about 7 rows in the third column which is what I need to dump to a csv

1 solution

I fixed it:
C#
string[] apString = new string[lstvResults.Items.Count];
            if (dlgExportFile.ShowDialog() == DialogResult.OK)
            { 
                
                // Count Row Indicies
                for (int i = 0; i < lstvResults.Items.Count-1; i++)
                {
                    // Get Row Index
                    ListViewItem Col_Get = lstvResults.Items[i];    

                    // Element in Array Gets MD5 SubItem
                    apString[i] = Col_Get.SubItems[2].Text;    
                }
            }
            string join = string.Join(",\n", apString);
            join = join.TrimEnd('\r');
            System.IO.File.WriteAllText(dlgExportFile.FileName, join);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900