Export RadGridview to Excel with variable image sizes (adjusting cell sizes)

4 posts, 0 answers
  1. Pin
    Pin avatar
    9 posts
    Member since:
    Jul 2019

    Posted 10 Sep Link to this post

    Hi

    I have a RadGridview that contains some data columns and some image columns and images are in different sizes (refer to attached image). I want to export it to excel. what is the best way to adjust row height and column width (based on image sizes)?

    Note1: I think that i should put some conditions on "ExcelexportRenderer_WorkbookCreated" event but can't find the way.

    Note2: My images are in binary format ("System.Byte[]").

  2. Pin
    Pin avatar
    9 posts
    Member since:
    Jul 2019

    Posted 11 Sep in reply to Pin Link to this post

    UPDATE:  It seems that using "ExportVisualSettings = true" is a good solution in ordinary tables (without image column) but unfortunately NOT works correctly on tables with grouped data (and image columns). The location of images (relative to their cells) are not calculated correctly in excel when there is some grouping...

    What should i do?

     

    Thanks

  3. Todor Vyagov
    Admin
    Todor Vyagov avatar
    44 posts

    Posted 15 Sep Link to this post

    Hello Pin,

    As you noticed the ExportVisualSettings property of GridViewSpreadExport controls whether the visual settings and sizes are exported to the excel workbook.

    Thanks for the detailed description, which helped me reproduce the problem you are facing. When the image column is exported in a non-grouped grid - the alignment of images in excel cells is correct, but when the grid is grouped the image positioning is not correct. I have logged the issue on our feedback portal, here: RadGridView: Export to excel does not position images correctly when grid is grouped. I have also updated your Telerik points for the report.

    As a workaround, if you need to handle the event, you can subscribe to the WorkbookCreated event of SpreadExportRenderer and adjust the position of the image, which is illustrated in the code below:

    private void Renderer_WorkbookCreated(object sender, WorkbookCreatedEventArgs e)
    {
        if (this.gridView.GroupDescriptors.Count == 0)
        {
            return;
        }
    
        foreach (var imageShape in e.Workbook.ActiveWorksheet.Shapes)
        {
            double cellWidth = e.Workbook.ActiveWorksheet.Columns[imageShape.CellIndex.ColumnIndex].GetWidth().Value.Value;
            double imageWidth = imageShape.Width;
            
            imageShape.OffsetX = (cellWidth - imageWidth) / 2;
        }
    }
    

    Here is the result on my end after the applied workaround:


    I hope this helps. Let me know if you need further assistance.

    Regards,
    Todor Vyagov
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

  4. Pin
    Pin avatar
    9 posts
    Member since:
    Jul 2019

    Posted 18 Sep in reply to Todor Vyagov Link to this post

    Thanks so much
Back to Top