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

Export to Excel

5 Answers 103 Views
GridView
This is a migrated thread and some comments may be shown as answers.
bkagan98
Top achievements
Rank 1
bkagan98 asked on 03 Jun 2011, 06:48 AM
When I cancel the grid_ElementExporting event for all cells in the row, but not for the row itself, it leaves an empty record in Excel file. Is there any way to eliminate these empty records? Please advise. Thanks.

grid_ElementExporting()
private void grdDataView_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
    if (e.Element == ExportElement.HeaderCell || e.Element == ExportElement.Cell)
    e.Cancel = true;
}

grid_ElementExported()
private void grdDataView_ElementExported(object sender, GridViewElementExportedEventArgs e)
{
    if (e.Element == ExportElement.HeaderRow)
    {
        e.Writer.Write(String.Format(@"<td>{0}</td>", "Plan Code"));
    }
  
    if (e.Element == ExportElement.Row)
    {
        JobDetailModel obj = e.Context as JobDetailModel;
        e.Writer.Write(String.Format(@"<td>{0}</td>", obj.PlanCode));
    }
}

resulting Excel file - see attached picture.

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 06 Jun 2011, 08:46 AM
Hi Boris,

 You have empty records as you do not exclude them when exporting. That is why the ElementExported event is fired for these rows. If you want to remove these rows from the result excel file, you should remove all empty rows in the output stream, like:

var strm = new MemoryStream();
                    this.clubsGrid.Export(strm, exportOptions);
                    var reader = new StreamReader(strm);
                    strm.Position = 0;
                    var text = reader.ReadToEnd();
                    text = text.Replace("<tr>\r\n</tr>", "");
                    var writer = new StreamWriter(stream);
                    writer.Write(text);
                    writer.Flush();

I have prepared a sample project, showing this. You may review how it is working.

Regards,
Didie
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
bkagan98
Top achievements
Rank 1
answered on 06 Jun 2011, 04:54 PM
Hi Didie,

My data grid contains hundreds of thousands records and if I do everything in the memory stream, will it affect the performance?

Thanks.
0
bkagan98
Top achievements
Rank 1
answered on 06 Jun 2011, 11:20 PM
That worked. One more question: would it be possible to show gridlines in generated XLS file? If yes, would you please tell how? Thanks.
0
Dimitrina
Telerik team
answered on 07 Jun 2011, 02:43 PM
Hi Boris,

Unfortunately showing gridlines in generated export XLS file would not be possible.

If you have any further questions, please do not hesitate to contact us.

Greetings,
Didie
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Prithvi
Top achievements
Rank 1
answered on 08 Mar 2012, 04:03 PM
Hi,
Am very new to telerick control. Please let me know how to export data from radGridview to Excel (SILVERLIGHT). Please help me

While esporting the data if there is date field/combo box field in RadGrid, that data is not getting export to Excel instead it is showing empty cell(data missing ).

Thanks in advance. please help me.


Prithvi
Tags
GridView
Asked by
bkagan98
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
bkagan98
Top achievements
Rank 1
Prithvi
Top achievements
Rank 1
Share this question
or