Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Write and save a semicolon delimeted csv file

Posted on 2014-01-03
4
Medium Priority
?
547 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 501 total points
ID: 39753469
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 501 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 498 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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