Solved

Data reader to CSV

Posted on 2006-06-28
4
335 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
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 earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now