Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Export a table to a csv

Export a table to a csv

Scheduled Pinned Locked Moved ASP.NET
sysadminhelpquestiondiscussion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Senseicads
    wrote on last edited by
    #1

    Can someone tell me if this is possible please. I have created a web based reporting application for my boss and he wants the option to export the tables that get shown to a .csv file. Seems reasonable except I am pretty sure that as it's web based I can only write back to the server...is this true? If not what do I do to save the .csv on his local machine? I was thinking that maybe I can save it on the server then try and open it on via the browser so the standard download attatchment window comes up? is there a set/standard or best practice way of doing this? Thanks in advance for any help. Ian

    V M 2 Replies Last reply
    0
    • S Senseicads

      Can someone tell me if this is possible please. I have created a web based reporting application for my boss and he wants the option to export the tables that get shown to a .csv file. Seems reasonable except I am pretty sure that as it's web based I can only write back to the server...is this true? If not what do I do to save the .csv on his local machine? I was thinking that maybe I can save it on the server then try and open it on via the browser so the standard download attatchment window comes up? is there a set/standard or best practice way of doing this? Thanks in advance for any help. Ian

      V Offline
      V Offline
      Vasudevan Deepak Kumar
      wrote on last edited by
      #2

      http://www.codeproject.com/Purgatory/Exporting_Dataset_ASPNet.asp[^]

      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

      S 1 Reply Last reply
      0
      • V Vasudevan Deepak Kumar

        http://www.codeproject.com/Purgatory/Exporting_Dataset_ASPNet.asp[^]

        Vasudevan Deepak Kumar Personal Homepage Tech Gossips

        S Offline
        S Offline
        Senseicads
        wrote on last edited by
        #3

        So having a quick glance thru that article and the code for it, that just writes a csv string to the browser. I can do that already, I can parse the table to a csv stream no probs. :D My question and what I am more interested in knowing about is how I can export a .csv file from the datatable to a file through a button click on the browser...do i have to save the csv on the server? or can i save it straight to a user specified location on the local machine? I was thinking that if it wasn't possible to save to the user's local machine do I save it to the server with a unique name then try and open the file from the server? Is that the best way of doing things? or is there a reccomended best practive for this? Thanks for taking the time to help. Cheers Ian

        E 1 Reply Last reply
        0
        • S Senseicads

          So having a quick glance thru that article and the code for it, that just writes a csv string to the browser. I can do that already, I can parse the table to a csv stream no probs. :D My question and what I am more interested in knowing about is how I can export a .csv file from the datatable to a file through a button click on the browser...do i have to save the csv on the server? or can i save it straight to a user specified location on the local machine? I was thinking that if it wasn't possible to save to the user's local machine do I save it to the server with a unique name then try and open the file from the server? Is that the best way of doing things? or is there a reccomended best practive for this? Thanks for taking the time to help. Cheers Ian

          E Offline
          E Offline
          ednrgc
          wrote on last edited by
          #4

          Check into placing the results in a stream.

          S 1 Reply Last reply
          0
          • E ednrgc

            Check into placing the results in a stream.

            S Offline
            S Offline
            Senseicads
            wrote on last edited by
            #5

            yeah I can do all that I can put the csv file using streamwriter...i just need to know where to put the file that streamwriter creates and how do i access it! At the moment I am attempting to place the file on the server and then redirect the browser to the file name. This isn't working for me yet however, and I am not sure that this is even the best way to do it. I was asking which is the best way. Here is my code, so you can see where I am currently coming from, it don't work like but it should give you an idea as to where i am coming from. But don't forget I am enquiring about best practices here, not how to fix this specific bit of code. Protected Sub btExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btExport.Click Dim datTable As DataTable = gvReport.DataSource ' Create a directory for the file to be stored on the server ' Check whether a directory exists in the first place. If not create it. If Not Directory.Exists(Server.MapPath("CSV")) Then Directory.CreateDirectory(Server.MapPath("CSV")) End If ' Create a unique identifier. Dim strFileName As String strFileName = Server.MapPath("CSV") & "\" & GetTempName() Dim objStreamWriter As StreamWriter objStreamWriter = File.AppendText(strFileName) ' Code to write the csv file to the writer. objStreamWriter.Close() End Sub Cheers Ian

            1 Reply Last reply
            0
            • S Senseicads

              Can someone tell me if this is possible please. I have created a web based reporting application for my boss and he wants the option to export the tables that get shown to a .csv file. Seems reasonable except I am pretty sure that as it's web based I can only write back to the server...is this true? If not what do I do to save the .csv on his local machine? I was thinking that maybe I can save it on the server then try and open it on via the browser so the standard download attatchment window comes up? is there a set/standard or best practice way of doing this? Thanks in advance for any help. Ian

              M Offline
              M Offline
              martin_hughes
              wrote on last edited by
              #6

              Here's the way I've seen it done before (no idea if it's best practice - but what the hey! :) ) When the user clicks the button, have the database export to a temporary .csv somewhere on the server (that the user will be able to access). Then do a response.redirect as the return of the page postback giving the url of the newly created temporary file. Hey presto, the user should be presented with a file download dialog. There's bound to be a few gotchas, the one that springs readily to my mind is that you must make sure to purge that temp directory on a frequent basis, or you'll enter a world of pain!

              S 1 Reply Last reply
              0
              • M martin_hughes

                Here's the way I've seen it done before (no idea if it's best practice - but what the hey! :) ) When the user clicks the button, have the database export to a temporary .csv somewhere on the server (that the user will be able to access). Then do a response.redirect as the return of the page postback giving the url of the newly created temporary file. Hey presto, the user should be presented with a file download dialog. There's bound to be a few gotchas, the one that springs readily to my mind is that you must make sure to purge that temp directory on a frequent basis, or you'll enter a world of pain!

                S Offline
                S Offline
                Senseicads
                wrote on last edited by
                #7

                Thanks thats the way I have implemented it now. I perform the purge whenever the application start method runs. The Server is shutdown at the end of each working day so it should purge once a day. Its a right bit of a bugger tho. Esp as I haven't been able to figure out as to when the application object is created if the path of the calling webform isn't in the root then it has the potential to create CSV directories allover the shop :-S I am using MapPath to get the Path name and it just picks up the current location. nightmare! :( lol I am sure is will come up with something tho :D Cheers For everyone's help esp Martin! :D Ian

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups