List of objects to CSV?


Well-known member
Dec 8, 2009
Programming Experience
Given this code:

    Public Class Foo
        Property thing1 As String
        Property thing2 As String
        Property thing3 As String
        Property thing4 As String
        Property thing5 As String
    End Class

    Sub Main()
        Dim s as String = ""
        Dim l As List(Of Foo) = GetListOfFoo()

    End Sub

I want to set 's' to a CSV that MS Excel will understand. I figure there's got to be an MS library that lets me do this fairly simply, and that already accounts for tricky cases like comma values in the properties of Foo, or anything like that that I didn't think of. I did see some simple looking code examples involving Linq, but they don't account for the possibility of comma in the property values, which is why I'm hoping there's something in the framework (or in an Office library) that does all this for me.


Well-known member
Sep 9, 2012
Healing, NE Lincs, UK
Programming Experience

As far as I am aware there is no standard assembly built to do this although it is easy enough to create your own routines to complete this task.

To get you started you need to look at Generics to pass a List of any Object Type to a routine. You can then use Reflection to both iterate the Property Names of the object to create a CSV Header record and then also iterate the Property Values of each object in the List to create each record for the CSV File. At the same time, you can create and call a Function that looks for the “Comma” character in each Property Value and, if found, encase that particular value in Quote characters. (In the case of numeric types make sure you leave these as numeric types and NOT string types formatted with comma characters)

Once done you can then just simply write all the CSV Records to a file using any method of your choice.

Hope that helps.


Top Bottom