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

Export to Excel with color on row

3 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
CLAUDE LLOP
Top achievements
Rank 1
CLAUDE LLOP asked on 18 Jul 2012, 01:23 PM
Hello,

I'd like to keep colors of my radgrid's rows on my Excel file. When I execute my export, Excel displays an loading issue of file ("Table" is the cause of error).
Can you help me to resolve this problem, please !

Here my source code :

transaction.aspx :
...
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" GridLines="None"
        AllowFilteringByColumn="True" AllowSorting="True" Skin="Windows7" OnItemDataBound="RadGrid1_ItemDataBound"
        OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged" 
        DataSourceID="odsTransactionsValidees" onprerender="RadGrid1_PreRender"
        OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated" 
        onexcelmlexportrowcreated="RadGrid1_ExcelMLExportRowCreated">
...
<ExportSettings OpenInNewWindow="True" ExportOnlyData="True" FileName="ExportValidees"
            IgnorePaging="true" Excel-Format="ExcelML"  />
....
 <MasterTableView DataSourceID="odsTransactionsValidees" AutoGenerateColumns="False"
            NoMasterRecordsText="Pas de transactions." Font-Size="Medium"  UseAllDataFields="true" DataKeyNames="ID_TRANSACTION,DOUTE_CLASSIFICATION"
            OverrideDataSourceControlSorting="true" AllowNaturalSort="true" CommandItemDisplay="Bottom">
            <CommandItemSettings ExportToExcelText="Export Excel" ShowExportToExcelButton="False"
                ShowAddNewRecordButton="False" ShowRefreshButton="False" />
....
</telerik:RadGrid>
....
transactions.aspx.cs :

protected void RadGrid1_ExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e)
        {
            // ajoute les bordures de cellule
            BorderStylesCollection borders = new BorderStylesCollection();
            BorderStyles borderStyle = null;
 
            for (int i = 1; i <= 4; i++)
            {
                borderStyle = new BorderStyles();
                borderStyle.PositionType = (PositionType)i;
                borderStyle.Color = System.Drawing.Color.Black;
                borderStyle.LineStyle = LineStyle.Continuous;
                borderStyle.Weight = 1.0;
                borders.Add(borderStyle);
            }
            foreach (StyleElement style in e.Styles)
            {
                foreach (BorderStyles border in borders)
                {
                    style.Borders.Add(border);
                }
            }
           
            StyleElement style1 = new StyleElement("MyStyleWhite");
 
            style1.InteriorStyle.Pattern = InteriorPatternType.Solid;
            style1.InteriorStyle.Color = System.Drawing.Color.White;
            e.Styles.Add(style1);
 
            StyleElement style2 = new StyleElement("MyStyleOrange");
            style2.InteriorStyle.Pattern = InteriorPatternType.Solid;
            style2.InteriorStyle.Color = System.Drawing.Color.Orange;
            e.Styles.Add(style2);
 
}

protected void RadGrid1_ExcelMLExportRowCreated(object sender, GridExportExcelMLRowCreatedArgs e)
{
            string typeTransaction = hidType.Value;
 
            if (e.RowType == GridExportExcelMLRowType.DataRow)
            {
                if (typeTransaction == TypeTransactionValide || typeTransaction == TypeTransactionValideReseau)
                {
                    string style;
                    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
                    {
                        string color = item.BackColor.ToString();
                        if (!(bool)item.GetDataKeyValue("DOUTE_CLASSIFICATION"))
                        {
                            style = "myStyleWhite";
                        }
                        else
                        {
                            style = "myStyleOrange";
                        }
                        foreach (CellElement cellElement in e.Row.Cells)
                        {
                            cellElement.StyleValue = style;
                        }
                    }
 
                }
            }    
}


Best Regards

3 Answers, 1 is accepted

Sort by
0
CLAUDE LLOP
Top achievements
Rank 1
answered on 19 Jul 2012, 07:43 AM
Anyone ?
0
Daniel
Telerik team
answered on 23 Jul 2012, 02:21 PM
Hello Claude,

I believe the problem is caused by the different case for the first character in your style names:
myStyleWhite VS MyStyleWhite
myStyleOrange VS MyStyleOrange

Please note that the style names are case sensitive.

Kind regards,
Daniel
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.
0
CLAUDE LLOP
Top achievements
Rank 1
answered on 01 Aug 2012, 07:27 AM
Thank you Daniel for your precious help ;-)
Tags
Grid
Asked by
CLAUDE LLOP
Top achievements
Rank 1
Answers by
CLAUDE LLOP
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or