Search code examples
jqueryasp.netvb.netexport-to-excelserver-side

Passing client data to server to create Excel or CSV File


I think what I have may actually be a design issue rather than a technical issue, but either way I've seem to a hit a wall in both my ability and understanding to get past this problem.

I'm creating a reporting web page as part of a standard VB.NET web application. The page is data driven, with jquery making calls to a web service local to the application to get values of elements that are dynamically created in the DOM. Ultimately these dynamic elements and their values are all wrapped up in a JSON object, so that part of the client side is fine.

Right now, I can pass that JSON object to a method in the web service which creates an SQL Data adapter from the values and queries a database. My ultimate goal is to have this client JSON data be used to query the database and construct an excel file to send back to the user, or failing that a CSV file.

It's my understanding (from working with PDF file generation) that this type of function can only be done on the server side of the web application, not even from the web service (i.e. I can't have the web service return an array of bytes and have JavaScript/jQuery present that as a file).

So, how do I go about this? Now I have my JSON object on the client side, where should I be sending it, how, and how do I go about returning my file?

I've read about either storing the JSON in a hidden field and having the server take the elements value and go from there, or using AJAX to send the JSON to the server just before a postback is made to fire off the server-side function to run the query and return the data.

I just need a little guidance, and a little help understanding where to use the hidden field, or what I am actually doing with the pre-postback AJAX call.

Thanks.


Solution

  • I have done things similar to this in the past using a generic handler(ASHX) page. If you set the content type of the response HttpContext.Response.ContentType to be text/csv and you call

    window.location = "handler.ashx";

    It will generate the csv file on the server side and will download in the users browser. You could try passing the JSON as a QueryString and parse it in the handler page to do whatever you want on the server side in order to generate the CSV.

    Sample code:

    Public Sub ProcessRequest(context As HttpContext)
        context.Response.ContentType = "text/csv"
        context.Response.AddHeader("Content-Disposition", "attachment;filename=test.csv")
        Dim json As String = context.Request.QueryString("json")
        Using sw As New StreamWriter(context.Response.OutputStream)
            sw.WriteLine("Test!")
            sw.Flush()
        End Using
    End Sub
    

    I'm not sure if setting a hidden field on client side would help as reading it on server-side would probably not reflect the value you set due to the ViewState not recognizing the changes. There may be ways to get around this though.

    EDIT: Sorry over read the part that you said the code was VB.Net. Changed the c# code to VB.net