Skip to content

Repository files navigation

publish to nugetExcelObjectMapper on NuGetNuGetLicenseSponsor

ExcelObjectMapper

A simple and efficient .NET library for mapping Excel files to C# objects. This library makes it easy to read Excel files and convert rows to custom C# objects, with minimal code.

Features

  • Read Excel files (.xlsx) with ease
  • Map Excel rows to C# objects
  • Auto-detect column names and map them to C# object properties
  • Easily specify custom column-to-property mappings
  • Supports .NET Standard 2.0 and higher

Getting Started

Installation

Install the ExcelObjectMapper package from NuGet:

Install-Package ExcelObjectMapper

Usage

  1. Create a class that represents the structure of your Excel file:
publicclassEmployee{publicintEmployeeId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}publicDateTimeDateOfBirth{get;set;}}
  1. Read the Excel file and map the rows to the Employee class:
usingExcelObjectMapper.Readers;// ...varfilePath="path/to/your/excel-file.xlsx";varmapping=newDictionary<string,string>{{"EmployeeId","Excel ColumnName1"},{"FirstName","Excel ColumnName2"},{"LastName","Excel ColumnName3"},{"DateOfBirth","Excel ColumnName4"}};varexcelReader=newExcelReader<Employee>(filePath);varemployees=excelReader.ReadSheet(mapping);foreach(varemployeeinemployees){Console.WriteLine($"{employee.EmployeeId}: {employee.FirstName}{employee.LastName}");}

Examples

Reading Excel file using byte[]

You can read an Excel file directly from a byte array:

usingSystem.IO;usingExcelObjectMapper.Readers;byte[]fileBytes=File.ReadAllBytes("path/to/your/excel-file.xlsx");varmapping=newDictionary<string,string>{{"EmployeeId","Excel ColumnName1"},{"FirstName","Excel ColumnName2"},{"LastName","Excel ColumnName3"},{"DateOfBirth","Excel ColumnName4"}};varexcelReader=newExcelReader<Employee>(fileBytes);varemployees=excelReader.ReadSheet(mapping);foreach(varemployeeinemployees){Console.WriteLine($"{employee.EmployeeId}: {employee.FirstName}{employee.LastName}");}

Reading Excel file using IFormFile in ASP.NET Core

When working with file uploads in ASP.NET Core, you can use the IFormFile interface to read an Excel file:

usingExcelObjectMapper;usingMicrosoft.AspNetCore.Http;publicasyncTask<IActionResult>Upload(IFormFilefile){varmapping=ExcelMappingHelper.Create().Add("EmployeeId","Excel ColumnName1").Add("FirstName","Excel ColumnName2").Add("LastName","Excel ColumnName3").Add("DateOfBirth","Excel ColumnName4").Build();varexcelReader=newExcelReader<Employee>(file);varemployees=excelReader.ReadSheet(mapping);foreach(varemployeeinemployees){Console.WriteLine($"{employee.EmployeeId}: {employee.FirstName}{employee.LastName}");}returnOk("File processed successfully.");}

These examples demonstrate how you can read Excel files using different input sources with the ExcelObjectMapper library.

For more information, please refer to the documentation.

About

A .NET library for mapping Excel data to objects, built on top of EPPlus. Simplifies importing and manipulating Excel data in applications, with filtering and sorting capabilities.

Topics

Resources

Stars

23 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages