Define row height and width when exporting to excel

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

    Posted 01 Jul Link to this post

    Hi. I have a radgridview with some data columns and one image column. I want to set row height and width manually (to fit the images and all data) before exporting to Excel (note: i dont want to use "ExportVisualSettings" property).

    so... what should i do?

  2. Nadya
    Admin
    Nadya avatar
    43 posts

    Posted 02 Jul Link to this post

    Hello Pin,

    This can be achieved by using WorkbookCreated event which allows to introduce some customization before the document is saved to a file. Please refer to the following code snippet:
    private void ExportToExcel_Click(object sender, EventArgs e)
     {
         GridViewSpreadExport spreadExport = new GridViewSpreadExport(this.radGridView1);
         spreadExport.ExportVisualSettings = false;
         SpreadExportRenderer renderer = new SpreadExportRenderer();
         renderer.WorkbookCreated += Renderer_WorkbookCreated;
         spreadExport.RunExport("..\\..\\Exported", renderer);
     }
     
     private void Renderer_WorkbookCreated(object sender, WorkbookCreatedEventArgs e)
     {
         Worksheet worksheet = (Worksheet)e.Workbook.Sheets[0];
         worksheet.Columns[worksheet.UsedCellRange].AutoFitWidth();
         worksheet.Rows[worksheet.UsedCellRange].AutoFitHeight();
     }

    You can find additional information here: https://docs.telerik.com/devtools/winforms/telerik-presentation-framework/export-renderers/spreadexportrenderer

    I hope this information helps. Should you have any other questions, I will be glad to help.

    Regards,
    Nadya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Pin
    Pin avatar
    4 posts
    Member since:
    Jul 2019

    Posted 03 Jul Link to this post

    Hi,

    I tried that but it seems that this method doesn't work on image column... images doesn't fit into cells...

  4. Answer
    Nadya
    Admin
    Nadya avatar
    43 posts

    Posted 04 Jul Link to this post

    Hello Pin,

    Images in the RadSpreadProcessing are represented as floating images in the Worksheet and they are not content of the cell. This is why they are not measured when AutoFitWidth and AutoFitHeight methods are called. You can set the width of image column and the height of rows in the WorkbookCreated event. Please refer to the updated code snippet which result is illustrated in the attached picture:
    private void Renderer_WorkbookCreated(object sender, WorkbookCreatedEventArgs e)
    {
        Worksheet worksheet = (Worksheet)e.Workbook.Sheets[0];
        worksheet.Columns[worksheet.UsedCellRange].AutoFitWidth();
      
        RowSelection rowSelection = worksheet.Rows[worksheet.UsedCellRange.FromIndex.RowIndex + 1, worksheet.UsedCellRange.ToIndex.RowIndex];
        rowSelection.SetHeight(new RowHeight(77, true));
      
        int columnIndex = this.radGridView1.Columns["Image"].Index;
        ColumnSelection columnSelection = worksheet.Columns[columnIndex];
        columnSelection.SetWidth(new ColumnWidth(102, true));
      
        // if you want to resize the images you can use this:
        foreach (var floatingImageShape in worksheet.Shapes)
        {
            floatingImageShape.SetWidth(false, 100);
            floatingImageShape.SetHeight(false, 75);
        }
    }

     I hope this information helps. Should you have any other questions, I will be glad to help.

    Regards,
    Nadya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. Pin
    Pin avatar
    4 posts
    Member since:
    Jul 2019

    Posted 04 Jul Link to this post

    It works great! Thanks a lot...

Back to Top