?
Solved

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

Posted on 2006-06-20
6
Medium Priority
?
227 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
[X]
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
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 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
                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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:MacNuttin
MacNuttin earned 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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