Header add

In this article we will discuss how to import and export excel file using ASP .NET Core 3.1 razor pages. We will discuss the below points
>> How to import Excel file in .NET Core and preview the uploaded excel file?
>> How to export the excel file  ?

What is NPOI package ?
NPOI is a free tool which supports xls, xlsx and docx extensions. This project is the .NET version of POI Java project at http://poi.apache.org/. POI is an open source project, which can help you read/write XLS, DOC, PPT files. It covers most features of Excel like styling, formatting, data formulas, extract images, etc.. The good thing is, it doesn’t require Microsoft Office to be installed on the server. NPOI is an open source component and you can use it everywhere. This post shows you the basic functionalities of NPOI, but you can do much more with NPOI like styling the individual cell or rows, creating excel formulas and other stuff. The NPOI package supports both “xls” and “xlsx” extensions using HSSFWorkbook and XSSFWorkbook classes. The HSSFWorkbook class is for “xls”, where the other one is for “xlsx”. To know more about NPOI, Refer the official documentation.

Import and Export excel in ASP.NET Core 3.1 razor pages 
Let's create a .NET Core web application targeting to .NET Core 3.1 . Open VS 2019 ➠ Select ASP .NET Core web application
Install NPOI package using Nuget.
In this example we create a input file for upload the excel and after uploading it append the excel data into DIV and then download that excel and also create a excel file using dummy data and export for same. The design should like below.

The HTML code has file upload control and a button to upload the file. The below jQuery code uploads the excel file using Ajax. The code also performs the client side validation for file selection and extension checking. Once the request is successful, it appends the server response to the HTML. Read this post for handling Ajax request with ASP.NET Core 3.1 razor pages and this post for uploading files in ASP.NET Core 3.1 Razor Pages.
To import the file create a post method in Homecontroller.cs file and save the uploaded file into wwwroot folder then append it into div. To download the file follow the below code
To create and export the file, let's take a dummy data of employee and export that data.
Let's run the application and see the output.
You can see after upload the file it save it into the specified path

</> Find the Source Code in Github.com/CoreProgramm/

    In this tutorial we discussed import and export Excel file in ASP .NET Core 3.1 razor page. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post