When I click a button I want to export data returned from a stored procedure to an Excel file using Asp.Net C# And Sql Server?
Asked
Active
Viewed 5,307 times
-1
-
2Do you have to use asp.net and c#? you can load the data on excel connected direct to sql server. – Ademar Oct 24 '13 at 10:07
3 Answers
1
just put your output as a table and add
Response.ContentType = "application/vnd.xls";
Response.AddHeader("Content-Disposition","attachment; filename=" + filName+ ".xls");

Izikon
- 902
- 11
- 23
0
You should probably be using a SDK like the OpenXMLSDK which is not specific to Microsoft
, but can generate the documents in any format that can be opened across different operating systems and platforms.
For more details, the pointers are as follows,
- Beginner sample codes for the Open XML SDK?
- http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML
- http://msdn.microsoft.com/en-us/library/office/ff478153.aspx
This is used in many products and IMHO, a professional way to export data from applications.
0
You can use COM Interop to create a new excel sheet: http://msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx Read values from your database, then create the excelsheet.
You could also think about using the CSV format. You can easely write a ';' seperated csv file which can be imported in excel It depends on what your reason is to store in excel format.

Jeroen Doppenberg
- 1,558
- 1
- 10
- 13