What am I missing because when my gridview exports the Area (DataTextField) column still has the links although I have a function to remove the links:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" EnableModelValidation="True" Width="856px" CaptionAlign="Top" Height="126px" BackColor="White" BorderColor="#DCBE68" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" HorizontalAlign="Center" CellSpacing="2" Font-Names="Arial" AllowSorting="True"> <Columns> <asp:HyperLinkField DataTextField="area" NavigateUrl="~/BCTU.aspx" HeaderText="Region" SortExpression="area" Text="BCTU" > <HeaderStyle VerticalAlign="Top" /> </asp:HyperLinkField> <asp:BoundField DataField="reg" HeaderText="reg" SortExpression="reg" ReadOnly="True" Visible="False" HtmlEncode="false" /> <asp:BoundField DataField="RegionAcronym" HeaderText="RegionAcronym" ReadOnly="True" SortExpression="RegionAcronym" Visible="False" HtmlEncode="false"/> <asp:BoundField DataField="area" HeaderText="area" SortExpression="area" ReadOnly="True" Visible="False" /> <asp:BoundField DataField="totpenfodds" HeaderText="Total Pending" SortExpression="totpenfodds" ReadOnly="True" DataFormatString="{0:N0}" HtmlEncode="false" > <HeaderStyle VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="pendinfo" HeaderText="Pending In FO" ReadOnly="True" SortExpression="pendinfo" DataFormatString="{0:N0}" HtmlEncode="false"> <HeaderStyle VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="pendndds" HeaderText="Pending In DDS" SortExpression="pendndds" ReadOnly="True" DataFormatString="{0:N0}" HtmlEncode="false"> <HeaderStyle VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="pendfo300" HeaderText="Pend in FO > <br>300 Days<br>" ReadOnly="True" SortExpression="pendfo300" DataFormatString="{0:N0}" HtmlEncode="false" > </asp:BoundField> <asp:BoundField DataField="penddds300" HeaderText="Pend In DDS > <br>300 Days</br>" ReadOnly="True" SortExpression="penddds300" DataFormatString="{0:N0}" HtmlEncode="false" > </asp:BoundField> <asp:BoundField DataField="pend250" HeaderText="FO & DDS <br>Pend 250 - 300</br>" ReadOnly="True" SortExpression="pend250" DataFormatString="{0:N0}" HtmlEncode="false"> </asp:BoundField> <asp:BoundField DataField="pend300" HeaderText="FO & DDS <br>Pend 301 - 350</br>" ReadOnly="True" SortExpression="pend300" DataFormatString="{0:N0}" HtmlEncode="false" > </asp:BoundField> <asp:BoundField DataField="pend351" HeaderText="FO & DDS <br>Pend 351 - 400</br>" ReadOnly="True" SortExpression="pend351" DataFormatString="{0:N0}" HtmlEncode="false" > </asp:BoundField> <asp:BoundField DataField="pend400" HeaderText="FO & DDS <br>Pend > 400</br>" ReadOnly="True" SortExpression="pend400" DataFormatString="{0:N0}" HtmlEncode="false" > </asp:BoundField> </Columns> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" VerticalAlign="Top" Font-Size="Medium" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <RowStyle BackColor="White" BorderStyle="Double" ForeColor="#330099" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> </asp:GridView>
Protected Sub BtnExportExcel_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Buffer = True Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls") Response.Charset = "" Response.ContentType = "application/vnd.ms-excel" Dim sw As New IO.StringWriter() Dim hw As New HtmlTextWriter(sw) GridView2.AllowPaging = False GridView2.DataBind() GridView2.Caption = "T16 Region Pending Claims Listings" GridView2.HeaderRow.Attributes.Add("style", "font-size:10px") For i As Integer = 0 To GridView2.HeaderRow.Cells.Count - 1 GridView2.HeaderRow.Cells(i).Style.Add("background-color", "#B43636") 'GridView1.HeaderRow.Cells(i).Style.Add("ss:WrapText", "1") 'GridView1.HeaderRow.Attributes.Add("ss:StyleID", "customHeader") Next For i As Integer = 0 To GridView2.Rows.Count - 1 Dim row As GridViewRow = GridView1.Rows(i) 'Change Color back to white row.BackColor = System.Drawing.Color.White 'Apply text style to each Row row.Attributes.Add("class", "textmode") Next 'Remove Controls Me.RemoveControls(GridView2) Response.Write(AddExcelStyling()) GridView2.RenderControl(hw) 'style to format numbers to string Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>" Response.Write(style) Response.Output.Write(sw.ToString()) Response.Flush() Response.[End]() End Sub
Private Function AddExcelStyling() As String Dim sb As New StringBuilder() sb.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office'" & vbLf & "xmlns:x='urn:schemas-microsoft-com:office:excel'" & vbLf & "xmlns='http://www.w3.org/TR/REC-html40'>" & vbLf & "<head>" & vbLf) sb.Append("<style ss:ID='customHeader'>>" & vbLf) sb.Append("<Alignment ss:Vertical='Bottom' ss:WrapText='1'/>") sb.Append("</style>" & vbLf) sb.Append("<style>" & vbLf) sb.Append("@page") sb.Append("{margin:.0in .0in .0in .0in;" & vbLf) sb.Append("mso-header-margin:.5in;" & vbLf) sb.Append("mso-footer-margin:.5in;" & vbLf) sb.Append("mso-page-orientation:landscape;}" & vbLf) sb.Append("</style>" & vbLf) sb.Append("<!--[if gte mso 9]><xml>" & vbLf) sb.Append("<x:ExcelWorkbook>" & vbLf) sb.Append("<x:ExcelWorksheets>" & vbLf) sb.Append("<x:ExcelWorksheet>" & vbLf) sb.Append("<x:Name>Projects 3 </x:Name>" & vbLf) sb.Append("<x:WorksheetOptions>" & vbLf) sb.Append("<x:Print>" & vbLf) sb.Append("<x:ValidPrinterInfo/>" & vbLf) sb.Append("<x:PaperSizeIndex>9</x:PaperSizeIndex>" & vbLf) sb.Append("<x:HorizontalResolution>600</x:HorizontalResolution" & vbLf) sb.Append("<x:VerticalResolution>600</x:VerticalResolution" & vbLf) sb.Append("</x:Print>" & vbLf) sb.Append("<x:Selected/>" & vbLf) sb.Append("<x:DoNotDisplayGridlines/>" & vbLf) sb.Append("<x:ProtectContents>False</x:ProtectContents>" & vbLf) sb.Append("<x:ProtectObjects>False</x:ProtectObjects>" & vbLf) sb.Append("<x:ProtectScenarios>False</x:ProtectScenarios>" & vbLf) sb.Append("</x:WorksheetOptions>" & vbLf) sb.Append("</x:ExcelWorksheet>" & vbLf) sb.Append("</x:ExcelWorksheets>" & vbLf) sb.Append("<x:WindowHeight>12780</x:WindowHeight>" & vbLf) sb.Append("<x:WindowWidth>19035</x:WindowWidth>" & vbLf) sb.Append("<x:WindowTopX>0</x:WindowTopX>" & vbLf) sb.Append("<x:WindowTopY>15</x:WindowTopY>" & vbLf) sb.Append("<x:ProtectStructure>False</x:ProtectStructure>" & vbLf) sb.Append("<x:ProtectWindows>False</x:ProtectWindows>" & vbLf) sb.Append("</x:ExcelWorkbook>" & vbLf) sb.Append("</xml><![endif]-->" & vbLf) sb.Append("</head>" & vbLf) sb.Append("<body>" & vbLf) Return sb.ToString() End Function
Private Sub RemoveControls(ByVal grid As Control) Dim literal As New Literal() For i As Integer = 0 To grid.Controls.Count - 1 If TypeOf grid.Controls(i) Is LinkButton Then literal.Text = TryCast(grid.Controls(i), LinkButton).Text grid.Controls.Remove(grid.Controls(i)) grid.Controls.AddAt(i, literal) ElseIf TypeOf grid.Controls(i) Is DropDownList Then literal.Text = TryCast(grid.Controls(i), DropDownList).SelectedItem.Text grid.Controls.Remove(grid.Controls(i)) grid.Controls.AddAt(i, literal) ElseIf TypeOf grid.Controls(i) Is CheckBox Then literal.Text = If(TryCast(grid.Controls(i), CheckBox).Checked, "True", "False") grid.Controls.Remove(grid.Controls(i)) grid.Controls.AddAt(i, literal) ElseIf TypeOf grid.Controls(i) Is HyperLink Then literal.Text = TryCast(grid.Controls(i), HyperLink).Text grid.Controls.Remove(grid.Controls(i)) grid.Controls.AddAt(i, literal) ElseIf TypeOf grid.Controls(i) Is Button Then literal.Text = TryCast(grid.Controls(i), Button).Text grid.Controls.Remove(grid.Controls(i)) grid.Controls.AddAt(i, literal) End If If grid.Controls(i).HasControls() Then RemoveControls(grid.Controls(i)) End If Next End Sub