ASP. NET Core Import Export Excel xlsx file

Source: Internet
Author: User
Tags rowcount

ASP. NET Core uses Epplus.core import to export an Excel xlsx file, Epplus.core supports Excel 2007/2010 xlsx file Import and export, which can be run on Windows, Linux and Mac.

Epplus.core is based on epplus changes and requires the installation of Libgdiplus under Linux.

epplus:http://epplus.codeplex.com/

Epplus.core:https://github.com/vahidn/epplus.core

Import the export Excel xlsx file in ASP. NET Core.

New Project

Create a new ASP. NET Core Web application project Aspnetcoreexcel, and choose not to authenticate the WEB application.

Then add the Epplus.core reference.

Using the NuGet command line:

Install-package Epplus.core

You can also use the NuGet Package Manager to install.

Export xlsx file

Create a new Xlsxcontroller and add an export operation.

     Public classXlsxcontroller:controller {Privateihostingenvironment _hostingenvironment;  PublicXlsxcontroller (ihostingenvironment hostingenvironment) {_hostingenvironment=hostingenvironment; }         PublicIactionresult Index () {returnView (); }         PublicIactionresult Export () {stringSwebrootfolder =_hostingenvironment.webrootpath; stringsFileName = $"{guid.newguid ()}.xlsx"; FileInfo file=NewFileInfo (Path.Combine (Swebrootfolder, sfilename)); using(Excelpackage package =Newexcelpackage (file)) {                //Add Worksheetexcelworksheet worksheet = Package. WORKBOOK.WORKSHEETS.ADD ("Aspnetcore"); //Add HeaderWorksheet. cells[1,1]. Value ="ID"; Worksheet. cells[1,2]. Value ="Name"; Worksheet. cells[1,3]. Value ="URL"; //Add ValueWorksheet. cells["A2"]. Value = +; Worksheet. cells["B2"]. Value ="Linezero"; Worksheet. cells["C2"]. Value ="http://www.cnblogs.com/linezero/"; Worksheet. cells["A3"]. Value =1001; Worksheet. cells["B3"]. Value ="Linezero GitHub"; Worksheet. cells["C3"]. Value ="Https://github.com/linezero"; Worksheet. cells["C3"]. Style.Font.Bold =true;Package .             Save (); }            returnFile (sFileName,"Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); }    }

By relying on injection to get hostingenvironment, corresponding can get the relevant directory and properties of the program.

Then add the index view to increase a link to export Excel

@{}"export" > Export excel</a>

Click Export File to open the results below.

Import xlsx file

Add an upload file to the index view and add an import operation.

Index.cshtml

@{ }"Export"> Export EXCEL&LT;/A&GT;&LT;HR/><form enctype="Multipart/form-data"Method="Post"asp-action="Import"> <input type="file"Name="Excelfile"/> <input type="Submit"Value="Upload"/></form>

[HttpPost] Publiciactionresult Import (iformfile excelfile) {stringSwebrootfolder =_hostingenvironment.webrootpath; stringsFileName = $"{guid.newguid ()}.xlsx"; FileInfo file=NewFileInfo (Path.Combine (Swebrootfolder, sfilename)); Try            {                using(FileStream fs =NewFileStream (file. ToString (), FileMode.Create)) {excelfile.                    CopyTo (FS); Fs.                Flush (); }                using(Excelpackage package =Newexcelpackage (file)) {StringBuilder SB=NewStringBuilder (); excelworksheet worksheet= Package. workbook.worksheets[1]; intRowCount =worksheet.                    Dimension.rows; intColCount =worksheet.                    Dimension.columns; BOOLBheaderrow =true;  for(introw =1; Row <= RowCount; row++)                    {                         for(intCol =1; Col <= ColCount; col++)                        {                            if(Bheaderrow) {sb. Append (worksheet. Cells[row, Col]. Value.tostring ()+"\ t"); }                            Else{sb. Append (worksheet. Cells[row, Col]. Value.tostring ()+"\ t"); }} sb.                    Append (Environment.NewLine); }                    returnContent (sb.)                ToString ()); }            }            Catch(Exception ex) {returnContent (ex.            Message); }        }

Run the program to open http://localhost:5000/xlsx

Upload the corresponding file, shown below.

An ASP. NET core simple import and export Excel feature is also completed.

If you think this article is helpful to you, please click " recommend ", thank you.

ASP. NET Core Import Export Excel xlsx file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.