Import List of Objects

This module uses the DevExpress Spreadsheet API to import a user list into an Excel document. You can select Imported Fields or leave the editor empty to import all available data. To preview the data source, expand the Imported Data panel.

Use the Import Data to Document... dropdown button to select the output format, generate an Excel file with the specified data, and download the result.

Press backspace or delete to remove the tag
First Name
Last Name
Birth Date



using DevExpress.Spreadsheet;
using Microsoft.AspNetCore.Mvc;

async Task<Stream> ImportList(IList<UserModel> users, string[] selectedFields, DocumentFormat outputFormat) {

    using Workbook workbook = new Workbook();

    // Prepare first worksheet
    var firstWorksheet = workbook.Worksheets.First();
    firstWorksheet.Name = "Users";
    firstWorksheet["A1"].Value = "Import data from a list of users:";

    // Set headers
    if(selectedFields == null || !selectedFields.Any()) {
        selectedFields = new string[3];
        selectedFields[0] = nameof(UserModel.FirstName);
        selectedFields[1] = nameof(UserModel.LastName);
        selectedFields[2] = nameof(UserModel.BirthDate);
    }
    for(int i = 0; i < selectedFields.Length; i++) {
        firstWorksheet[0, i + 1].Value = selectedFields[i];
    }
    // Make header text bold
    var formatting = firstWorksheet["B1:D1"].BeginUpdateFormatting();
    formatting.Font.Bold = true;
    firstWorksheet["B1:D1"].EndUpdateFormatting(formatting);

    // Import values from specific data source fields
    // Data starts with the B1 cell.
    var importOptions = new DataSourceImportOptions() {
        PropertyNames = selectedFields
    };
    firstWorksheet.Import(users, 1, 1, importOptions);

    // AutoFit columns
    firstWorksheet.Columns.AutoFit(0, 4);

    // Prepare output stream
    var outputStream = new MemoryStream();

    // Save the document
    workbook.SaveDocument(outputStream, outputFormat);
    outputStream.Seek(0, SeekOrigin.Begin);

    return outputStream;
}