[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2006-06-20
Medium Priority
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.
Question by:IT_Steve
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 11

Expert Comment

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
LVL 34

Accepted Solution

Sancler earned 1600 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
            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)
                    myLine &= dr(dc.ColumnName)
                End If
            records += 1
        Dim timelapse As Long = (Now.Ticks - ticks) / TimeSpan.TicksPerSecond
        MsgBox(records.ToString & " records added in " & timelapse & " seconds")
    End Sub


Author Comment

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.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 11

Assisted Solution

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

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

LVL 11

Expert Comment

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

Author Comment

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.  

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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