-1

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?

dcaswell
  • 3,137
  • 2
  • 26
  • 25
Mohamed Salah
  • 29
  • 1
  • 6
  • 2
    Do 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 Answers3

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,

  1. Beginner sample codes for the Open XML SDK?
  2. http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML
  3. 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.

Community
  • 1
  • 1
Saravanan
  • 7,637
  • 5
  • 41
  • 72
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