Import and Export Excel file in ASP .NET Core 3.1 Razor age
Home .NET Core 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 >> H...
Join the DZone community and get the full member experience.Join For Free
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 ?
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 with .NET Core 3.1 . Open VS 2019 ➠ Select ASP .NET Core web application
In this example, we create an input file for upload the Excel file, and after uploading it, append the Excel data into DIV. Then, we download that Excel data and also create an Excel file using dummy data. The design is shown below:
Code Snippet Explanation
The HTML code has the file upload control and a button to upload the file. The below JQuery code uploads the Excel file using Ajax. The code also performs 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 the Homecontroller.cs file and save the uploaded file into wwwroot folder then append it into the div.
To download the file, follow the code below:
To create and export the file, let's take dummy data for an employee and export it.
Let's run the application and see the output.
Find the Source Code in Github.com/CoreProgramm/.
Published at DZone with permission of Jayant Tripathy. See the original article here.
Opinions expressed by DZone contributors are their own.