ASP. NET Core import and export Excel xlsx file, corexlsx

Source: Internet
Author: User

ASP. NET Core import and export Excel xlsx file, corexlsx

ASP. NET Core uses EPPlus. Core to import and export Excel xlsx files. EPPlus. Core supports Excel 2007/2010 xlsx file Import and Export and can run on Windows, Linux, and Mac.

EPPlus. Core is modified based on EPPlus. libgdiplus must be installed in Linux.

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

EPPlus. Core: https://github.com/VahidN/EPPlus.Core

Next, import and export the Excel xlsx file in ASP. NET Core.

Create a project

Create an ASP. NET Core Web Application project ASPNETCoreExcel and select a Web Application for no authentication.

Add an EPPlus. Core reference.

Run the NuGet command line:

Install-Package EPPlus.Core

You can also use the NuGet Package Manager for installation.

Export the xlsx file

Create an XlsxController and add the Export operation.

Public class XlsxController: Controller {private region _ hostingEnvironment; public XlsxController (IHostingEnvironment hostingEnvironment) {_ hostingEnvironment = hostingEnvironment;} public IActionResult Index () {return View ();} public IActionResult Export () {string sWebRootFolder = _ hostingEnvironment. webRootPath; string sFileName = $ "mongoguid.newguid(mongomongo.xlsx"; FileInfo file = new FileInfo (Path. combine (sWebRootFolder, sFileName); using (ExcelPackage package = new ExcelPackage (file) {// Add worksheet ExcelWorksheet worksheet = package. workbook. worksheets. add ("aspnetcore"); // Add the header worksheet. cells [1, 1]. value = "ID"; worksheet. cells [1, 2]. value = "Name"; worksheet. cells [1, 3]. value = "Url"; // Add the Value worksheet. cells ["A2"]. value = 1000; 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 ();} return File (sFileName, "application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ");}}

 

Obtain the HostingEnvironment through dependency injection, and obtain the related directories and properties of the program.

Add a link to the Index view to export the Excel file.

@ {}< H2> ASP. NET Core import and Export an Excel xlsx file 

Click Export file. The result is as follows.

 

Import xlsx files

Add an upload file in the index view and add the Import operation.

Index. cshtml

{}< H2> ASP. NET Core import and Export Excel xlsx files 

 

        [HttpPost]        public IActionResult Import(IFormFile excelfile)        {            string sWebRootFolder = _hostingEnvironment.WebRootPath;            string sFileName = $"{Guid.NewGuid()}.xlsx";            FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));            try            {                using (FileStream fs = new FileStream(file.ToString(), FileMode.Create))                {                    excelfile.CopyTo(fs);                    fs.Flush();                }                using (ExcelPackage package = new ExcelPackage(file))                {                    StringBuilder sb = new StringBuilder();                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];                    int rowCount = worksheet.Dimension.Rows;                    int ColCount = worksheet.Dimension.Columns;                    bool bHeaderRow = true;                    for (int row = 1; row <= rowCount; row++)                    {                        for (int col = 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);                    }                    return Content(sb.ToString());                }            }            catch (Exception ex)            {                return Content(ex.Message);            }        }

 

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

 

 

 

Upload the corresponding file, as shown below.

 

ASP. NET Core can be simply imported and exported to Excel.

 

If you think this article is helpful to you, click"Recommendation", Thank you.

 

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.