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

Export to excel and replace a char in a column

1 Answer 423 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 09 Apr 2014, 09:08 AM
I use this snippet to export to excel all the data in RadGrid, viable and not visible, with some columns filtered out:

protected void ExpExcel_Click(object sender, EventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
    {
        if (col.UniqueName.Contains("notes") || (col.UniqueName.Contains("EditCommandColumn")) ||
            (col.UniqueName.Contains("column1")))
        {
            col.Display = false;
        }
        else
        {
            col.Display = true;
        }
    }
    foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
        item.Visible = false;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.MasterTableView.ExportToExcel();       
}
It is working fine. My problem is with two columns where the values are exported with the formatting "." and "$" that I do not won't to be exported. Is there a way to replace those two chars with "" or to avoid the formatting to get exported?

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Apr 2014, 01:01 PM
Hi Felice,

You can attach the OnExportCellFormatting event of RadGrid which will fire while exporting. There you can replace the item text and export as follows.

C#:
protected void RadGrid1_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
    GridDataItem item = e.Cell.Parent as GridDataItem;
    item["Cityname"].Text = item["Cityname"].Text.Replace(".", "").Replace("$", "");
}

Thanks,
Princy.
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or