程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> ASP.NET Core 導入導出Excel xlsx 文件實例,corexlsx

ASP.NET Core 導入導出Excel xlsx 文件實例,corexlsx

編輯:關於.NET

ASP.NET Core 導入導出Excel xlsx 文件實例,corexlsx


ASP.NET Core 使用EPPlus.Core導入導出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件導入導出,可以運行在Windows, Linux和Mac。

EPPlus.Core 是基於EPPlus 更改而來,在Linux 下需要安裝libgdiplus 。

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

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

下面在ASP.NET Core 中導入導出Excel xlsx 文件。

新建項目

新建一個ASP.NET Core Web Application 項目ASPNETCoreExcel,選擇Web 應用程序 不進行身份驗證。

然後添加EPPlus.Core 引用。

使用NuGet 命令行:

Install-Package EPPlus.Core

也可以使用NuGet包管理器安裝。

導出xlsx文件

新建一個XlsxController ,添加Export 操作。

 public class XlsxController : Controller
 {
  private IHostingEnvironment _hostingEnvironment;

  public XlsxController(IHostingEnvironment hostingEnvironment)
  {
   _hostingEnvironment = hostingEnvironment;
  }
  public IActionResult Index()
  {
   return View();
  }

  public IActionResult Export()
  {
   string sWebRootFolder = _hostingEnvironment.WebRootPath;
   string sFileName = $"{Guid.NewGuid()}.xlsx";
   FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
   using (ExcelPackage package = new ExcelPackage(file))
   {
    // 添加worksheet
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore");
    //添加頭
    worksheet.Cells[1, 1].Value = "ID";
    worksheet.Cells[1, 2].Value = "Name";
    worksheet.Cells[1, 3].Value = "Url";
    //添加值
    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");
  }
 }

通過依賴注入獲取HostingEnvironment,對應可以獲取程序的相關目錄及屬性。

然後添加Index 視圖增加一個鏈接導出Excel

@{ 
}
<h2>ASP.NET Core 導入導出Excel xlsx 文件</h2>
<a asp-action="Export">導出Excel</a>

點擊導出文件,打開結果如下。

 

導入xlsx文件

在index視圖中添加一個上傳文件,添加Import操作。

Index.cshtml

@{ 
}
<h2>ASP.NET Core 導入導出Excel xlsx 文件</h2>
<a asp-action="Export">導出Excel</a>
<hr />
<form enctype="multipart/form-data" method="post" asp-action="Import">
 <input type="file" name="excelfile" />
 <input type="submit" value="上傳" />
</form>

 [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);
   }
  }

運行程序打開http://localhost:5000/xlsx

上傳對應文件,顯示如下。

ASP.NET Core簡單的導入導出Excel 功能也就完成了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved