/Excel2Object

This library convert an Excel file to an object list and also convert a list to an Excel File

Primary LanguageC#MIT LicenseMIT

Excel2Object

This library convert an Excel file to an object list and also convert a list to an Excel File

Usage

  • Add nuget package:
dotnet add package Excel2Object.Extensions --version 1.1.0
  • For converting a list to an Excel file:
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", 
            "Mild", "Warm", "Balmy", "Hot", "Sweltering", 
            "Scorching"
        };


        [HttpGet(Name = "ExportWeatherAsExcelFile")]
        public IActionResult Get()
        {
            var myList = Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToList();

            // <YourList>.ToExcelFile();
            var fileInfo = myList.ToExcelFile();

            return File(fileInfo.File, fileInfo.ContentType, fileInfo.FileName);
        }
  • For converting an Excel file to an Object list:
        [HttpPost(Name = "ImportWeatherAsExcelFile")]
        public IActionResult Post(IFormFile file)
        {

            var fileStream = file.OpenReadStream();

            // <YourStream>.ToList<YourModel>();
            var objectList = fileStream.ToList<WeatherForecast>();

            return Ok(objectList);
        }