DownLoad project from this article
Export GridView with TemplateField Column to Excel in ASP.Net
Replace these:
HTML:
<form id="form1" runat="server">
<asp:Panel ID="pnlPerson" runat="server">
<asp:Image ID="imgDemo" runat="server" ImageUrl="F://Users//azim//Downloads//GridView_ExportToPdf//Images//Chrysanthemum.jpg"
Height="100" Width="100" />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:HyperLink ID="lnkCity" runat="server" NavigateUrl="#" Text='<%# Eval("City") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox Control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" Text="RadioButton Control" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="ExportToExcel" />
</form>
C#:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand("SELECT * FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Protected Sub ExportToExcel(sender As Object, e As EventArgs)
Using sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
'To Export all pages
GridView1.AllowPaging = False
Me.BindGrid()
GridView1.HeaderRow.BackColor = System.Drawing.Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
row.BackColor = System.Drawing.Color.White
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Dim controls As New List(Of Control)()
'Add controls to be removed to Generic List
For Each control As Control In cell.Controls
controls.Add(control)
Next
'Loop through the controls to be removed and replace then with Literal
For Each control As Control In controls
Select Case control.[GetType]().Name
Case "HyperLink"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, HyperLink).Text _
})
Exit Select
Case "TextBox"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, TextBox).Text _
})
Exit Select
Case "LinkButton"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, LinkButton).Text _
})
Exit Select
Case "CheckBox"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, CheckBox).Text _
})
Exit Select
Case "RadioButton"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, RadioButton).Text _
})
Exit Select
End Select
cell.Controls.Remove(control)
Next
Next
Next
pnlPerson.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 0.0F, 0.0F, 0.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
Image:
I have try to export Panel which contains Image and GridView so got the image but its Over lapping.
If you want to do it with Repeater then Add your logo in table
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Image
</td>
</tr>
<asp:Repeater ID="rpt_Demo" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" Text="your text" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
Add this in Panel and Export that in Pdf.
Thank You.