Introduction
In this post, we will see how we can import and export Excel data in ASP.NET Core. We are using EPPlus.Core library which helps us to perform import and export operations. I hope you will like it.
Prerequisites
Make sure you have installed Visual Studio 2017 (.Net Framework 4.6.1) and SQL Server.
In this post, we are going to -
- Create Database.
- Create MVC application.
- Install EPPlus.Core library
- Use Entity Framework Core database first approach.
- Create our Customer Controller.
SQL Database part
Here, you can find the script to create a database and its tables.
Create Database
- USE [master]
- GO
- /****** Object: Database [DbCustomers] Script Date: 2/18/2018 2:19:48 AM ******/
- CREATE DATABASE [DbCustomers]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'DbCustomers', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbCustomers.mdf' , SIZE = 4096KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
- LOG ON
- ( NAME = N'DbCustomers_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbCustomers_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
- GO
- ALTER DATABASE [DbCustomers] SET COMPATIBILITY_LEVEL = 110
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [DbCustomers].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [DbCustomers] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [DbCustomers] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [DbCustomers] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [DbCustomers] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [DbCustomers] SET ARITHABORT OFF
- GO
- ALTER DATABASE [DbCustomers] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [DbCustomers] SET AUTO_CREATE_STATISTICS ON
- GO
- ALTER DATABASE [DbCustomers] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [DbCustomers] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [DbCustomers] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [DbCustomers] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [DbCustomers] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [DbCustomers] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [DbCustomers] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [DbCustomers] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [DbCustomers] SET DISABLE_BROKER
- GO
- ALTER DATABASE [DbCustomers] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [DbCustomers] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [DbCustomers] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [DbCustomers] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [DbCustomers] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [DbCustomers] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [DbCustomers] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [DbCustomers] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE [DbCustomers] SET MULTI_USER
- GO
- ALTER DATABASE [DbCustomers] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [DbCustomers] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [DbCustomers] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
- GO
- ALTER DATABASE [DbCustomers] SET TARGET_RECOVERY_TIME = 0 SECONDS
- GO
- ALTER DATABASE [DbCustomers] SET READ_WRITE
- GO
Create Table
After creating the database, we will move to create the "Customers" table.
Customers Table
- USE [DbCustomers]
- GO
- /****** Object: Table [dbo].[Customers] Script Date: 2/18/2018 2:20:40 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[Customers](
- [CustomerID] [int] IDENTITY(1,1) NOT NULL,
- [CustomerName] [varchar](50) NULL,
- [CustomerEmail] [varchar](50) NULL,
- [CustomerCountry] [varchar](50) NULL,
- CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
- (
- [CustomerID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
Create your MVC application








Geho Da FirenzePosted Nov 5, 2019, 5:23 PM
What do I need to do to save a new file in the computer, instead overwritting the content of the file??
Robert EverettPosted Mar 16, 2019, 7:28 AM
When I run the app, worksheet ["Customer"] is null. Do I need to create the sheet with some data in MS Excel in ImportCustomers.xlsx before adding it to wwwroot ?
Former memberPosted Aug 30, 2018, 6:22 AM
Detailed blog:http://wp.me/p3Osmq-zr
Former memberPosted Aug 30, 2018, 6:15 AM
A detailed blog with sample code : http://wp.me/p3Osmq-A4
Sagar Pandurang KapPosted Feb 19, 2018, 10:25 PM
Nice article.What is primary use of this??