ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
ASP.NET Core 使用EPPlus.Core導(dǎo)入導(dǎo)出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件導(dǎo)入導(dǎo)出,可以運(yùn)行在Windows, Linux和Mac。
EPPlus.Core 是基于EPPlus 更改而來(lái),在Linux 下需要安裝libgdiplus 。
EPPlus:http://epplus.codeplex.com/
EPPlus.Core:https://github.com/VahidN/EPPlus.Core
下面在ASP.NET Core 中導(dǎo)入導(dǎo)出Excel xlsx 文件。
新建項(xiàng)目
新建一個(gè)ASP.NET Core Web Application 項(xiàng)目ASPNETCoreExcel,選擇Web 應(yīng)用程序 不進(jìn)行身份驗(yàn)證。
然后添加EPPlus.Core 引用。
使用NuGet 命令行:
Install-Package EPPlus.Core
也可以使用NuGet包管理器安裝。
導(dǎo)出xlsx文件
新建一個(gè)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");
}
}
通過(guò)依賴注入獲取HostingEnvironment,對(duì)應(yīng)可以獲取程序的相關(guān)目錄及屬性。
然后添加Index 視圖增加一個(gè)鏈接導(dǎo)出Excel
@{
}
<h2>ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件</h2>
<a asp-action="Export">導(dǎo)出Excel</a>
點(diǎn)擊導(dǎo)出文件,打開(kāi)結(jié)果如下。

導(dǎo)入xlsx文件
在index視圖中添加一個(gè)上傳文件,添加Import操作。
Index.cshtml
@{
}
<h2>ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件</h2>
<a asp-action="Export">導(dǎo)出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);
}
}
運(yùn)行程序打開(kāi)http://localhost:5000/xlsx

上傳對(duì)應(yīng)文件,顯示如下。

ASP.NET Core簡(jiǎn)單的導(dǎo)入導(dǎo)出Excel 功能也就完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET MVC實(shí)現(xiàn)批量文件上傳
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)批量文件上傳,簡(jiǎn)單介紹單文件上傳的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
asp.net Page.Controls對(duì)象(找到所有服務(wù)器控件)
通過(guò)此對(duì)象找到所有服務(wù)器控件。2008-11-11
VS2017做為Unity3D的腳本編輯器需要的最精簡(jiǎn)組件
這篇文章主要為大家詳細(xì)介紹了VS2017做為Unity3D的腳本編輯器需要的最精簡(jiǎn)組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Asp.net獲取服務(wù)器指定文件夾目錄文件并提供下載的方法
這篇文章主要介紹了Asp.net獲取服務(wù)器指定文件夾目錄文件并提供下載的方法,涉及使用http協(xié)議操作文件的技巧,需要的朋友可以參考下2015-01-01
ASP.NET通過(guò)第三方網(wǎng)站Bitly實(shí)現(xiàn)短鏈接地址程序
這篇文章主要介紹了ASP.NET通過(guò)第三方網(wǎng)站Bitly實(shí)現(xiàn)短鏈接地址程序的步驟,需要的朋友可以參考下。2016-06-06
三種方法讓Response.Redirect在新窗口打開(kāi)
通過(guò)設(shè)置form的target屬性同樣可以讓Response.Rederect所指向的url在新的窗口打開(kāi),下面為大家介紹三種具體的實(shí)現(xiàn)方法2013-10-10

