Solved

Write and save a semicolon delimeted csv file

Posted on 2014-01-03
4
538 Views
Last Modified: 2014-01-08
Hi,

I have this data that I would like to save to a semicolon seperated csv/text file.

The data result from the sql database looks like this:

EmployeeID   aDay  aMonth  aYear      WHours
1                    1        12          2013       8
1                    2        12          2013       6,5
2                    1        12          2013       7
2                    2        12          2013       4,75

I would like this to be written to the following format:
1;20131201;800
1;20131202;650
2;20131201;700
2;20131202;475

And then possible to save as a csv/text file to the harddrive.

Thanks for help!

Peter
0
Comment
Question by:peternordberg
[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
4 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 167 total points
ID: 39753469
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 167 total points
ID: 39754090
Hi peternordberg;

I am assuming that you place the data from the database into a DataTable control and that all columns are integer except the WHours which I assume is Decimal type. The following Linq to Object query should work the way you need it to.

Dim query = From data In dt.AsEnumerable() _
            Select data.Field(Of Integer)("EmployeeID").ToString() & ";" & _
                   data.Field(Of Integer)("aYear").ToString("D4") & _
                   data.Field(Of Integer)("aMonth").ToString("D2") & _
                   data.Field(Of Integer)("aDay").ToString("D2") & ";" & _
                   (data.Field(Of Decimal)("WHours") * 100).ToString("G3")

For Each line In query
    Console.WriteLine(line)
Next

Open in new window

0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 166 total points
ID: 39755993
This is my rough idea ., do make testing with code when u apply.,

Code to Write the Text in to TEXT File :

Dim FILE_OUT_PATH As String =HttpContext.Current.Server.MapPath("~") & "\SaveFile\"
            If Not Directory.Exists(FILE_OUT_PATH) Then
                Directory.CreateDirectory(FILE_OUT_PATH)
            End If
            Dim outFileName As String = "Filename1_" & Today.ToString("ddMMyyyy") & "_" & Now.ToString("HHmmss") & ".csv"
            Dim outFilePath As String = FILE_OUT_PATH & "\" & outFileName

            Dim oWrite As StreamWriter
            oWrite = File.CreateText(outFilePath)
            dim line as string
               
         'Format the String as u mentioned in loop and assign the formatted value to variable "Line"
          '********************************************************
            oWrite.WriteLine(Line)

            Dim file As System.IO.FileInfo = New System.IO.FileInfo(outFilePath)
                If file.Exists Then 'set appropriate headers
                    Response.Clear()
                    HttpContext.Current.Response.Clear()
                    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", file.Name))
                    HttpContext.Current.Response.ContentType = "plain/text"

                    Dim MyStreamReader As New StreamReader(file.FullName)
                    Dim str As String = MyStreamReader.ReadToEnd()
                    HttpContext.Current.Response.Write(str)
                    MyStreamReader.Close()
                    HttpContext.Current.Response.End()
                Else
                    Response.Write("This file does not exist.")
                End If
0
 

Author Closing Comment

by:peternordberg
ID: 39766607
thanks for good help!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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