none
vb.net ListView control

    Question

  • Does anyone know of a good link, a resource article or some code on exporting Data from a ListView control to Excel (using VB.Net)?

    I can't use a DataGrid. I was but once I implemented Multithreading, I couldn't use the DataGrid and had to use the ListView control.

     

    I would appreciate any help. Smile

     

    Thank you,

    Wallace

    Tuesday, July 03, 2007 6:57 PM

Answers

  • Hi

     

    Is it feasible to write the data out to a CSV file? This would be a fairly simple solution and Excel works fairly well with CSV files.

     

            Dim csvFileContents As New System.Text.StringBuilder

            Dim currentLine As String = String.Empty

     

            'Write out the column names as headers for the csv file.

            For columnIndex As Int32 = 0 To ListView1.Columns.Count - 1

                currentLine &= (String.Format("{0},", ListView1.Columns(columnIndex).Text))

            Next

     

            'Remove trailing comma

            csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))

            currentLine = String.Empty

     

            'Write out the data.

            For Each item As ListViewItem In ListView1.Items

     

                For Each subItem As ListViewItem.ListViewSubItem In item.SubItems

                    currentLine &= (String.Format("""{0}"",", subItem.Text))

                Next

     

                'Remove trailing comma

                csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))

                currentLine = String.Empty

     

            Next

     

            'Create the file.

            Dim sw As New System.IO.StreamWriter("C:\Temp\Test.csv")

            sw.WriteLine(csvFileContents.ToString)

            sw.Flush()

            sw.Dispose()

     

     

    HTH

    Tuesday, July 03, 2007 7:42 PM

All replies

  • Hi

     

    Is it feasible to write the data out to a CSV file? This would be a fairly simple solution and Excel works fairly well with CSV files.

     

            Dim csvFileContents As New System.Text.StringBuilder

            Dim currentLine As String = String.Empty

     

            'Write out the column names as headers for the csv file.

            For columnIndex As Int32 = 0 To ListView1.Columns.Count - 1

                currentLine &= (String.Format("{0},", ListView1.Columns(columnIndex).Text))

            Next

     

            'Remove trailing comma

            csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))

            currentLine = String.Empty

     

            'Write out the data.

            For Each item As ListViewItem In ListView1.Items

     

                For Each subItem As ListViewItem.ListViewSubItem In item.SubItems

                    currentLine &= (String.Format("""{0}"",", subItem.Text))

                Next

     

                'Remove trailing comma

                csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))

                currentLine = String.Empty

     

            Next

     

            'Create the file.

            Dim sw As New System.IO.StreamWriter("C:\Temp\Test.csv")

            sw.WriteLine(csvFileContents.ToString)

            sw.Flush()

            sw.Dispose()

     

     

    HTH

    Tuesday, July 03, 2007 7:42 PM
  • This was perfect David. I just made a few modifications to suite some of my applications needs. This certainly sped up creating the last section of my app. Can't thank you enough. Nice work!!

     

    Wallace

    Friday, July 06, 2007 3:01 PM