private void GridCellExcelExportingEvent(object sender, GridCellExcelExportingEventArgs e)
{
//Check whehter the CellType is RecordCell and the column name is IsCheck which is GridTemplateColumn contains the image.
if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsCheck")
{
//Access the fail image as default value
System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\..\Images\fail.png");
//Images are exported based on the CellValue
if (e.CellValue.Equals("Pass"))
{
//Access the image from the specified path
image = System.Drawing.Image.FromFile(@"..\..\Images\pass.png");
}
//Add Picture to the specific range of cell in Worksheet
var pic = e.Range.Worksheet.Pictures.AddPicture(e.Range.Row, e.Range.Column, image);
//Adjust Position of the picture based on the expected appearance in the exported cell
pic.Left += 20;
pic.Top += 5;
//Adjust image size to the cell width and height when it's exceeded
if (image.Width > e.Range.ColumnWidth)
pic.Width = (int)e.Range.ColumnWidth;
if (image.Height > e.Range.RowHeight)
pic.Height = (int)e.Range.RowHeight;
e.Range.Cells[0].Value = null;
e.Handled = true;
}
} |