Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
103
How to Remove Hyperlinks while exporting grid cells
posted

Hi,

I have a webdatagrid and i have a column of type LinkButton that. I have written some code to give URLs to Linkbutton in InitializeRow of event.

Now when i am using WebExcelExporter, i am not able to remove these hyperlinks in the exported Excel. How can i do this without loosing theme and formatting.

Thanks in advance.

 

Parents
No Data
Reply
  • 103
    posted

    Hi,

    I could able to figure out the solution for removing Hyperlinks in the template columns. 

    Here is the code which will remove Hyperlinks while downloading into excel.

     protected void urlDownloads_Click(object sender, System.EventArgs e)
            { 
                try
                {

                    uwgCostCenterAdmin.Behaviors.Paging.Enabled = false;
                    BindGrid();

                    uwgCostCenterAdmin.Columns[4].Hidden = true;
                    WebExcelExporter1.DownloadName = "CostCenter";
                    this.WebExcelExporter1.Export(uwgCostCenterAdmin);
                    this.WebExcelExporter1.EnableStylesExport = true;
                    uwgCostCenterAdmin.Behaviors.Paging.Enabled = true;
                    BindGrid();
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }           

            private int GetIndex(string text)
            {
                for (int i = uwgCostCenterAdmin.Columns.Count - 1; i >= 0; i--)
                {
                    if (uwgCostCenterAdmin.Columns[i].Header.Text == text)
                        return i;
                }
                return 0;
            }

            protected void WebExcelExporter1_CellExporting(object sender, ExcelCellExportingEventArgs e)
            {
                if (!e.IsHeaderCell && !e.IsFooterCell && !e.IsSummaryCell)
                {
                    int index = GetIndex(System.Convert.ToString(e.Worksheet.Rows[0].Cells[e.CurrentColumnIndex].Value));
                    GridRecordItem item = uwgCostCenterAdmin.Rows[e.CurrentRowIndex - 1].Items[index];

                    if (item.Column.GetType().Name == "TemplateDataField")
                    {
                        TemplateDataField field = item.Column as TemplateDataField;
                        HyperLink costcenterLink = item.FindControl("costcenterLink") as HyperLink;
                        if (costcenterLink != null)
                        {
                            e.WorksheetCell.Value = costcenterLink.Text;
                            e.Cancel = true;
                        }

                    }
                    else
                    {                
                        e.WorksheetCell.Value = item.Text;
                        e.Cancel = true;
                    }
                }
            }

    Hope this helps.

     

Children
No Data