ASP.NET Core 导入导出Excel xlsx 文件
2023-09-27 14:28:58 时间
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 功能也就完成了。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
相关文章
- ADO.NET(OleDb)读取Excel表格时的一个BUG
- Using serilog with azure application insights and .Net core
- Understanding multiple anti-forgery tokens in ASP.NET MVC
- JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List<T>.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
- .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类
- ASP.NET MVC5 实现基于Quartz.NET任务调度
- Word控件Spire.Doc 【Table】教程(17):如何在 C#、VB.NET 中删除 Word 表格中的行和列
- 【C#】asp.net
- C#数据库教程7-ADO.NET三层架构和数据库DBNull问题
- .NET EntityFramework
- Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded
- [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持wordexcelpptpdf等格式)
- RDIFramework.NET框架SOA解(集Windows服务、WinForm形式和IIS发布形式)-分布式应用程序
- ASP.NET Web深入开发系列
- asp.net 性能提升
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- Oracle Net Configuration(监听程序和网络服务配置)