Generate Excel files using Razor View in MVC

Опубликовано: 04 Ноябрь 2024
на канале: Everyday Be Coding
11,826
87

QUERY SOLVE -
1. How to generate excel files in C#
2. How to create and save excel file in c#
3. How to create and download excel file in c#
4. How to create an excel file programmatically without an excel application
5. How to create an excel file in c# using StringWriter Object
6. How to render view using ViewEngineResult object
7. ViewEngines class in MVC
8. ViewContext class in MVC
9. StringWriter Object in MVC
10. Entity Framework in MVC c#
11. Code first approach in Entity Framework
12. Add model, Controller, view in MVC
13. Razor View in MVC
14. DbContext class in Entity Framework

ASP.NET MVC Tutorial - Advanced Developer Guide - (C#) - #1
Blog: https://everyday-be-coding.blogspot.c...
---------------------------------------------------------------------------------
Source code download link: https://goo.gl/FPCdBz
---------------------------------------------------------------------------------
1) First Create a Model View Control Project?

Step 1: Create a new empty MVC project In Visual Studio & solution name "RazorViewToExcel".

Step 2: Adding Entity framework version 6.1.3 from Manage NuGet Packages.

Step 3: After add reference of Entity Framework, it looks like that.

Step 4: Here I am using entity framework code first approach for retrieve data from the database.

Step 5:
Create Model - First, we need to add a tblSalesman.cs class file into the Model folder. After that Copy & Paste this below code into this tblSalesman.cs file.

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ExportDatabaseToExcel.Models
{
[Table("tblSalesman")]
public class tblSalesman
{
[Key]
public int id { get; set; }
public string Name { get; set; }
public decimal SaleAmount { get; set; }
public string Country { get; set; }
public DateTime Date { get; set; }
}
}

Step 6:
Create Database Context - Add a SalesmanDBContext.cs class file into the model folder.

Step 7:
Create Repository - After that add a SalesmanRepository.cs class file into the model folder. After that Copy & Paste this code within this SalesmanRepository.cs file.

Step 8:
Configure Web.config: We need to attach a connection string into the Web.config file & the name of the connection is the same as SalesmanContext class file.

Step 9: Jquery Script File: We need to attach a jquery library file into the scripts folder in solution (Download the jquery library from https://jquery.com/download ).

Step 10: Controller: Add HomeController.cs file into the Controllers folder in solution.

Thank you for reading these blogs.
Please subscribe to my YouTube Channel & don't forget to like and share.