We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exporting Stacked Headers - Excel/PDF

Dear Sir,

I am unable to export Stacked Header in Excel/PDF format with Background Color/Text Color in RGB.

I can Export only Stacked Header but its color is not working. What is the Solution for that ?

Thanks
Deepak


3 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team May 20, 2019 12:23 PM UTC

Hi Deepak, 

Thanks for using Syncfusion product. 
Query 
Solution 
Exporting the Stacked headers to Excel 
You can able to customize the back color and text color of the stacked headers in excel exporting can be achieved by using the Exporting event. Please refer the below code snippet, here the colors can be set from grid style setting. You can also able to initialize the RGB colors in below method. 
 
 
ExcelExportingOptions GridExcelExportingOptions = new ExcelExportingOptions(); 
GridExcelExportingOptions.Exporting += GridExcelExportingOptions_Exporting1; 
 
var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, GridExcelExportingOptions); 
 
 
private void GridExcelExportingOptions_Exporting1(object sender, DataGridExcelExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.StackedHeaderCell) 
    { 
        e.CellStyle.BackGroundColor = sfDataGrid.Style.StackedHeaderStyle.BackColor; // you can also assign the RGB color 
        e.CellStyle.ForeGroundColor = sfDataGrid.Style.StackedHeaderStyle.TextColor; 
        e.Handled = true; 
    } 
} 
 
Please refer the below documentation for your reference. 
 
Exporting the stacked headers to PDF 
 
You can able to customize the appearance of stacked headers while exporting to PDF by using the  
 
PdfExportingOptions ExportOptions = new PdfExportingOptions(); 
ExportOptions.Exporting += ExportOptions_Exporting; 
ExportOptions.ExportStackedHeaders = true; 
 
var document = sfDataGrid.ExportToPdf(ExportOptions); 
 
private void ExportOptions_Exporting(object sender, DataGridPdfExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.StackedHeaderCell) 
    { 
        e.CellStyle.BackgroundBrush = PdfBrushes.LightGray; 
        e.CellStyle.TextBrush = PdfBrushes.Red; 
    } 
} 
 
Please refer the below documentation for your reference. 
 

Please find the sample from below location. 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan 



DE Deepak July 19, 2019 07:13 AM UTC

Hi Jagadeesan,

Thanks for your reply.

I want to export Stack Header in different color.

Example : I have Two Stack Header in my SFDATAGRID.. First Stach Header  has different color and second Stach Header has different color. Check the attachment for reference.

I am unable to get the required result.

Pls send me the solution with sample.

Thanks
Deepak 


Attachment: stackheadercolorsimple_b43b3e56.rar


SS Susmitha Sundar Syncfusion Team July 22, 2019 06:27 PM UTC

 
Hi Deepak, 
 
We have analyzed your reported query “Export the StackedHeaders with different colors” in Excel/PDF. You can achieve this in Excel by add the range for cells. Please refer the following code snippet, 
 
C#: 
private void Excel_Exporting(object sender, EventArgs e) 
        { 
            var options = new ExcelExportingOptions(); 
            var excelEngine = sfDataGrid1.ExportToExcel(sfDataGrid1.View, options); 
            var workBook = excelEngine.Excel.Workbooks[0]; 
            workBook.Worksheets[0].Range["A1:A1"].CellStyle.Color = Color.DarkCyan; 
            workBook.Worksheets[0].Range["A1:A1"].CellStyle.Font.Color = ExcelKnownColors.White; 
            workBook.Worksheets[0].Range["B1:C1"].CellStyle.Color = Color.LightCyan; 
            workBook.Worksheets[0].Range["B1:C1"].CellStyle.Font.Color = ExcelKnownColors.Black; 
            workBook.Worksheets[0].Range["D1:E1"].CellStyle.Color = Color.DarkGray; 
            workBook.Worksheets[0].Range["D1:E1"].CellStyle.Font.Color = ExcelKnownColors.White; 
            workBook.SaveAs("SampleRange.xlsx"); 
           
        } 
 
For exporting in PDF, you can achieve this by CellExporting event. Please refer the following code snippet, 
C#: 
private void options_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
        { 
            if(e.CellType==ExportCellType.StackedHeaderCell) 
            { 
               if(e.CellValue.ToString()=="Order Details") 
                { 
                     
                    var cellStyle = new PdfGridCellStyle(); 
                    cellStyle.BackgroundBrush = PdfBrushes.DarkCyan; 
                    cellStyle.TextBrush = PdfBrushes.White; 
                    e.PdfGridCell.Style = cellStyle; 
                } 
               else if (e.CellValue.ToString() == "Customer Details") 
                { 
 
                        var cellStyle = new PdfGridCellStyle(); 
                        cellStyle.BackgroundBrush = PdfBrushes.LightCyan; 
                        e.PdfGridCell.Style = cellStyle; 
                } 
                else if (e.CellValue.ToString() == "City Details") 
                { 
                        var cellStyle = new PdfGridCellStyle(); 
                        cellStyle.BackgroundBrush = PdfBrushes.DarkGray; 
                        cellStyle.TextBrush = PdfBrushes.White; 
                    e.PdfGridCell.Style = cellStyle; 
                } 
            } 
           
        } 
 
 
Please contact us for further assistance. 
 
Regards, 
Susmitha S 


Loader.
Live Chat Icon For mobile
Up arrow icon