How to Import Data from Dynamic Collection to ASP.NET Core Excel?
In Syncfusion® ASP.NET Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It 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
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the 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 a trial message.
I hope you enjoyed learning about how to Import Data from Dynamic Collection to ASP.NET Core Excel.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!