RadGrid Export To Excel with Excel Basic Styles

Using below code steps to get the basic excel styles in RadGrid Exporting data to Excel.

1. Add the event in the radgrid like below:

OnExcelMLExportStylesCreated="rgdReport_OnExcelMLExportStylesCreated"

2. Add the event in code behind for:

protected void rgdReport_OnExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e)
{
   // To set the Excel cell borders style

orderStylesCollection 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);

   } 

  

// styles have to set for export excel

   foreach (StyleElement style in e.Styles){

      //For Header style - background color

      if (style.Id == "headerStyle"){

            style.InteriorStyle.Pattern = InteriorPatternType.Solid;

            style.InteriorStyle.Color = System.Drawing.Color.Gray;

      }

      //For alternate row style - background color

      if (style.Id == "alternatingItemStyle" || style.Id == "alternatingPriceItemStyle" || style.Id == "alternatingPercentItemStyle" || style.Id == "alternatingDateItemStyle"){

            style.InteriorStyle.Pattern = InteriorPatternType.Solid;

            style.InteriorStyle.Color = System.Drawing.Color.LightGray;

      }

      if

(

style.Id.Contains("itemStyle") || style.Id == "priceItemStyle" || style.Id == "percentItemStyle" || style.Id == "dateItemStyle")   {

      style.InteriorStyle.Pattern = InteriorPatternType.Solid;

      style.InteriorStyle.Color = System.Drawing.Color.White;

      }

     

// for each cell border styles

      foreach (BorderStyles border in borders)

      {

         style.Borders.Add(border);

      }

     

// Each cell text wrapping

      style.AlignmentElement.Attributes.Add("ss:WrapText", "1");

}

}

3. From above code we can get below styles:

  1. n excel export data get borders
  2. Export data can wrap on each cell
  3. Export data alternative rows colors
  4. Export data header styles

References

http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-export-data-to-excel.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/wrapping-text-in-an-excel-cell-via-gridexcelbuilder.aspx

Ebook Download
View all
Learn
View all