XlsxExportOptionsEx.AllowCellImages Property
Gets or sets whether to export cell images. Available in data-aware export mode for WPF Data Grid and WinForms Data Grid.
Namespace: DevExpress.XtraPrinting
Assembly: DevExpress.Printing.v24.2.Core.dll
Declaration
Property Value
Type | Default | Description |
---|---|---|
DefaultBoolean | False |
|
Available values:
Name | Description | Return Value |
---|---|---|
True | The value is true. |
|
False | The value is false. |
|
Default | The value is specified by a global option or a higher-level object. |
|
Remarks
Set the AllowCellImages
property to true
to export images displayed in grid cells to .xlsx files as cell pictures.
WPF Specifics
Export Images Displayed in Cells
You can export cell images if the grid control meets the following conditions:
- Cells with pictures contain image data (Byte[], ImageSource, BitmapImage, etc.).
- (for unbound columns) The ColumnBase.UnboundDataType property is set to ImageSource or Byte[].
- The image column’s EditSettings property is set to ImageEditSettings or PopupImageEditSettings.
The following code sample exports images from the Image column to Excel cells:
<dxg:GridColumn FieldName="Image">
<dxg:GridColumn.EditSettings>
<dxe:ImageEditSettings Stretch="None"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
public class Record : BindableBase {
public ImageSource Image {
get => GetValue<ImageSource>();
set => SetValue(value);
}
// ...
}
// ...
void Button_Click(object sender, RoutedEventArgs e) {
XlsxExportOptionsEx options = new XlsxExportOptionsEx();
options.AllowCellImages = DevExpress.Utils.DefaultBoolean.True;
view.ExportToXlsx(@"c:\work\grid_export.xlsx", options);
}
Replace Cell Content with Images
If grid cells do not contain image data, you can use the XlsxExportOptionsEx.CustomizeCell event to populate document cells with images based on cell values:
void Button_Click(object sender, RoutedEventArgs e) {
XlsxExportOptionsEx options = new XlsxExportOptionsEx();
options.AllowCellImages = DevExpress.Utils.DefaultBoolean.True;
options.CustomizeCell += Options_CustomizeCell;
view.ExportToXlsx(@"c:\work\grid_export.xlsx", options);
}
void Options_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs e) {
if (e.ColumnFieldName == nameof(Record.IsRead) && e.AreaType == DevExpress.Export.SheetAreaType.DataArea) {
XlCellAlignment columnAlignment = new XlCellAlignment() {
HorizontalAlignment = XlHorizontalAlignment.Center
};
e.Formatting.Alignment = columnAlignment;
if ((bool)e.Value)
e.Value = ImagesExportHelper.ImageUriToByteArray(new System.Uri("pack://application:,,,/DevExpress.Images.v24.2;component/Images/Actions/Apply_32x32.png"));
else
e.Value = ImagesExportHelper.ImageUriToByteArray(new System.Uri("pack://application:,,,/DevExpress.Images.v24.2;component/Images/Actions/Cancel_32x32.png"));
}
e.Handled = true;
}
The e.Value
property accepts images only as Byte[] objects. Use ImagesExportHelper
methods to convert images to the required format.
WinForms Specifics
Export Images Displayed in Cells
The WinForms Data Grid can export images if it meets the following requirements:
- Columns store image data (Byte[], Image, Bitmap, SvgImage, SvgBitmap etc.).
- Cells contain image editors (RepositoryItemImageEdit or RepositoryItemPictureEdit).
Note
Images of SvgImage and SvgBitmap types are exported as raster images that depend on the applied skin and DPI. To process vector images in the XlsxExportOptionsEx.CustomizeCell event, render such images as Image objects.
The following code snippet uses the AllowCellImages
property to export grid images as cell pictures:
void btnExport_Click(object sender, EventArgs e) {
DevExpress.XtraPrinting.XlsxExportOptionsEx options = new DevExpress.XtraPrinting.XlsxExportOptionsEx();
options.AllowCellImages = DevExpress.Utils.DefaultBoolean.True;
advBandedGridView1.ExportToXlsx("grid_export.xlsx", options);
}
Replace Cell Content with Images
If grid cells do not contain image data, you can use the XlsxExportOptionsEx.CustomizeCell event to populate document cells with images based on cell values. The following code snippet adds placeholder images during the export operation if cells in the Photo column are empty:
void Options_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs e) {
if(e.ColumnFieldName == "Photo" && e.Value == null) {
e.Value = DevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(imageCollection1.Images["car"]);
e.Handled = true;
}
}
The e.Value
property accepts images only as Byte[] objects. Use ByteImageConverter
methods to convert images to the required format.