首页 技术 正文
技术 2022年11月21日
0 收藏 927 点赞 3,283 浏览 2886 个字

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[, ].Value = "ID";
worksheet.Cells[, ].Value = "Name";
worksheet.Cells[, ].Value = "Url";
//添加值
worksheet.Cells["A2"].Value = ;
worksheet.Cells["B2"].Value = "LineZero";
worksheet.Cells["C2"].Value = "http://www.cnblogs.com/linezero/"; worksheet.Cells["A3"].Value = ;
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>

点击导出文件,打开结果如下。

ASP.NET Core 导入导出Excel xlsx 文件

导入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[];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
for (int row = ; row <= rowCount; row++)
{
for (int col = ; 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 xlsx 文件

上传对应文件,显示如下。

ASP.NET Core 导入导出Excel xlsx 文件

ASP.NET Core简单的导入导出Excel 功能也就完成了。

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918