protected void ButtonExportar_Click(object sender, EventArgs e) { if (Session[this.lblTableNameReport.Text] != null) { GridView gvExport = new GridView(); gvExport.DataSource = (DataTable)(Session[this.lblTableNameReport.Text]); gvExport.DataBind(); PaginaBase ExportExcel = new PaginaBase(); ExportExcel.ExportarExcel(gvExport, "reporteGlobal"); } }
//metodo para exportar a excel private void ExportarXLS() { try { GridView gvExport = new GridView(); gvExport.AutoGenerateColumns = false; DataControlFieldCollection gvdcfCollection = GridViewRIUF.Columns.CloneFields(); foreach (DataControlField col in gvdcfCollection) { if (col.Visible) { gvExport.Columns.Add(col); } } gvExport.Columns[14].Visible = false; gvExport.Columns[0].Visible = false; gvExport.DataSource = Session[this.lblTableName.Text]; gvExport.DataBind(); PaginaBase ExportExcel = new PaginaBase(); ExportExcel.ExportarExcel(gvExport, "ReporteRIUFArrendamiento"); } catch (Exception ex) { Msj = "Ha ocurrido un error al expoprtar a Excel."; this.LabelInfo.Text = "<div class='alert alert-danger'><strong> Error </strong>" + Msj + "</div>"; BitacoraExcepcion BitacoraExcepcionAplictivo = new BitacoraExcepcion { CadenaconexionBD = System.Configuration.ConfigurationManager.ConnectionStrings["cnArrendamientoInmueble"].ConnectionString, Aplicacion = "ContratosArrto", Modulo = MethodInfo.GetCurrentMethod().DeclaringType.ToString() + ".aspx", Funcion = MethodBase.GetCurrentMethod().Name + "()", DescExcepcion = ex.InnerException == null ? ex.Message : ex.InnerException.Message, Usr = ((SSO)Session["Contexto"]).UserName.ToString() }; BitacoraExcepcionAplictivo.RegistrarBitacoraExcepcion(); BitacoraExcepcionAplictivo = null; } }
//exporta a Excel con todo y formato, como se ve la rejilla private void ExportarXLS() { try { GridView gvExport = new GridView(); gvExport.AutoGenerateColumns = false; DataControlFieldCollection gvdcfCollection = GridViewSolicitudesOpinionEmitidas.Columns.CloneFields(); foreach (DataControlField col in gvdcfCollection) { if (col.Visible) { gvExport.Columns.Add(col); } } gvExport.Columns[10].Visible = false; gvExport.DataSource = Session[this.lblTableName.Text]; gvExport.DataBind(); PaginaBase ExportExcel = new PaginaBase(); ExportExcel.ExportarExcel(gvExport, this.lblTableName.Text); } catch (Exception ex) { Msj = "Ha ocurrido un error al exportar a Excel. Contacta al área de sistemas."; this.LabelInfo.Text = "<div class='alert alert-danger'><strong> Error </strong>" + Msj + "</div>"; MostrarMensajeJavaScript(Msj); BitacoraExcepcion BitacoraExcepcionAplictivo = new BitacoraExcepcion { CadenaconexionBD = System.Configuration.ConfigurationManager.ConnectionStrings["cnArrendamientoInmueble"].ConnectionString, Aplicacion = "ContratosArrto", Modulo = MethodInfo.GetCurrentMethod().DeclaringType.ToString() + ".aspx", Funcion = MethodBase.GetCurrentMethod().Name + "()", DescExcepcion = ex.InnerException == null ? ex.Message : ex.InnerException.Message, Usr = ((SSO)Session["Contexto"]).UserName.ToString() }; BitacoraExcepcionAplictivo.RegistrarBitacoraExcepcion(); BitacoraExcepcionAplictivo = null; } ////quitar paginacion para mandar todo a excel. //GridViewSolicitudesOpinionEmitidas.AllowSorting = false; //GridViewSolicitudesOpinionEmitidas.AllowPaging = false; ////this.GridViewResult.DataSource = Session["lstResultBusqInmueblesPort"] as List<Portafolio>; ////this.GridViewResult.DataBind(); //StringBuilder sb = new StringBuilder(); //StringWriter sw = new StringWriter(sb); //HtmlTextWriter htw = new HtmlTextWriter(sw); //Page page = new Page(); //HtmlForm form = new HtmlForm(); //GridViewSolicitudesOpinionEmitidas.EnableViewState = false; //page.EnableEventValidation = false; ////Page que requieran los diseñadores RAD. //page.DesignerInitialize(); //page.Controls.Add(form); //form.Controls.Add(GridViewSolicitudesOpinionEmitidas); //page.RenderControl(htw); //Response.Clear(); //Response.Buffer = true; //Response.ContentType = "application/vnd.ms-excel"; //Response.AddHeader("Content-Disposition", "attachment;filename=data.xls"); //Response.Charset = "UTF-8"; //Response.ContentEncoding = Encoding.Default; //Response.Write(sb.ToString()); //Response.End(); }