excel upload

 1. install 


version acc to ur entityframework core 


requires mentioning in json file above than this .. licence error.below i have used avobe this and used json update in application.json


1. controller

 public async Task<List<GetJoke>> Import(IFormFile file)

        {


            var list = new List<GetJoke>();

            using (var stream = new MemoryStream())

            {

                await file.CopyToAsync(stream);

                // If you are a commercial business and have

                // purchased commercial licenses use the static property

                // LicenseContext of the ExcelPackage class:

                ExcelPackage.LicenseContext = LicenseContext.Commercial; //this also we dont have to add for below version.


                // If you use EPPlus in a noncommercial context

                // according to the Polyform Noncommercial license:

                ExcelPackage.LicenseContext = LicenseContext.NonCommercial; //for below version we should not add this (4.0 below)

                using (var package = new ExcelPackage(stream))

                {

                    ExcelWorksheet worksheet = package.Workbook.Worksheets[0];

                    var rowcount = worksheet.Dimension.Rows;

                    for (int row = 2; row <= rowcount; row++)

                    {

                        list.Add(new GetJoke

                        {

                            Id=worksheet.Cells[row, 1].Value.ToString().Trim(),

                            jokequestion = worksheet.Cells[row, 2].Value.ToString().Trim(),

                            jokeanswer = worksheet.Cells[row, 3].Value.ToString().Trim(),

                            status = worksheet.Cells[row, 4].Value.ToString().Trim(),



                        });

                        

                    }

                     

                }

            }


2.view


<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">

    <div class="container-fluid">

        <form method="post"  asp-controller="Hasnu" asp-action="Import" enctype="multipart/form-data">

            <input type="file" name="file" />

            <button type="submit">Import From Excel</button>

            </form>

        </div>

    </div>


3.application.json --verion version above 4. ihave said erlier for below we dont have to do this


{

  "ConnectionStrings": {

    "DefaultConnection": "Server=DESKTOP-JENHTHC;Database=aspnet-WebApplication-41EC092B-9D08-4320-9980-F1F763F2A144;Trusted_Connection=True;MultipleActiveResultSets=true"

  },

  "EPPlus": {

    "ExcelPackage": {

      "LicenseContext": "Commercial" //The license context used

    }

  },

  "Logging": {

    "LogLevel": {

      "Default": "Information",

      "Microsoft": "Warning",

      "Microsoft.Hosting.Lifetime": "Information"

    }

  },

  "AllowedHosts": "*"

}


Comments