Telerik Export Excel Infrastructure - Setting Cell color and borders

1 Answer 104 Views
UI for ASP.NET AJAX in ASP.NET MVC
Ajay
Top achievements
Rank 1
Ajay asked on 12 May 2022, 12:32 PM

Hi Telerik Team,

Using Telerik.Web.UI.ExportInfrastructure we are generating an excel , and looking out a way to fill the cell colors , which we achieved but after filling cell colors borders getting disappeared. I tried using below code to set borders but still borders not applying

 


            int currentRow = 1;
            eis.Table WS_Sample = new eis.Table("Sample");
            eis.Row headerRw1 = WS_PriorAuthorization.Rows[currentRow];
            headerRw1.Style.BackColor = System.Drawing.Color.Red;
            headerRw1.Style.BorderLeftColor = System.Drawing.Color.Green;
            headerRw1.Style.BorderRightColor = System.Drawing.Color.Green;
            //headerRw1.Cells[1, currentRow].Style.BorderLeftWidth = Unit.Pixel(35);
            //headerRw1.Cells[1, currentRow].Style.BorderRightWidth = Unit.Pixel(35); 
            headerRw1.Style.BorderLeftWidth = Unit.Pixel(35);
            headerRw1.Style.BorderRightWidth = Unit.Pixel(35);
            headerRw1.Style.ForeColor = System.Drawing.Color.Black;
            headerRw1.Style.Font.Bold = true;

Doncho
Telerik team
commented on 17 May 2022, 08:40 AM

Hi Ajay,

Could you please share the markup declaration of the RadGrid along with the relevant code-behind logic so we can get a more clear overview of the case and assist you more accurately?

What export format are you currently using?

If the case is about exporting to XLSX, you can try the styling approaches listed in the Styling Columns / Rows / Cell section in our Excel-Xlsx (OOXML) Export article. As an example:

protected void RadGrid1_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
{
    //style all cells
    var allCells = e.ExportStructure.Tables[0].Cells;
    foreach (var cell in allCells)
    {
        cell.Style.BackColor = System.Drawing.Color.Coral;
        cell.Style.BorderLeftColor = System.Drawing.Color.Blue;
        cell.Style.BorderRightColor = System.Drawing.Color.Blue;
    }

    //override styles just for the header cells
    var rows = e.ExportStructure.Tables[0].Rows;
    //actual header index may vary depending on if commandItem, top pagerItem, filterItem are exported
    var headerCells = rows[2].Cells;
    foreach (var cell in headerCells)
    {
        cell.Style.BackColor = System.Drawing.Color.Red;
        cell.Style.BorderLeftColor = System.Drawing.Color.Green;
        cell.Style.BorderRightColor = System.Drawing.Color.Green;
    }
}

1 Answer, 1 is accepted

Sort by
0
Had
Top achievements
Rank 1
Iron
answered on 10 Jun 2022, 08:20 AM | edited on 10 Jun 2022, 08:21 AM

Hi Ajay,

Can use ItemDataBound to fill the cell colour. Then when exporting it will follow the format from the table.

   protected void WeeklyReport_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {  
                GridDataItem table = (GridDataItem)e.Item;
               
               
                    table["ColumnName"].BackColor = Color.Red;
               
            }

        }

Thanks.

By Had

Tags
UI for ASP.NET AJAX in ASP.NET MVC
Asked by
Ajay
Top achievements
Rank 1
Answers by
Had
Top achievements
Rank 1
Iron
Share this question
or