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

Export Excel Biff and column widths

4 Answers 243 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron Gibbs
Top achievements
Rank 2
Aaron Gibbs asked on 17 Dec 2012, 11:47 PM
I'm having a hard time getting column widths looking nice in a RadGrid Excel export to Biff format.  The grid is fairly simple, binding to a dataset with a few DataBound columns.

With no widths set on any of the GridBoundColumns, the grid looks wonderful in HTML (widths set automatically), but the Excel export appears with very small columns (all have width set to zero).

If I manually set widths on each of my GridBoundColumns, again it looks great in HTML but the resulting Excel export has very, very wide columns that run way off the right side of the page.  It appears this has to do with Excel measuring column widths in characters and the RadGrid of course handling column widths with pixels.  For example, if I set my GridBoundColumn.HeaderStyle.Width = 240, in the resulting Excel file I can see the column width set to 180.  Some conversion must be happening here to convert pixels to characters but it's not working right.

Is there any way I can set the Excel columns width equal to the pixel width settings in my RadGrid?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Dec 2012, 05:17 AM
Hi,

Try setting the width as shown below.
C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == RadGrid.ExportToExcelCommandName)
       {
           foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
           {
               if (col.UniqueName == "Uniiquename")
               {
                   col.HeaderStyle.Width = Unit.Pixel(240);
               }
           }
       }
   }

Thanks,
Shinu.
0
Bob
Top achievements
Rank 1
answered on 15 Feb 2013, 09:20 PM
An auto-resize property on radgrid for exporting would be nice...
0
Alanna
Top achievements
Rank 1
answered on 25 Mar 2013, 07:42 PM
I agree that an auto-resize property for the export would be amazing.

Shinu - I have tried your suggestion and it still doesn't seem to work.  The markup for the column in particular sets a width of 300px - the actual grid itself when rendered in the browser looks perfect.  However, when exporting to excel, the column ends up about twice as wide as it needs to be.  Excel is reporting a width of 94.57.  The export seems to ignore anything I try setting it to and ALWAYS ends up giving it the same width in the final export.

When using your suggestion, I set that column (which just contains a string, as well as all the "ExpandColumn"s) to a width of 10 and that has still made no difference at all.

Am I missing something?  Aside from the code snippet that you posted, is there anything else that needs to be done? 

FYI - I followed this example to export my hierarchical grid and then just added in the code you posted to deal with the column width:  Export Hierarchical Grid
0
Kostadin
Telerik team
answered on 28 Mar 2013, 03:30 PM
Hello Alanna,

I am assuming you are using HTML format and not Biff format in your project. In this case the width you set is in your mark up is the same as the one in the exported file. The problem is that the grid column widths is different than the excel column widths. More information could be found at the following article.

Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Aaron Gibbs
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Bob
Top achievements
Rank 1
Alanna
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or