exporting sql query result to excel
In my asp.net application i wanted to export the SQL query result to Excel and th开发者_如何学Pythonat excel should be avaliable to the user as a download. Please help
I find working with excel to be a real headache, but it's possible to do just about anything if you want to. What features do you need to leverage, because offering a csv file instead is much easier!
Do you have SSRS, you could offer the query as an SSRS report and it is automatically available as an excel download!
If your on MS SQL Server, reporting services are available for free and can do this for you easily, you can also export to PDF and Word.
If not, or if its just a one off try here
you can show your result in a datagrid. after that you can export this grid to excel file.
do like this : I pass grid by session.
Control grdList;
GridView grdList1 = Session["GridView"] as GridView;
if (grdList1 == null)
{
grdList = (DataGrid)Session["GridView"];
}
else
{
grdList = (GridView)Session["GridView"];
}
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExportList.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
Response.ContentEncoding = System.Text.Encoding.UTF8;// GetEncoding(1256);// UTF8;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
grdList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
精彩评论