Back to Devexpress

DxGrid.ExportToCsvAsync(String, GridCsvExportOptions) Method

blazor-devexpress-dot-blazor-dot-dxgrid-dot-exporttocsvasync-x28-system-dot-string-devexpress-dot-blazor-dot-gridcsvexportoptions-x29.md

latest4.5 KB
Original Source

DxGrid.ExportToCsvAsync(String, GridCsvExportOptions) Method

Exports grid data in CSV format and downloads the resulting file to the client machine.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public Task ExportToCsvAsync(
    string fileName,
    GridCsvExportOptions options = null
)

Parameters

NameTypeDescription
fileNameString

The name of the downloaded file.

|

Optional Parameters

NameTypeDefaultDescription
optionsGridCsvExportOptionsnull

An object that contains export options.

|

Returns

TypeDescription
Task

The task that is completed when the file is downloaded.

|

Remarks

Call the ExportToCsvAsync method to export grid data in CSV format. Note that only data cell values are exported - group rows, summaries, and cell style settings are ignored.

The method overloads allow you to write the result to a stream (ExportToCsvAsync(Stream, GridCsvExportOptions)) or to a file downloaded to a client machine (the current overload). The method accepts a GridCsvExportOptions object as a parameter. You can use this parameter to set up export settings.

Run Demo: Grid - Export

For additional information about data export in the Grid component, refer to the following topic: Export Blazor Grid Data to CSV.

Important

Security Considerations

Exported data can contain executable content. To prevent possible security vulnerabilities, set the EncodeExecutableContent property to true to enclose potentially dangerous content in quotation marks prior to exporting it to the CSV format. To learn more, see the property description.

razor
<DxGrid @ref="Grid"
        Data="@Data"
        SelectionMode="GridSelectionMode.Multiple">
    <Columns>
        <DxGridSelectionColumn Width="60px" AllowSelectAll="true" />
        <DxGridDataColumn FieldName="ContactName" Width="15%" />
        <DxGridDataColumn FieldName="ContactTitle" Width="15%" />
        <DxGridDataColumn FieldName="CompanyName" Width="20%" />
        <DxGridDataColumn FieldName="Country" Width="15%" />
        <DxGridDataColumn FieldName="FullAddress" UnboundType="GridUnboundColumnType.String" 
                          UnboundExpression="[City] + ' - ' + [PostalCode] + ' - ' + [Address]" />
    </Columns>
</DxGrid>
@* ... *@
<OptionButton Text="Export to CSV" OnClick="ExportCsv_Click" />
@* ... *@
@code {
    IEnumerable<object> Data { get; set; }
    IGrid Grid { get; set; }
    protected override async Task OnInitializedAsync() {
        Data = await NwindDataService.GetCustomersAsync();
    }
    async Task ExportCsv_Click() {
        await Grid.ExportToCsvAsync("ExportResult", new GridCsvExportOptions() {
            ExportSelectedRowsOnly = true,
        });
    }
}

Large Data Limitations

If the grid is bound to a GridDevExtremeDataSource object, the following limitations apply:

  • If you want to export selected rows only, specify the KeyFieldName property.
  • Grouped data, and filtered and sorted selected rows may differ in the resulting document. This happens because export depends on database collation. For example, the same string with different capitalization may form multiple groups in the exported document. The Grid component puts such values into a single group.

Implements

ExportToCsvAsync(String, GridCsvExportOptions)

See Also

DxGrid Class

DxGrid Members

DevExpress.Blazor Namespace