Skip to main content
All docs
V25.1
  • Export Blazor TreeList Data to CSV

    • 5 minutes to read

    Call the ExportToCsvAsync method to export TreeList data to CSV. You can save the result to a stream or download it on the client. The method parameter allows you to configure export settings.

    TreeList Exported Document

    Run Demo: TreeList - Export Data

    You can also export TreeList data to PDF, XLS, or XLSX formats.

    Limitations and Specifics

    Important

    Security Considerations

    Exported data can contain executable content. To prevent security vulnerabilities, set the EncodeExecutableContent property to true to enclose potentially dangerous content in quotation marks. For more information, refer to the property description.

    • The TreeList exports only column captions and data cell values (ignores hierarchy, summaries, and template content).
    • The TreeList exports all data rows that match the current filter criteria (including children of collapsed nodes). Handle the RowExporting event to exclude specific rows from export.

    Prevent a Column from Being Exported

    The TreeList exports data from all data columns. Set a column’s ExportEnabled property to false to exclude it from data export:

    @inject SpaceObjectDataProvider SpaceObjectDataProvider
    
    <DxButton Text="Export to CSV" Click="ExportCsv_Click" />
    <DxTreeList @ref="TreeList" Data="TreeListData" ChildrenFieldName="Satellites">
        <Columns>
            <DxTreeListDataColumn FieldName="Name" />
            <DxTreeListDataColumn FieldName="TypeOfObject" Caption="Type" />
            <DxTreeListDataColumn FieldName="Mass10pow21kg" Caption="Mass, kg" DisplayFormat="N2">
                <HeaderCaptionTemplate>Mass, 10<sup>21</sup> &#215; kg</HeaderCaptionTemplate>
            </DxTreeListDataColumn>
            <DxTreeListDataColumn FieldName="MeanRadiusInKM" ExportEnabled="false" Caption="Radius, km" DisplayFormat="N2" />
            <DxTreeListDataColumn FieldName="Volume10pow9KM3" Caption="Volume, km³" DisplayFormat="N2" />
            <DxTreeListDataColumn FieldName="SurfaceGravity" ExportEnabled="false" DisplayFormat="N2" />
        </Columns>
    </DxTreeList>
    
    @code {
        ITreeList TreeList { get; set; }
        object TreeListData { get; set; }
    
        protected override async Task OnInitializedAsync() {
            TreeListData = SpaceObjectDataProvider.GenerateData();
        }
        async Task ExportCsv_Click() {
            await TreeList.ExportToCsvAsync("ExportResult");
        }
    }
    

    Export Selected Rows

    Set the ExportSelectedRowsOnly property to true to export only selected rows:

    async Task ExportCsv_Click() {
        await TreeList.ExportToCsvAsync("ExportResult", new TreeListCsvExportOptions() {
            ExportSelectedRowsOnly = true,
        });
    }
    

    Export Display Text

    The TreeList component exports cell values. To export display text instead of values, assign true to the ExportDisplayText property:

    async Task ExportCsv_Click() {
        await TreeList.ExportToCsvAsync("ExportResult", new TreeListCsvExportOptions() {
            ExportDisplayText = true,
        });
    }
    

    Filter Exported Data

    Handle the RowExporting event to filter exported data. Set the Cancel event argument to true to exclude the row from the exported document:

    async Task ExportCsv_Click() {
        await TreeList.ExportToCsvAsync("Solar System", new TreeListCsvExportOptions() {
            RowExporting = RowExporting,
        });
    }
    
    void RowExporting(TreeListRowExportingEventArgs args) {
        var spaceObject = (SpaceObject) args.DataItem;
        if (spaceObject.TypeOfObject != "Planet" && spaceObject.TypeOfObject != "Star")
            args.Cancel = true;
    }
    

    Display Loading Indicators

    Use DevExpress Blazor Loading Panel to display a loading indicator during export operations:

    @inject SpaceObjectDataProvider SpaceObjectDataProvider
    
    <DxLoadingPanel @bind-Visible="@PanelVisible"
                    IsContentBlocked="true"
                    ApplyBackgroundShading="true"
                    IndicatorAreaVisible="false"
                    Text="Exporting Document...">
        <DxButton Text="Export to CSV" Click="ExportCsv_Click" />
        <DxTreeList @ref="TreeList" Data="TreeListData" ChildrenFieldName="Satellites">
            <Columns>
                <DxTreeListDataColumn FieldName="Name" />
                <DxTreeListDataColumn FieldName="TypeOfObject" Caption="Type" />
                <DxTreeListDataColumn FieldName="Mass10pow21kg" Caption="Mass, kg" DisplayFormat="N2">
                    <HeaderCaptionTemplate>Mass, 10<sup>21</sup> &#215; kg</HeaderCaptionTemplate>
                </DxTreeListDataColumn>
                <DxTreeListDataColumn FieldName="MeanRadiusInKM" Caption="Radius, km" DisplayFormat="N2" />
                <DxTreeListDataColumn FieldName="Volume10pow9KM3" Caption="Volume, km³" DisplayFormat="N2" />
                <DxTreeListDataColumn FieldName="SurfaceGravity" DisplayFormat="N2" />
            </Columns>
        </DxTreeList>
    </DxLoadingPanel>
    
    @code {
        ITreeList TreeList { get; set; }
        object TreeListData { get; set; }
        bool PanelVisible { get; set; } = false;
    
        protected override async Task OnInitializedAsync() {
            TreeListData = SpaceObjectDataProvider.GenerateData();
        }
        async Task ExportCsv_Click() {
            PanelVisible = true;
            await Task.Yield();
            await TreeList.ExportToCsvAsync("ExportResult");
            PanelVisible = false;
        }
    }