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

Set column width on Excel Biff Export

1 Answer 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle Smith
Top achievements
Rank 1
Kyle Smith asked on 07 Aug 2012, 06:24 PM
When exporting the radgrid using Biff Excel format, the resulting excel file has columns that are far too wide for the data they contain. I am confused because the Biff export demo seems to work just fine. The only major difference between my grid and the grid used in the demo is that I am not auto-generating my columns. Here is an example definition of one of my columns:

<telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="First Name"
                    AutoPostBackOnFilter="True" ShowFilterIcon="False" FilterControlWidth="60px">
                    <ItemStyle Width="80px"/>
                    <HeaderStyle Width="80px"/>
</telerik:GridBoundColumn>

I have attached a screenshot of the resulting excel file's extremely wide columns.

Am I doing something wrong? Should I not be setting the ItemStyle and HeaderStyle in this manner? Is there a way for me to fix the column widths on export? The BiffExporting event did not seem to have a means of doing so.

1 Answer, 1 is accepted

Sort by
0
Kyle Smith
Top achievements
Rank 1
answered on 07 Aug 2012, 06:49 PM
I believe I have answered my part of my own question. After studying the demo further, I see that the width for the columns is in fact set on the ItemCommand handler shown below. I would still like to know if there is a way to autoset the width based on content.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {
                foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
                {
                    if (col.UniqueName == "ProductName" || col.UniqueName == "QuantityPerUnit")
                    {
                        col.HeaderStyle.Width = Unit.Point(27);
                    }
                    else
                    {
                        col.HeaderStyle.Width = Unit.Point(16);
                    }
                }
 
                RadGrid1.ExportSettings.IgnorePaging = CheckBox1.Checked;
 
                if (CheckBox3.Checked)
                {
                    isExporting = true;
 
                    if (!RadGrid1.ExportSettings.IgnorePaging)
                    {
                        RadGrid1.Rebind();
                    }
                }
            }
        }
Tags
Grid
Asked by
Kyle Smith
Top achievements
Rank 1
Answers by
Kyle Smith
Top achievements
Rank 1
Share this question
or