Asp.net
-
how to export the data from stored procedure to excel?
-
how to export the data from stored procedure to excel?
What have you tried? Where are you stuck? What help do you need?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
how to export the data from stored procedure to excel?
NB: You can't use Office Interop in an ASP.NET application:
Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Use a tool like EPPlus[^], ClosedXML[^], or The OpenXML SDK[^] to generate an Excel file. For example, EPPlus provides both
LoadFromDataReader
andLoadFromDataTable
methods which you could use to load the data from your query into the Excel file.// Load the data from your stored procedure:
DataTable dt = LoadDataFromStoredProcedure();// Export the data to an Excel file:
var pck = new ExcelPackage();
var wsDt = pck.Workbook.Worksheets.Add("FromDataTable");
wsDt.Cells["A1"].LoadFromDataTable(dt, /* PrintHeaders: */ true, TableStyles.Medium9);// Send the Excel file back to the user:
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
pck.SaveAs(Response.OutputStream);
Response.Flush();// If you're running this code from a page (.aspx) instead of a generic handler (.ashx):
Response.SuppressContent = true;
Context.ApplicationInstance.CompleteRequest();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
how to export the data from stored procedure to excel?