This is a migrated thread and some comments may be shown as answers.

RadGrid Export to XLSX with GridHyperlinkColumn

3 Answers 380 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Beth
Top achievements
Rank 1
Beth asked on 03 Feb 2020, 03:16 PM

Hello,

I have a RadGrid for which I have enabled exporting to Excel using the XSLX format. The export works very well except the export removes hyperlinks from the grid and replaces them with plain text. I have a requirement to include in the resulting XSLX file the hyperlink that appears in the grid in a GridHyperlinkColumn. I have tried altering the ExportOnlyData value in the grid's export settings, but that causes the data in the GridHyperlinkColumn to not appear at all. Here is a simplified grid. Thank you for your advice on whether XSLX exports can include hyperlinks.

  <telerik:RadGrid ID="rgInstSearchResults" runat="server" RenderMode="Lightweight"
        DataSourceID="odsInstSearch" AutoGenerateColumns="false" Skin="Bootstrap"
        ShowStatusBar="true" AllowMultiRowSelection="true" Width="95%" GridLines="Both"
        AllowFilteringByColumn="false">
        <ClientSettings>
            <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true" />
        </ClientSettings>
        <ExportSettings ExportOnlyData="true" OpenInNewWindow="true" FileName="InstitutionSearchResults" IgnorePaging="true">
            <Excel DefaultCellAlignment="Left" Format="Xlsx" />
            <Word Format="Html" />
        </ExportSettings>
        <MasterTableView Name="mtvInstSearchResults" DataSourceID="odsInstSearch"
            DataKeyNames="InstitutionID" CommandItemDisplay="Top" AllowPaging="true"
            AllowSorting="true" PageSize="50" AllowMultiColumnSorting="true" AllowAutomaticDeletes="true"
            ShowFooter="True" AllowAutomaticInserts="False" AllowAutomaticUpdates="False">
            <CommandItemStyle BackColor="#dae0ed" Font-Bold="true" HorizontalAlign="Right" />
            <CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="true"
                ShowExportToWordButton="true" />
            <PagerStyle AlwaysVisible="true" PageSizes="25,50,100,500" />
            <Columns>
                <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" Reorderable="False" Exportable="false" />
                <telerik:GridHyperLinkColumn UniqueName="InstitutionID" HeaderText="ID"
                    DataNavigateUrlFields="InstitutionID" Reorderable="False"
                    DataNavigateUrlFormatString="ViewInstitution.aspx?InstitutionID={0}"
                    DataTextField="InstitutionID" Target="_blank" />
</Columns>
  </MasterTableView>
    </telerik:RadGrid>

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 06 Feb 2020, 02:46 PM

Hi Beth,

 

You can achieve this requirement using the following approach:

<ExportSettings ExportOnlyData="true">
C#:

    protected void RadGrid1_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
    {
        Telerik.Web.UI.ExportInfrastructure.Table table = e.ExportStructure.Tables[0];
        for (int col = 0; col < RadGrid1.Columns.Count; col++)
        {
            if (RadGrid1.Columns[col].UniqueName == "InstitutionID")
            {
                for (int row = 1; row <= table.Rows.Count; row++)
                {
                    table.Cells[col + 1, row].Hyperlink = "https://en.wikipedia.org";
                }
                break;
            }
        }
    }

I hope this will prove helpful.

 

Regards,
Eyup
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
0
Beth
Top achievements
Rank 1
answered on 14 Feb 2020, 03:27 PM

This worked perfectly! Thank you very much. 

How would this same task work when exporting to Word (HTML)? 

0
Beth
Top achievements
Rank 1
answered on 14 Feb 2020, 04:02 PM

Please disregard the question, I figured out how to do it. For anyone looking at this question, I used the radgrid's ExportCellFormatting event:

    Private Sub myGrid_ExportCellFormatting(sender As Object, e As ExportCellFormattingEventArgs) Handles myGrid.ExportCellFormatting
        Dim myItem As GridDataItem = e.Cell.Parent
        Dim currInst As Institution = myItem.DataItem
        Dim myColumn As GridColumn = e.FormattedColumn
        If myColumn.UniqueName = "InstitutionID" Then
            Dim url As String = SystemConfig.baseURL + "Institutions/ViewInstitution.aspx?InstitutionID=" + currInst.InstitutionID.ToString()
            e.Cell.Text = "<a href='" + url + "'>" + currInst.InstitutionID.ToString() + "</a>"
        End If
    End Sub

 

 

Tags
Grid
Asked by
Beth
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Beth
Top achievements
Rank 1
Share this question
or