Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Data reader to CSV

Posted on 2006-06-28
4
Medium Priority
?
345 Views
Last Modified: 2012-06-27
I need a web page in C# that will take the results from the execution of a stored procedure and create an Excel compatible .csv file and prompt the user to download it. Is there a way to do this on the fly?
0
Comment
Question by:strickdd
[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 12

Expert Comment

by:deanvanrooyen
ID: 17005163
does this help
http://www.codeproject.com/Purgatory/Exporting_Dataset_ASPNet.asp

can xl read xml?

cause you could just write the dataset to xml - it already supports that...

        DataSet ds = new DataSet();
        //fill dataset
        ds.WriteXml("filename.xml");
0
 
LVL 27

Accepted Solution

by:
Sammy Ageil earned 2000 total points
ID: 17005232
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17005349
here is a function to which you'll have to pass the path to the file you want to write to and a datatable object with data from database:

Public Sub WriteDataToCSV(ByVal path As String, ByVal dt As System.Data.DataTable)
        Dim delim As String
        Dim sw As StreamWriter

        ' Write out the header row
        delim = ""
        Try
            sw = New StreamWriter(path, False, UnicodeEncoding.Default)
            For Each col As DataColumn In dt.Columns
                sw.Write(delim)
                sw.Write(col.ColumnName)

                delim = ","
            Next
            sw.WriteLine()

            ' write out each data row
            For Each row As DataRow In dt.Rows
                delim = ""
                For Each value As Object In row.ItemArray
                    sw.Write(delim)
                    If TypeOf value Is String Then
                        sw.Write(""""c) ' thats four double quotes and a c
                        sw.Write(value)

                        sw.Write(""""c) ' thats four double quotes and a c
                    Else
                        sw.Write(value)
                    End If
                    delim = ","
                Next
                sw.WriteLine()
            Next
        Catch ex As Exception
            Console.Write("ERROR: " & ex.Message)
        Finally
            sw.Close()
        End Try

    End Sub
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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