This project has moved. For the latest updates, please go here.

Displaying the DataTextField of a Hyperlink in a gridview when Using ClosedXML to create Excel Spreadsheet

Nov 21, 2013 at 1:27 PM
I have a Gridview that contains a Hyperlink field, and I want to display the DataTextField of the Hyperlink in the Excel spreadsheet that I create using ClosedXML. However when I use the code below the field is blank. Is there anyway of seeing this field.

Dim workbook As New XLWorkbook
    'Create table
    Dim dt As New DataTable

    'Table Name
    dt.TableName = "Table1"

    'No AllowPaging
    GridView1.AllowPaging = False

    'Add columns to DataTable

    Dim i As Integer
    'Since we are using a gridview we need to add column names.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Not GridView1.HeaderRow Is Nothing Then
        For i = 0 To GridView1.HeaderRow.Cells.Count - 1
            Select Case i
                Case 0
                    dt.Columns.Add("Inspection Date")
                Case 1
                    dt.Columns.Add("Inspector")
                Case 2
                    dt.Columns.Add("InspectorLB")
                Case 3
                    dt.Columns.Add("Completed")
                Case 4
                    dt.Columns.Add("Visual")
                Case 5
                    dt.Columns.Add("Dimensional")
                Case 6
                    dt.Columns.Add("Set_Ups")
                Case 7
                    dt.Columns.Add("Floor_Checks")
                Case 8
                    dt.Columns.Add("Avg_Time")


            End Select

        Next
    End If

    'Add each data rows to table

    Dim row As GridViewRow
    Dim dr As DataRow

    For Each row In GridView1.Rows

        dr = dt.NewRow()
'here is where the text is being pulled for each row in the gridview
        For i = 0 To row.Cells.Count - 1
        If Not IsDBNull(row.Cells(i).Text) Then
                dr(i) = row.Cells(i).Text.Replace(" ", "")
            End If

        Next
        dt.Rows.Add(dr)
    Next

    'Add footer row to table

    If Not GridView1.FooterRow Is Nothing Then

        dr = dt.NewRow()

        For i = 0 To GridView1.FooterRow.Cells.Count - 1
            dr(i) = GridView1.FooterRow.Cells(i).Text.Replace(" ", "")

        Next
        dt.Rows.Add(dr)
    End If

    'Add DataTable as a worksheet but you must declare a worksheet so that you can then access the properties.
    Dim ws = workbook.Worksheets.Add(dt)

    ws.PageSetup.Header.Center.AddText("Inspector Summary Report").FontSize = 18
    ws.PageSetup.SetRowsToRepeatAtTop(1, 1)







    ws.PageSetup.Scale = 65

    ws.PageSetup.CenterHorizontally = True
    ws.Range(1, 1, 15, 9).Style.Alignment.Horizontal = XLAlignmentVerticalValues.Center
    ws.Range(1, 1, 1, 9).Style.Font.Bold = True
    ws.Range(1, 1, 1, 9).Style.Alignment.WrapText = True
    ws.Range(1, 1, 1, 9).Style.Font.FontSize = 12
    ws.Range(2, 1, 15, 9).Style.Font.FontSize = 12



    ' ws.PageSetup.FitToPages(1, 1)

    'Create Response
    Dim httpresponse = Response

    'Prepare Response
    httpresponse.Clear()
    httpresponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    httpresponse.AddHeader("content-disposition", "attachment;filename=" + fileName)

    'Flush the workbook to the Response.OutputStream
    Using memoryStream As New MemoryStream()
        workbook.SaveAs(memoryStream)
        memoryStream.WriteTo(httpresponse.OutputStream)
        memoryStream.Close()
    End Using
    httpresponse.End()
Thanks

Marc