Export a table to a csv
-
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
-
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
http://www.codeproject.com/Purgatory/Exporting_Dataset_ASPNet.asp[^]
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
http://www.codeproject.com/Purgatory/Exporting_Dataset_ASPNet.asp[^]
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
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
-
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
-
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 -
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
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!
-
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!
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