ASP. NET Core import and export Excel xlsx file instance, 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.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.