Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/09/2004, 16:31
miguel_20042004
 
Fecha de Ingreso: agosto-2004
Mensajes: 23
Antigüedad: 19 años, 8 meses
Puntos: 0
problemas para expostar a excel desde Datagrid

Buenas.
Por favor las disculpas si es muy extenso el codigo de este menasje ....
estoy tratando de exportar a excel un datagrid...con este codigo..
------------------------------------------------------------------
ToExcel.aspx

<form id="Form1" method="post" runat="server">
<asp:datagrid id="Datos" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 56px" runat="server"
Font-Size="XX-Small" Font-Names="Arial" BorderColor="#DEDFDE" HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="LightGray" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Vertical" ForeColor="Black" Font-Bold="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:datagrid><asp:button id="btnExport" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 192px"
runat="server" Text="Export to Excel"></asp:button>&nbsp;
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 440px; POSITION: absolute; TOP: 72px" runat="server"
Text="Button"></asp:Button></form>
------------------------------------------------------------------
ToExcel.aspx.vb

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
cmpDataGridToExcel.DataGridToExcel(Datos, Response)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Datos.Visible = True
Dim dt As New DataTable
Dim dcName As New DataColumn("Nombre", GetType(String))
Dim dcAge As New DataColumn("Age", GetType(Integer))
dt.Columns.Add(dcName)
dt.Columns.Add(dcAge)
Dim dr As DataRow
dr = dt.NewRow()
dr("Nombre") = "Scott"
dr("Age") = 25
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("Nombre") = "Jisun"
dr("Age") = 24
dt.Rows.Add(dr)
Datos.DataSource = dt
Datos.DataBind()
End Sub
------------------------------
cmpDataGridToExcel.vb

Public Class cmpDataGridToExcel
Inherits System.ComponentModel.Component

Public Shared Sub DataGridToExcel(ByVal dgExport As DataGrid, ByVal response As HttpResponse)
response.Clear()
response.Charset = ""
response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim dg As New DataGrid()
dg = dgExport
dg.GridLines = GridLines.None
dg.HeaderStyle.Font.Bold = True
dg.DataBind()
dg.RenderControl(htmlWrite)
response.Write(stringWrite.ToString)
response.End()
End Sub
End Class
-------------------------------------------------------------
ORIGINALMETE el codigo de Button1_Click venia en Page_Load y ahi funciona perfectamente pero debido a mis necesidades el datagrid debe ser construido no al momento de cargar la página.
Haciendo un Debug con VB.Net detecto q la variable stringWrite no contiene dato alguno..pero no se como solucionar este problema.
Desde ya Muchas Gracias a quien pueda ayudarme.