Solved

Would anyone want to share theire favorite coding for creating 'CSV' files?

Posted on 2006-06-20
6
223 Views
Last Modified: 2006-11-18
I can create a CSV file using FileOpen and PrintLine but was wondering what other more experienced programmers like to use. For this example, let's say each line of the file would be a record of 5 fields. It could be 1 record or 1000 or more recordsThe way I have done it is to have each record be a string variable and then append it to my file. It seems simple enough but was wondering if there are better ways.

I am sure this is easy but want to assign 500 points in hopes this interests someone and promotes discussion. I like seeing different ways of approaching a solution. Thank you in advance.
0
Comment
Question by:IT_Steve
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16947621
I find the best way to send a great number of fields under a certain amount of coding control is to use osql script right from the command line. SQL can run this as a job and it's a great way to build an interface
0
 
LVL 34

Accepted Solution

by:
Sancler earned 400 total points
ID: 16949673
Here's a sub for you to try

Imports System.IO

    Private Sub FillDelimitedFile(ByVal FullPath As String, ByVal Table As DataTable, ByVal Delimiter As String, Optional ByVal Append As Boolean = False, Optional ByVal Headers As Boolean = False)
        Dim ticks As Long = Now.Ticks
        Dim records As Long
        Dim sr As New StreamWriter(FullPath, Append)
        If Not Append Then
            If Headers Then
                Dim myLine As String = ""
                For Each dc As DataColumn In Table.Columns
                    If myLine <> "" Then
                        myLine &= Delimiter
                    End If
                    myLine &= dc.ColumnName
                Next
                sr.WriteLine(myLine)
            End If
        End If
        For Each dr As DataRow In Table.Rows
            Dim myLine As String = ""
            For Each dc As DataColumn In Table.Columns
                If myLine <> "" Then
                    myLine &= Delimiter
                End If
                If CStr(dr(dc.ColumnName)).IndexOf(Delimiter) <> -1 Then
                    myLine &= Chr(34) & dr(dc.ColumnName) & Chr(34)
                Else
                    myLine &= dr(dc.ColumnName)
                End If
            Next
            sr.WriteLine(myLine)
            records += 1
        Next
        sr.Close()
        Dim timelapse As Long = (Now.Ticks - ticks) / TimeSpan.TicksPerSecond
        MsgBox(records.ToString & " records added in " & timelapse & " seconds")
    End Sub

Roger
0
 

Author Comment

by:IT_Steve
ID: 16959610
I have been busy with other projects. Am beginning to try out Rogers ideas and should have more later today.

MacNuttin, could you show a small example of what you mean? I understand SQL but not sure how to use it the way you suggest.

Steve
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Assisted Solution

by:MacNuttin
MacNuttin earned 100 total points
ID: 16959892
I put this in a FileName.sql file:

set PAGESIZE 0
set COLSEP ','
set TERMOUT off
set WRAP on
set VERIFY off
set HEADING off
set SHOWMODE off
set feedback off
spool c:\FileName.txt
select Variables from Table-or-View  where Conditions;
spool off

Then call it from a cmd line in this case using sqlPlus for Oracle:

sqlplus login/password@ServerName @c:\FileName

The output is FileName.txt


0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16959905
Another way is that I use Kronos Connect to build csv files from databases and other flat file data sources
0
 

Author Comment

by:IT_Steve
ID: 16969277
For my given application, Sancler provided the example that best suits my needs and seems very portable for other applications. MacNuttin provided an interesting example using an SQL paradigm that I may explore more someday. Thank you both for your willinginess to help me out.  
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question