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

Define row height and width when exporting to excel

4 Answers 192 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pin
Top achievements
Rank 1
Pin asked on 01 Jul 2019, 04:51 PM

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?

4 Answers, 1 is accepted

Sort by
0
Nadya
Telerik team
answered on 03 Jul 2019, 04:11 AM
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.
0
Pin
Top achievements
Rank 1
answered on 03 Jul 2019, 10:57 AM

Hi,

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

0
Accepted
Nadya
Telerik team
answered on 04 Jul 2019, 07:31 AM
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.
0
Pin
Top achievements
Rank 1
answered on 04 Jul 2019, 04:02 PM

It works great! Thanks a lot...

Tags
GridView
Asked by
Pin
Top achievements
Rank 1
Answers by
Nadya
Telerik team
Pin
Top achievements
Rank 1
Share this question
or