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

RadGrid export to excel item style background color

1 Answer 560 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 13 Jun 2019, 09:55 PM

When exporting a RadGrid to excel, how do I get the backcolor of my rows to not extend infinitely. My RadGrid has 11 columns but when I export to excel, the backcolor of my alternateitemstyle extends past 11 columns. This is my RadGrid code:

 

<telerik:RadGrid ID="On_Time_Receipt_Summary" runat="server" RenderMode="Lightweight" Height="700px" Width="1300px" ShowFooter="true"
OnCustomAggregate="On_Time_Receipt_Summary_CustomAggregate" OnExportCellFormatting="On_Time_Receipt_Summary_ExportCellFormatting">
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="true" />
<Resizing AllowColumnResize="true" AllowRowResize="true" ClipCellContentOnResize="false" ResizeGridOnColumnResize="true" />
</ClientSettings>
<ExportSettings ExportOnlyData="true" Excel-FileExtension="xls" Excel-Format="ExcelML" FileName="On_Time_Receipt_Summary" OpenInNewWindow="true" UseItemStyles="true"></ExportSettings>
<MasterTableView AutoGenerateColumns="false">
<HeaderStyle BackColor="#839DBE" BorderStyle="Solid" Font-Bold="False" Wrap="False"
ForeColor="White" Font-Names="Arial" Font-Size="8pt" BorderWidth="1px"
Height="50px" />
<ItemStyle BackColor="White" Font-Size="8pt" Font-Names="Arial" ForeColor="Black" Wrap="False" />
<AlternatingItemStyle BackColor="#e1e6ef" Font-Size="8pt" Font-Names="Arial" ForeColor="Black" Wrap="False" />
<FooterStyle BackColor="#839DBE" BorderStyle="Solid" Font-Bold="False" Wrap="False"
ForeColor="White" Font-Names="Arial" Font-Size="8pt" BorderWidth="1px"
Height="25px" />
<Columns>
<telerik:GridBoundColumn DataField="Vendor" HeaderText="Vendor" HeaderStyle-Width="65px" FooterText="Total:"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Vendor Name" HeaderText="Vendor Name" HeaderStyle-Width="225px"></telerik:GridBoundColumn>
<telerik:GridNumericColumn DataField="Total Receipt Lines" HeaderText="Total Receipt Lines" HeaderStyle-Width="50px" DataFormatString="{0:#,##0;}" Aggregate="Sum">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="Total Received Qty" HeaderText="Total Received Qty" HeaderStyle-Width="65px" DataFormatString="{0:#,##0;}" Aggregate="Sum">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="Cost Amount" HeaderText="Cost Amount" HeaderStyle-Width="70px" DataFormatString="{0:$#,##0;}" Aggregate="Sum">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="On Time" HeaderText="On Time" HeaderStyle-Width="50px" DataFormatString="{0:#,##0;}" Aggregate="Sum">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="On Time Pct" HeaderText="On Time Pct" HeaderStyle-Width="50px" UniqueName="OnTimePct" Aggregate="Custom" FooterAggregateFormatString="{0:0%}">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="On Time 90% Fill Rate" HeaderText="On Time 90% Fill Rate" HeaderStyle-Width="65px" DataFormatString="{0:#,##0;}" Aggregate="Sum">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridNumericColumn DataField="On Time 90% Fill Rate Pct" HeaderText="On Time 90% Fill Rate Pct" HeaderStyle-Width="65px" UniqueName="OnTimeFillRatePct" Aggregate="Custom" FooterAggregateFormatString="{0:0%}">
<ItemStyle HorizontalAlign="Right" />
<FooterStyle HorizontalAlign="Right" />
</telerik:GridNumericColumn>
<telerik:GridBoundColumn DataField="Purchaser" HeaderText="Purchaser" HeaderStyle-Width="100px">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Responsibility Center" HeaderText="Responsibility Center" HeaderStyle-Width="100px"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 18 Jun 2019, 12:30 PM
Hi,

You may want to decorate the rows with custom style as shown at https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/export-formats/excelml-export/excelml-basics#custom-styles and https://demos.telerik.com/aspnet-ajax/grid/examples/functionality/exporting/excel-export/defaultcs.aspx:

if (alternateText == "Xlsx" && CheckBox2.Checked)
{
                RadGrid1.MasterTableView.GetColumn("EmployeeID").HeaderStyle.BackColor = Color.LightGray;
                RadGrid1.MasterTableView.GetColumn("EmployeeID").ItemStyle.BackColor = Color.LightGray;
}


Another approach is demonstrated at https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/export-formats/excelml-export/excelml-basics#built-in-styles


protected void RadGrid1_ExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e)
{
    foreach (StyleElement style in e.Styles)
    {
        switch (style.Id)
        {
            case "itemStyle":
                style.FontStyle.Color = System.Drawing.Color.LightBlue;
                break;
            case "alternatingItemStyle":
                style.FontStyle.Color = System.Drawing.Color.Blue;
                break;
        }
    }
}


If you need further assistance, please provide a screenshot of the problem and the aspx/codebehind files for examination.

Regards,
Rumen
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.
Tags
Grid
Asked by
Joseph
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or