Usuario
Como crear un PDF desde GRIDVIEW ASP.NET

Pregunta
-
Lo que quiero hacer es reemplazar este codigo que me genera el GRIDVIEW A EXCEL AHORA QUIERO QUE ME LO GENERE A PDF
Actualmente uso este codigo en mi html
<html> <head> </head> <body> <asp:GridView ID="GridView2" CssClass="Grid filtrar " Width="100%" runat="server" AutoGenerateColumns="False" DataKeyNames="IDEnvios" DataSourceID="SqlDataSourceEnvios" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" > <Columns> <asp:CommandField HeaderText="" ShowSelectButton="False" /> <asp:BoundField DataField="NGuia" HeaderText="N° Guia" SortExpression="NGuia" /> <asp:BoundField DataField="Documento" HeaderText="Doc" SortExpression="Documento" /> <asp:BoundField DataField="CIRIf" HeaderText="Codigo -CI-RIF" SortExpression="CIRIf" /> <asp:BoundField DataField="Destinatario" HeaderText="Destinatario" SortExpression="Destinatario" /> <asp:BoundField DataField="DireciconLlegada" HeaderText="DirecciónLlegada" SortExpression="DireciconLlegada" /> <asp:BoundField DataField="Status" HeaderText="Estado" SortExpression="Status" /> <asp:BoundField DataField="Motivo" HeaderText="Motivo" SortExpression="Motivo" /> <asp:BoundField DataField="FechaRegistr" HeaderText="FechaRegistro" SortExpression="FechaRegistr" /> <asp:BoundField DataField="NFactura" HeaderText="NFactura" SortExpression="NFactura" /> <asp:BoundField DataField="Telefono" HeaderText="Teléfono" SortExpression="Telefono" /> <asp:BoundField DataField="Enviadopor" HeaderText="Enviadopor" SortExpression="Enviadopor" /> <asp:BoundField DataField="ReferenciaDeEnvio" HeaderText="ReferenciaDeEnvio" SortExpression="ReferenciaDeEnvio" /> <asp:BoundField DataField="CorreoElectronico" HeaderText="CorreoElectrónico" SortExpression="CorreoElectrónico" /> <asp:BoundField DataField="Tipo" HeaderText="Tipo" SortExpression="Tipo" /> </Columns> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#4B4B4B" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#242121" /> </asp:GridView> <center> <hr /> <asp:Button ID="Button6" CssClass="btn btn-primary" runat="server" Text="Excel" /> </center> </div> </div> </div> </div> </body> </html>
CÓDIGO DEL BOTÓN
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Response.Clear() Response.Buffer = True Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls") Response.Charset = "" Response.ContentType = "application/vnd.ms-excel" Using sw As New StringWriter() Dim hw As New HtmlTextWriter(sw) 'To Export all pages GridView2.AllowPaging = False GridView2.DataBind( GridView2.HeaderRow.BackColor = Color.White For Each cell As TableCell In GridView2.HeaderRow.Cells cell.BackColor = GridView2.HeaderStyle.BackColor Next For Each row As GridViewRow In GridView2.Rows row.BackColor = Color.White For Each cell As TableCell In row.Cells If row.RowIndex Mod 2 = 0 Then cell.BackColor = GridView2.AlternatingRowStyle.BackColor Else cell.BackColor = GridView2.RowStyle.BackColor End If cell.CssClass = "textmode" Next Next GridView2.RenderControl(hw) 'style to format numbers to string Dim style As String = "<style> .textmode { } </style>" Response.Write(style) Response.Output.Write(sw.ToString()) Response.Flush() Response.[End]() End Using End Sub
Todas las respuestas
-
Hola.
Podrías fijarte en el siguiente ejemplo How to export GridView data to PDF file in ASP.Net
Espero te sirva de ayuda.
Saludos.