We let users create ad-hoc queries in our website. We would like to have the user select their criteria, then click submit and have the results streamed automatically to Excel. I have the application populating a DataTable, then using the datatable to create a tab delimited string. The problem is getting that to excel.
What is the best way to stream data to Excel? Preferrably, we wouldn't have to make users close an empty window after clicking the submit button.
I would use a handler for the .xls file extension and a free component to convert the DataTable to native xls format. The component from this site http://www.csvreader.com/ does more that the URL implies. The newest version of excel will complain about an HTML formatted XLS file. Also keep in mind the size of the data being returned. Your web server should use compression for this extension and your code should check if the number of rows returned is greater than what excel can display in one worksheet; multiple sheets may be required. http://www.mrexcel.com/archive2/23600/26869.htm
Kindly use this code to resolve your problem.This code will convert excel sheet to text format.Hope this will solve your problem
I got a utils function that does this already. Once you put it into a datatable, you can export it with the Response using
I'd recommend using a filehandler (.ashx) The only issue is creating the excel file from the DataTable. There are a lot of third party products that will do this for you (e.g. Infragistics provides a component that does just this).
One thing I highly recommend against is using the Excel interop on your server...it's very heavyweight and isn't supported.
Change the page's file type to excel, and only stream the HTML necessary to build a table to the page. code from here
If you create a page that is just a table with the results and set the page's content type to "application/vnd.ms-excel", then the output will be in Excel.
If you want to force a save, you would do something like the following: