Articles in this section
Category / Section

Import data from dynamic collection to Excel worksheet in .NET Core

2 mins read

iSyncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can import data from a dynamic collection in C# and VB.NET.

How to import dynamic collection?

 

XlsIO provides support to import data into a worksheet from data table, array, and business objects. The data source can be a dynamic object collection.

 

Importing System.Dynamic.DynamicObject type object collection is not supported in .NET Core 2.1 platform. Instead of this, we can use System.Dynamic.ExpandoObject type object collection to import data into the worksheet.

The following complete code snippet explains this.

C#

//Instantiate the spreadsheet creation engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the excel application object.
    IApplication application = excelEngine.Excel;
 
    //The workbook is created
    IWorkbook workbook = application.Workbooks.Create(1);
 
    //The first worksheet object in the worksheets collection is accessed
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //The dynamic collection is imported
    worksheet.ImportData(GetMembersReport(), 1, 1, true);
 
    worksheet.UsedRange.AutofitColumns();
 
    //Saving and closing the workbook
    MemoryStream stream = new MemoryStream();
    workbook.Version = ExcelVersion.Xlsx;
    workbook.SaveAs(stream);
 
    //Set the position as '0'.
    stream.Position = 0;
 
    //Download the Excel file in the browser
    FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/excel");
    fileStreamResult.FileDownloadName = "Output.xlsx";
    return fileStreamResult;
}
 
public static List<ExpandoObject> GetMembersReport()
{
    List<ExpandoObject> reports = new List<ExpandoObject>();
    dynamic dynamicObject = new ExpandoObject();
    dynamicObject.Id = 01;
    dynamicObject.Name = "Andy Bernard";
    dynamicObject.Age = 21;
    reports.Add(dynamicObject);
 
    dynamicObject = new ExpandoObject();
    dynamicObject.Id = 02;
    dynamicObject.Name = "Jim Halpert";
    dynamicObject.Age = 23;
    reports.Add(dynamicObject);
 
    dynamicObject = new ExpandoObject();
    dynamicObject.Id = 03;
    dynamicObject.Name = "Karen Fillippe";
    dynamicObject.Age = 20;
    reports.Add(dynamicObject);
 
    return reports;
}

 

A complete working sample can be downloaded from ImportData.zip.

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

An online sample link to generate Excel file.

See Also:

Import data from dynamic collection in .NET Framework

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.


Conclusion

Hope you enjoyed learning about how to import data from dynamic collection to Excel worksheet in .NET Core

You can refer to our ​.NET Excel library feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET Excel Library documentation to understand how to present and manipulate data.

For current customers, you can check out our Angular components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Angular Diagram and other Angular components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied