Export EntityCollection or TypedList to CSV?

Posts   
 
    
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 16-Dec-2004 21:34:11   

I know I'm going to have to do this in a few days, so I was hoping someone might have already done this or at least some pointers on where to start...

The end result should be a comma separated text file..

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Dec-2004 22:06:33   

Hmm. Can be pretty horrible to code. IF possible, but this is pretty resource intensive, use Excel. Open Excel in code and write the data to a sheet, then tell Excel to save the stuff as comma separated data using a macro.

but this is very 'shakey'. In the old days, you could use the ado text driver, but this isn't supported in .NET...

(the main problems with CSV's are that you have to scan each string for commas, add quotes where appropriate... )

Frans Bouma | Lead developer LLBLGen Pro
banusi
User
Posts: 43
Joined: 08-Jul-2006
# Posted on: 14-Jul-2006 09:41:17   

I also need this function, and I study now how to do it in a smart way, without using Excel.

Is there any other place in the forum, which discuss simular things?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Jul-2006 11:31:27   

It basicly comes down to traversing all rows /instances in a container and per instance/row all fields and write them to a file, separated by commas, and when a comma is present in the string to write, you enclose it in quotes.

The problem with quotes in the middle of a string is perhaps interesting, but you could solve that in a lot of ways in your own implementation.

Frans Bouma | Lead developer LLBLGen Pro
stoneyowl avatar
stoneyowl
User
Posts: 62
Joined: 29-Jan-2004
# Posted on: 14-Jul-2006 14:40:48   

You might want to take alook at this...

http://www.codeproject.com/useritems/filehelpers.asp

I have used it for several months, with great results.

arschr
User
Posts: 893
Joined: 14-Dec-2003
# Posted on: 14-Jul-2006 14:50:02   

You might want to take alook at this...

http://www.codeproject.com/useritems/filehelpers.asp

I have used it for several months, with great results.

A new version was released a few weeks ago.

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 14-Jul-2006 21:21:04   

Public Function ExportBalances(ByVal exportType As String) As Boolean

    Dim result As Boolean
    Dim balances As DataTable
    Dim fields As New HelperClasses.ResultsetFields(19)
    Dim relations As IRelationCollection = New RelationCollection
    Dim sorter As ISortExpression = New SortExpression
    Dim filter As IPredicateExpression = New PredicateExpression
    Dim exportFileName As String

    'define fields
    fields.DefineField(TblKlantFieldIndex.KlantNummer, 0, "KlantNummer")
    fields.DefineField(TblKlantFieldIndex.Naam, 1, "KlantNaam")
    fields.DefineField(TblBoekingFieldIndex.DepotId, 2, "DepotNummer")
    fields.DefineField(TblDepotFieldIndex.DepotNaam, 3, "DepotNaam")
    fields.DefineField(TblBoekingFieldIndex.Klm1, 4, "Klm1", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm2, 5, "Klm2", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm3, 6, "Klm3", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm4, 7, "Klm4", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm5, 8, "Klm5", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm6, 9, "Klm6", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm7, 10, "Klm7", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm8, 11, "Klm8", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm9, 12, "Klm9", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm10, 13, "Klm10", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm11, 14, "Klm11", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm12, 15, "Klm12", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm13, 16, "Klm13", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm14, 17, "Klm14", "", AggregateFunction.Sum)
    fields.DefineField(TblBoekingFieldIndex.Klm15, 18, "Klm15", "", AggregateFunction.Sum)
    'add relations
    sorter.Add(FactoryClasses.SortClauseFactory.Create(TblKlantFieldIndex.KlantNummer, SortOperator.Ascending))
    filter.Add(FactoryClasses.PredicateFactory.CompareValue(TblBoekingFieldIndex.Datum, ComparisonOperator.LessEqual, Date.Today))
    relations.Add(EntityClasses.TblKlantEntity.Relations.TblBoekingEntityUsingKlantNummer)
    relations.Add(EntityClasses.TblBoekingEntity.Relations.TblDepotEntityUsingDepotId)

    Try
        exportFileName = settings.GetConfigValue("strExportFolder")
        exportFileName &= "\BalancePerClient_" & Format(Date.Today, "yyyyMMdd")
        balances = FetchTypedList(fields, filter, relations, sorter, False, 0, True, Nothing)
        balances.TableName = "BalancePerClient"

        If balances.Rows.Count > 0 Then
            Select Case exportType
                Case "XML"
                    Dim ds As New DataSet("Balances")
                    ds.Tables.Add(balances)
                    ds.WriteXml(exportFileName & ".xml")
                    ds.WriteXmlSchema(exportFileName & ".xsd")
                    ds.Dispose() : ds = Nothing
                Case "CSV"
                    exportFileName &= ".csv"
                    Dim output As New StreamWriter(exportFileName, False, System.Text.UnicodeEncoding.Default)

                    ' write out the header row
                    For Each col As DataColumn In balances.Columns
                        output.Write(col.ColumnName)
                        output.Write(delimiter)
                    Next
                    output.WriteLine()

                    ' write out each data row
                    For Each row As DataRow In balances.Rows
                        For Each value As Object In row.ItemArray
                            If TypeOf value Is String Then
                                output.Write(""""c) 'that's four double quotes and a c
                                output.Write(value)

                                output.Write(""""c) 'that's four double quotes and a c
                            Else
                                output.Write(value)
                            End If
                            output.Write(delimiter)
                        Next
                        output.WriteLine()
                    Next

                    output.Close()
                    output.Flush()
                    output = Nothing
            End Select
        Else
            Console.WriteLine("No data found !")
        End If
    Catch ex As Exception
        LogError("ExportBalances", ex.Message)
    Finally
        balances = Nothing
        fields = Nothing
        relations = Nothing
        sorter = Nothing
    End Try

End Function
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 15-Jul-2006 01:11:09   

stoneyowl wrote:

You might want to take alook at this...

http://www.codeproject.com/useritems/filehelpers.asp

I have used it for several months, with great results.

Awesome link there, Mr. Owl, and very timely. Thanks!

Jeff

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 15-Jul-2006 18:24:12   

This works great!

http://www.csvreader.com/

alexdresko wrote:

I know I'm going to have to do this in a few days, so I was hoping someone might have already done this or at least some pointers on where to start...

The end result should be a comma separated text file..

Thanks!