i already read this article but this is not helpful
https://www.syncfusion.com/kb/5987/how-to-export-the-sfdatagrid-to-excel-with-styles
i am using StyleSelector base for each Cell value.
any way to get each cell style and assign to the excel?
right now my solution is using old method
Microsoft.Office.Interop.Excel
to loop through all value and cell style condition,
assign to excel one by one
the performance is bad but still did the job.
I also have a idea to port everything to gridcontrol
Using the Excel Engine (XlsIO)
but not test it yet.
|
public static Color blueCellBackgroundColor;
public static Color redCellBackgroundColor;
private static void GetDataGridStyles(SfDataGrid dataGrid)
{
var blueCellStyle = dataGrid.FindResource("blueCellStyle") as Style;
var redCellStyle = dataGrid.FindResource("redCellStyle") as Style;
if (blueCellStyle == null || redCellStyle == null)
return;
//here get the blueCellStyle background color
foreach (Setter setter in blueCellStyle.Setters)
{
if (setter.Property == GridCell.BackgroundProperty)
blueCellBackgroundColor = (Color)ColorConverter.ConvertFromString(setter.Value.ToString());
}
//here get the redCellStyle background color
foreach (Setter setter in redCellStyle.Setters)
{
if (setter.Property == GridCell.BackgroundProperty)
redCellBackgroundColor = (Color)ColorConverter.ConvertFromString(setter.Value.ToString());
}
} |
|
private void OnExportToExcelClicked(object sender, RoutedEventArgs e)
{
//here get the style
GetDataGridStyles(sfDataGrid);
var options = new ExcelExportingOptions();
options.ExcelVersion = ExcelVersion.Excel2013;
options.CellsExportingEventHandler = CellExportingHandler;
var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options);
var workBook = excelEngine.Excel.Workbooks[0];
} |
|
private static void CellExportingHandler(object sender, GridCellExcelExportingEventArgs e)
{
// Based on the column mapping name and the cell type, we can change the cell
//values while exporting to excel.
//here customize based on your scenario
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "OrderID")
{
//here apply cell style based on condition
if ((int)e.CellValue < 1005)
e.Range.CellStyle.FillBackgroundRGB = System.Drawing.Color.FromArgb(redCellBackgroundColor.A, redCellBackgroundColor.R, redCellBackgroundColor.G, redCellBackgroundColor.B);
else
e.Range.CellStyle.FillBackgroundRGB = System.Drawing.Color.FromArgb(blueCellBackgroundColor.A, blueCellBackgroundColor.R, blueCellBackgroundColor.G, blueCellBackgroundColor.B);
}
} |
thanks for the support.
I have a concern about this method have to be obsolete on next release.
one more thing I missed to mention
on my StyleSelector i use other cell value to set the style,
how can I get the other cell base on the GridCellExcelExportingEventArgs ?
|
private static void CellExportingHandler(object sender, GridCellExcelExportingEventArgs e)
{
// Based on the column mapping name and the cell type, we can change the cell
//values while exporting to excel.
//here customize based on your scenario
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "CustomerID")
{
//here get the datarow details
var dataRow = e.NodeEntry as DataRowView;
//here apply cell style based on condition
if ((int)dataRow.Row["EmployeeID"] < 1005)
e.Range.CellStyle.FillBackgroundRGB = System.Drawing.Color.FromArgb(redCellBackgroundColor.A, redCellBackgroundColor.R, redCellBackgroundColor.G, redCellBackgroundColor.B);
else
e.Range.CellStyle.FillBackgroundRGB = System.Drawing.Color.FromArgb(blueCellBackgroundColor.A, blueCellBackgroundColor.R, blueCellBackgroundColor.G, blueCellBackgroundColor.B);
}
} |