Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Data reader to CSV

Posted on 2006-06-28
4
Medium Priority
?
349 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 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