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

Adjust width on excel export

1 Answer 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Billy
Top achievements
Rank 2
Billy asked on 14 Apr 2014, 06:06 PM
When I export to excel the width is always set to 100%. This can be seen by exporting to excel and opening the file in notepad. For example, notice the 100% below:

<table rules="all" border="1" id="ctl05_grdBudget_ctl00" style="width:100%;table-layout:auto;empty-cells:show;">

Is there a way to prevent this? When I set the width in the NeedDataSource handler the grid displays on the page with the set width. However, this does not persist when exported to excel. I have tried setting the width prior to calling MasterTableView.ExportToExcel however this does not seem to make a difference as it is always 100% for the export.

From what I have seen, the only way around this is to set the width of each column which I do not want to do. I want to have the grid auto size when exported. If you edit the exported file and remove width:100% then open it in Excel, it will display the way I would like it to.

The reason I am asking is because the columns are too wide to fit on a single page when you go to print in Excel. Below is the code I am using if it is of any help.

    <%--Button to perform the export--%>
    <asp:LinkButton runat="server" ID="lnkExport" Text="Export"></asp:LinkButton>
 
    <%--The grid itseld--%>
    <telerik:RadGrid ID="grdTest" runat="server">
        <MasterTableView AutoGenerateColumns="false">
            <Columns>
                <telerik:GridBoundColumn SortExpression="Name" HeaderText="Supervisor" DataField="Name" />
                <telerik:GridBoundColumn SortExpression="Unconfirmed" HeaderText="Unconfirmed" DataField="Unconfirmed" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
 
Private Sub grdTest_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdTest.NeedDataSource
    Me.grdTest.DataSource = New List(Of Object) From {New With {.Name = "Name 1", .Unconfirmed = 5},
                                                      New With {.Name = "Name 2", .Unconfirmed = 3}}
 
    'manually setting the width, this displays as it should on the page
    Me.grdTest.Width = Unit.Pixel(500)
End Sub
 
Private Sub lnkExport_Click(sender As Object, e As EventArgs) Handles lnkExport.Click
    Me.grdTest.MasterTableView.ExportToExcel()
End Sub

1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 16 Apr 2014, 08:47 AM
Hello Billy,

A possible way to remove the width would be to handle the GridExporting event where you can get the output and get rid of that property using string/regex operations.
Let me know if you need more information.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Billy
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Share this question
or