- private static void ExportDataGridViewToExcel(Control dtg, string fileName)
- {
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
- HttpContext.Current.Response.Charset = "UTF-8";
- HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
- HttpContext.Current.Response.ContentType = "application/ms-excel";
- dtg.Page.EnableViewState = false;
- System.IO.StringWriter tw = new System.IO.StringWriter();
- System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
- dtg.RenderControl(hw);
- HttpContext.Current.Response.Write(tw.ToString());
- HttpContext.Current.Response.End();
- }
Note:
Khi gặp báo lỗi Control '...' of type 'GridView' must be placed inside a form tag with runat=server
thì ở webpage nhớ override lại hàm VerifyRenderingInServerForm là ok.
- public override void VerifyRenderingInServerForm(Control control)
- {
- // không làm gì cả
- }
Không có nhận xét nào :
Đăng nhận xét