Solved

Data reader to CSV

Posted on 2006-06-28
4
340 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is "packages element is not declared"? 2 129
Search by two fields at the same time using c# radio button 5 39
asp.net mvc return json 2 56
SQL Exceptions 3 37
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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 …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

25 Experts available now in Live!

Get 1:1 Help Now