Back to Devexpress

XlsxExportOptionsEx.AllowCellImages Property

corelibraries-devexpress-dot-xtraprinting-dot-xlsxexportoptionsex-98c841ee.md

latest10.8 KB
Original Source

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.v25.2.Core.dll

NuGet Package : DevExpress.Printing.Core

Declaration

csharp
[DefaultValue(DefaultBoolean.False)]
public DefaultBoolean AllowCellImages { get; set; }
vb
<DefaultValue(DefaultBoolean.False)>
Public Property AllowCellImages As DefaultBoolean

Property Value

TypeDefaultDescription
DefaultBooleanFalse

true to export cell images; otherwise, false.

|

Available values:

NameDescriptionReturn Value
True

The value is true.

|

0

| | False |

The value is false.

|

1

| | Default |

The value is specified by a global option or a higher-level object.

|

2

|

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:

The following code sample exports images from the Image column to Excel cells:

xaml
<dxg:GridColumn FieldName="Image">
    <dxg:GridColumn.EditSettings>
        <dxe:ImageEditSettings Stretch="None"/>
    </dxg:GridColumn.EditSettings>
</dxg:GridColumn>
csharp
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);
}
vb
Public Class Record
    Inherits BindableBase

    Public Property Image As ImageSource
        Get
            Return GetValue(Of ImageSource)()
        End Get
        Set(ByVal value As ImageSource)
            Return SetValue(value)
        End Set
    End Property
    ' ...
End Class
' ...
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim options As XlsxExportOptionsEx = New XlsxExportOptionsEx()
    options.AllowCellImages = DevExpress.Utils.DefaultBoolean.[True]
    view.ExportToXlsx("c:\work\grid_export.xlsx", options)
End Sub

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:

csharp
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.v25.2;component/Images/Actions/Apply_32x32.png"));
        else
            e.Value = ImagesExportHelper.ImageUriToByteArray(new System.Uri("pack://application:,,,/DevExpress.Images.v25.2;component/Images/Actions/Cancel_32x32.png"));
    }
    e.Handled = true;
}
vb
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim options As XlsxExportOptionsEx = New XlsxExportOptionsEx()
    options.AllowCellImages = DevExpress.Utils.DefaultBoolean.[True]
    options.CustomizeCell += AddressOf Options_CustomizeCell
    view.ExportToXlsx("c:\work\grid_export.xlsx", options)
End Sub

Private Sub Options_CustomizeCell(ByVal e As DevExpress.Export.CustomizeCellEventArgs)
    If e.ColumnFieldName = NameOf(Record.IsRead) AndAlso e.AreaType = DevExpress.Export.SheetAreaType.DataArea Then
        Dim columnAlignment As XlCellAlignment = New XlCellAlignment() With {
            .HorizontalAlignment = XlHorizontalAlignment.Center
        }
        e.Formatting.Alignment = columnAlignment

        If CBool(e.Value) Then
            e.Value = ImagesExportHelper.ImageUriToByteArray(New System.Uri("pack://application:,,,/DevExpress.Images.v25.2;component/Images/Actions/Apply_32x32.png"))
        Else
            e.Value = ImagesExportHelper.ImageUriToByteArray(New System.Uri("pack://application:,,,/DevExpress.Images.v25.2;component/Images/Actions/Cancel_32x32.png"))
        End If
    End If

    e.Handled = True
End Sub

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:

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:

csharp
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);
}
vb
Private Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim options As DevExpress.XtraPrinting.XlsxExportOptionsEx = New DevExpress.XtraPrinting.XlsxExportOptionsEx()
    options.AllowCellImages = DevExpress.Utils.DefaultBoolean.[True]
    advBandedGridView1.ExportToXlsx("grid_export.xlsx", options)
End Sub

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:

csharp
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;
    }
}
vb
Private Sub Options_CustomizeCell(ByVal e As DevExpress.Export.CustomizeCellEventArgs)
    If e.ColumnFieldName = "Photo" AndAlso e.Value Is Nothing Then
        e.Value = DevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(imageCollection1.Images("car"))
        e.Handled = True
    End If
End Sub

The e.Value property accepts images only as Byte[] objects. Use ByteImageConverter methods to convert images to the required format.

See Also

XlsxExportOptionsEx Class

XlsxExportOptionsEx Members

DevExpress.XtraPrinting Namespace