2

I would like to export data from a SQL Server pull (usually I use SqlDataReader, but am open to suggestions) into an Excel/CSV file and have the user of the web app get it as a download.

I'd like to do this AJAX style, so that the click on download won't cause a reload/postback.

Anyone done this before or have an idea on how?

2
  • You're going to have issues with a web app & Excel. Try this answer: stackoverflow.com/a/11559077/61339 Commented Oct 23, 2012 at 18:15
  • @JeffO I'm fine with a csv. I'm still not sure how to work the download on click though. Commented Oct 23, 2012 at 18:19

1 Answer 1

1

I think this mechanism would work.

Have an HTML button that calls a javascript function when clicked.

<input id="downloadbtn" type="button" value="Download" onclick="download();" />

Function would look something like this:

var download = function() {
  $.ajax({
  url: "datamaker.aspx",
  context: document.body
  }).done(function(data) { 
    //  parse response data, get file path
    var filepath = ParseDataToGetFilePath(data);

    // open the csv file path in a new window (which will begin download)
    window.open(filepath, '_blank');
  });

};

In the serverside, "datamaker.aspx" should connect to the SQL Server, and create the CSV file. Creating a CSV file is as easy as creating any other file using streamwriter, and writiing "comma" between fields. At the end of this page, output the filename either in a response xml or json.

     { filepath: \path\datafile.csv }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.