Solved

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

Posted on 2006-06-20
6
221 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Assisted Solution

by:MacNuttin
MacNuttin earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now