RadGrid Export To Excel Add Grid Lines

1 Answer 153 Views
Grid
Mark
Top achievements
Rank 2
Iron
Iron
Iron
Mark asked on 21 Mar 2023, 06:41 PM
We want to add Grid Lines to exported excel. How do we do that within this event?
       protected void grdInventory_ExcelMLWorkBookCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExcelMLWorkBookCreatedEventArgs e)
        {
            WorkBook wb = e.WorkBook;
            StyleElement header = new StyleElement("Header");
            header.FontStyle.Bold = true;
            header.FontStyle.FontName = "Arial";
            header.FontStyle.Size = 8;
            header.AlignmentElement.Attributes["ss:WrapText"] = "1";
            wb.Styles.Add(header);

 
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Doncho
Telerik team
answered on 24 Mar 2023, 12:45 PM

Hi Mark,

Grid lines in the exported file can be applied as borders of the Grid cells. When it comes to exporting to ExcelML format, borders can be added via StyleElement in the ExcelMLExportStylesCreated event:

The same StyleElement should be passed as StyleValue to all the cells that need to be styled. This can be achieved in the ExcelMLExportRowCreated event, see:

In short, here is a sample event handling that would produce Red lines separating rows and columns in the exported excel:

protected void RadGrid1_ExcelMLExportRowCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
{
    foreach (CellElement cell in e.Row.Cells)
    {
        cell.StyleValue = "myCustomStyle";
    }
}

protected void RadGrid1_ExcelMLExportStylesCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)
{
    StyleElement cstyle = new StyleElement("myCustomStyle");
    BorderStylesCollection borders = new BorderStylesCollection();
    BorderStyles borderStyle;
    for (int i = 1; i <= 4; i++) //four borders   
    {
        borderStyle = new BorderStyles();
        borderStyle.PositionType = (PositionType)i;
        borderStyle.Color = System.Drawing.Color.Red;
        borderStyle.LineStyle = LineStyle.Continuous;
        borderStyle.Weight = 1.0;
        borders.Add(borderStyle);
    }
    foreach (BorderStyles border in borders)
        cstyle.Borders.Add(border);
    e.Styles.Add(cstyle);
}

I hope this will help.

Please let me know if any questions come up.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Mark
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Doncho
Telerik team
Share this question
or