Export to Excel

6 posts, 0 answers
  1. bkagan98
    bkagan98 avatar
    16 posts
    Member since:
    Feb 2011

    Posted 03 Jun 2011 Link to this post

    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.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 06 Jun 2011 Link to this post

    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
  3. DevCraft banner
  4. bkagan98
    bkagan98 avatar
    16 posts
    Member since:
    Feb 2011

    Posted 06 Jun 2011 Link to this post

    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.
  5. bkagan98
    bkagan98 avatar
    16 posts
    Member since:
    Feb 2011

    Posted 06 Jun 2011 Link to this post

    That worked. One more question: would it be possible to show gridlines in generated XLS file? If yes, would you please tell how? Thanks.
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 07 Jun 2011 Link to this post

    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
  7. Prithvi
    Prithvi avatar
    19 posts
    Member since:
    Jul 2012

    Posted 08 Mar 2012 Link to this post

    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
Back to Top
DevCraft banner