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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Exporting Stacked Headers - Excel/PDF

Thread ID:

Created:

Updated:

Platform:

Replies:

144690 May 17,2019 01:57 PM UTC Jul 22,2019 06:27 PM UTC WinForms 3
loading
Tags: SfDataGrid
Deepak
Asked On May 17, 2019 01:57 PM UTC

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


Jagadeesan Pichaimuthu [Syncfusion]
Replied On 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 


Deepak
Replied On 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

Susmitha Sundar [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon