Solved

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

Posted on 2006-06-20
6
226 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Technology Partners: 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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

690 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