Solved

Write and save a semicolon delimeted csv file

Posted on 2014-01-03
4
535 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 63
Looping through datagridview and dataset ? 6 24
ASP.NET Content Page 3 35
reading excel file in .net 2 19
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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